Mercurial > piecrust2
comparison piecrust/templating/jinjaengine.py @ 274:1163bd034dc5
jinja: Provide a more "standard" Jinja configuration by default.
Only enable backwards compatibility stuff if asked for it. Expose most Jinja
options to the user via the site configuration.
| author | Ludovic Chabant <ludovic@chabant.com> |
|---|---|
| date | Sun, 01 Mar 2015 17:59:26 -0800 |
| parents | 27d623a241c6 |
| children | 078e64dba77d |
comparison
equal
deleted
inserted
replaced
| 273:d70a4adb61dd | 274:1163bd034dc5 |
|---|---|
| 89 self.app.templates_dirs) | 89 self.app.templates_dirs) |
| 90 loader = FileSystemLoader(self.app.templates_dirs) | 90 loader = FileSystemLoader(self.app.templates_dirs) |
| 91 extensions = [ | 91 extensions = [ |
| 92 PieCrustHighlightExtension, | 92 PieCrustHighlightExtension, |
| 93 PieCrustCacheExtension, | 93 PieCrustCacheExtension, |
| 94 PieCrustSpacelessExtension] | 94 PieCrustSpacelessExtension, |
| 95 twig_compatibility_mode = self.app.config.get('jinja/twig_compatibility') | 95 PieCrustFormatExtension] |
| 96 if twig_compatibility_mode is None or twig_compatibility_mode is True: | |
| 97 extensions.append(PieCrustFormatExtension) | |
| 98 if autoescape: | 96 if autoescape: |
| 99 extensions.append('jinja2.ext.autoescape') | 97 extensions.append('jinja2.ext.autoescape') |
| 100 self.env = PieCrustEnvironment( | 98 self.env = PieCrustEnvironment( |
| 101 self.app, | 99 self.app, |
| 102 loader=loader, | 100 loader=loader, |
| 103 extensions=extensions) | 101 extensions=extensions) |
| 104 | 102 |
| 105 | 103 |
| 106 class PieCrustEnvironment(Environment): | 104 class PieCrustEnvironment(Environment): |
| 107 def __init__(self, app, *args, **kwargs): | 105 def __init__(self, app, *args, **kwargs): |
| 106 self.app = app | |
| 107 | |
| 108 # Before we create the base Environement, let's figure out the options | |
| 109 # we want to pass to it. | |
| 110 twig_compatibility_mode = app.config.get('jinja/twig_compatibility') | |
| 111 | |
| 112 # Disable auto-reload when we're baking. | |
| 113 if app.config.get('baker/is_baking'): | |
| 114 kwargs.setdefault('auto_reload', False) | |
| 115 | |
| 116 # Let the user override most Jinja options via the site config. | |
| 117 for name in ['block_start_string', 'block_end_string', | |
| 118 'variable_start_string', 'variable_end_string', | |
| 119 'comment_start_string', 'comment_end_string', | |
| 120 'line_statement_prefix', 'line_comment_prefix', | |
| 121 'trim_blocks', 'lstrip_blocks', | |
| 122 'newline_sequence', 'keep_trailing_newline']: | |
| 123 val = app.config.get('jinja/' + name) | |
| 124 if val is not None: | |
| 125 kwargs.setdefault(name, val) | |
| 126 | |
| 127 # Twig trims blocks. | |
| 128 if twig_compatibility_mode is True: | |
| 129 self.trim_blocks = True | |
| 130 | |
| 131 # All good! Create the Environment. | |
| 108 super(PieCrustEnvironment, self).__init__(*args, **kwargs) | 132 super(PieCrustEnvironment, self).__init__(*args, **kwargs) |
| 109 self.app = app | 133 |
| 110 self.auto_reload = True | 134 # Now add globals and filters. |
| 111 self.globals.update({ | 135 self.globals.update({ |
| 112 'fail': raise_exception}) | 136 'fail': raise_exception}) |
| 137 | |
| 113 self.filters.update({ | 138 self.filters.update({ |
| 114 'keys': get_dict_keys, | 139 'keys': get_dict_keys, |
| 115 'values': get_dict_values, | 140 'values': get_dict_values, |
| 116 'paginate': self._paginate, | 141 'paginate': self._paginate, |
| 117 'formatwith': self._formatWith, | 142 'formatwith': self._formatWith, |
| 122 'stripoutertag': strip_outer_tag, | 147 'stripoutertag': strip_outer_tag, |
| 123 'stripslash': strip_slash, | 148 'stripslash': strip_slash, |
| 124 'titlecase': title_case, | 149 'titlecase': title_case, |
| 125 'atomdate': get_atom_date, | 150 'atomdate': get_atom_date, |
| 126 'date': get_date}) | 151 'date': get_date}) |
| 127 # Backwards compatibility with PieCrust 1.x. | |
| 128 self.globals.update({ | |
| 129 'pcfail': raise_exception}) | |
| 130 | 152 |
| 131 # Backwards compatibility with Twig. | 153 # Backwards compatibility with Twig. |
| 132 twig_compatibility_mode = app.config.get('jinja/twig_compatibility') | 154 if twig_compatibility_mode is True: |
| 133 if twig_compatibility_mode is None or twig_compatibility_mode is True: | |
| 134 self.trim_blocks = True | |
| 135 self.filters['raw'] = self.filters['safe'] | 155 self.filters['raw'] = self.filters['safe'] |
| 156 self.globals['pcfail'] = raise_exception | |
| 136 | 157 |
| 137 # Add route functions. | 158 # Add route functions. |
| 138 for route in app.routes: | 159 for route in app.routes: |
| 139 name = route.template_func_name | 160 name = route.template_func_name |
| 140 func = self.globals.get(name) | 161 func = self.globals.get(name) |
