comparison piecrust/templating/jinja/environment.py @ 907:3e69f18912f5

jinja: Remove Twig compatibility, add timer, improve code. - Add a timer for keeping track of extensions' performance. - Use the `select_template` function from Jinja to cut down on a few lines of code.
author Ludovic Chabant <ludovic@chabant.com>
date Sun, 23 Jul 2017 08:30:47 -0700
parents 41b0c94f9833
children
comparison
equal deleted inserted replaced
906:1c6a4d2ec16e 907:3e69f18912f5
18 def __init__(self, app, *args, **kwargs): 18 def __init__(self, app, *args, **kwargs):
19 self.app = app 19 self.app = app
20 20
21 # Before we create the base Environement, let's figure out the options 21 # Before we create the base Environement, let's figure out the options
22 # we want to pass to it. 22 # we want to pass to it.
23 twig_compatibility_mode = app.config.get('jinja/twig_compatibility') 23 #
24
25 # Disable auto-reload when we're baking. 24 # Disable auto-reload when we're baking.
26 if app.config.get('baker/is_baking'): 25 if app.config.get('baker/is_baking'):
27 kwargs.setdefault('auto_reload', False) 26 kwargs.setdefault('auto_reload', False)
27
28 # Don't unload templates from the cache.
29 kwargs.setdefault('cache_size', -1)
28 30
29 # Let the user override most Jinja options via the site config. 31 # Let the user override most Jinja options via the site config.
30 for name in ['block_start_string', 'block_end_string', 32 for name in ['block_start_string', 'block_end_string',
31 'variable_start_string', 'variable_end_string', 33 'variable_start_string', 'variable_end_string',
32 'comment_start_string', 'comment_end_string', 34 'comment_start_string', 'comment_end_string',
44 kwargs.setdefault('undefined', 46 kwargs.setdefault('undefined',
45 make_logging_undefined(logger)) 47 make_logging_undefined(logger))
46 elif undef == 'strict': 48 elif undef == 'strict':
47 from jinja2 import StrictUndefined 49 from jinja2 import StrictUndefined
48 kwargs.setdefault('undefined', StrictUndefined) 50 kwargs.setdefault('undefined', StrictUndefined)
49
50 # Twig trims blocks.
51 if twig_compatibility_mode is True:
52 kwargs['trim_blocks'] = True
53 51
54 # All good! Create the Environment. 52 # All good! Create the Environment.
55 super(PieCrustEnvironment, self).__init__(*args, **kwargs) 53 super(PieCrustEnvironment, self).__init__(*args, **kwargs)
56 54
57 # Now add globals and filters. 55 # Now add globals and filters.
76 'atomdate': get_xml_date, 74 'atomdate': get_xml_date,
77 'xmldate': get_xml_date, 75 'xmldate': get_xml_date,
78 'emaildate': get_email_date, 76 'emaildate': get_email_date,
79 'date': get_date}) 77 'date': get_date})
80 78
81 # Backwards compatibility with Twig. 79 self.filters['raw'] = self.filters['safe']
82 if twig_compatibility_mode is True:
83 self.filters['raw'] = self.filters['safe']
84 self.globals['pcfail'] = raise_exception
85 80
86 def _paginate(self, value, items_per_page=5): 81 def _paginate(self, value, items_per_page=5):
87 ctx = self.app.env.render_ctx_stack.current_ctx 82 ctx = self.app.env.render_ctx_stack.current_ctx
88 if ctx is None or ctx.page is None: 83 if ctx is None or ctx.page is None:
89 raise Exception("Can't paginate when no page has been pushed " 84 raise Exception("Can't paginate when no page has been pushed "