# HG changeset patch # User Ludovic Chabant # Date 1500823847 25200 # Node ID 3e69f18912f5efbdab7d9183f65156d6b63f467b # Parent 1c6a4d2ec16ec3448a3565fab91e6e7541b80e4f 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. diff -r 1c6a4d2ec16e -r 3e69f18912f5 piecrust/templating/jinja/environment.py --- a/piecrust/templating/jinja/environment.py Sun Jul 23 08:27:18 2017 -0700 +++ b/piecrust/templating/jinja/environment.py Sun Jul 23 08:30:47 2017 -0700 @@ -20,12 +20,14 @@ # Before we create the base Environement, let's figure out the options # we want to pass to it. - twig_compatibility_mode = app.config.get('jinja/twig_compatibility') - + # # Disable auto-reload when we're baking. if app.config.get('baker/is_baking'): kwargs.setdefault('auto_reload', False) + # Don't unload templates from the cache. + kwargs.setdefault('cache_size', -1) + # Let the user override most Jinja options via the site config. for name in ['block_start_string', 'block_end_string', 'variable_start_string', 'variable_end_string', @@ -47,10 +49,6 @@ from jinja2 import StrictUndefined kwargs.setdefault('undefined', StrictUndefined) - # Twig trims blocks. - if twig_compatibility_mode is True: - kwargs['trim_blocks'] = True - # All good! Create the Environment. super(PieCrustEnvironment, self).__init__(*args, **kwargs) @@ -78,10 +76,7 @@ 'emaildate': get_email_date, 'date': get_date}) - # Backwards compatibility with Twig. - if twig_compatibility_mode is True: - self.filters['raw'] = self.filters['safe'] - self.globals['pcfail'] = raise_exception + self.filters['raw'] = self.filters['safe'] def _paginate(self, value, items_per_page=5): ctx = self.app.env.render_ctx_stack.current_ctx diff -r 1c6a4d2ec16e -r 3e69f18912f5 piecrust/templating/jinjaengine.py --- a/piecrust/templating/jinjaengine.py Sun Jul 23 08:27:18 2017 -0700 +++ b/piecrust/templating/jinjaengine.py Sun Jul 23 08:30:47 2017 -0700 @@ -9,9 +9,8 @@ class JinjaTemplateEngine(TemplateEngine): - # Name `twig` is for backwards compatibility with PieCrust 1.x. - ENGINE_NAMES = ['jinja', 'jinja2', 'j2', 'twig'] - EXTENSIONS = ['html', 'jinja', 'jinja2', 'j2', 'twig'] + ENGINE_NAMES = ['jinja', 'jinja2', 'j2'] + EXTENSIONS = ['html', 'jinja', 'jinja2', 'j2'] def __init__(self): self.env = None @@ -19,11 +18,11 @@ self._jinja_not_found = None def renderSegmentPart(self, path, seg_part, data): - self._ensureLoaded() - if not _string_needs_render(seg_part.content): return seg_part.content + self._ensureLoaded() + part_path = _make_segment_part_path(path, seg_part.offset) self.env.loader.segment_parts_cache[part_path] = ( path, seg_part.content) @@ -49,20 +48,12 @@ def renderFile(self, paths, data): self._ensureLoaded() - tpl = None - logger.debug("Looking for template: %s" % paths) - rendered_path = None - for p in paths: - try: - tpl = self.env.get_template(p) - rendered_path = p - break - except self._jinja_syntax_error as tse: - raise self._getTemplatingError(tse) - except self._jinja_not_found: - pass - if tpl is None: + try: + tpl = self.env.select_template(paths) + except self._jinja_syntax_error as tse: + raise self._getTemplatingError(tse) + except self._jinja_not_found: raise TemplateNotFoundError() try: @@ -75,8 +66,8 @@ if self.app.debug: raise msg = "Error rendering Jinja markup" - rel_path = os.path.relpath(rendered_path, self.app.root_dir) - raise TemplatingError(msg, rel_path) from ex + name = getattr(tpl, 'name', '') + raise TemplatingError(msg, name) from ex def _getTemplatingError(self, tse, filename=None): filename = tse.filename or filename @@ -90,9 +81,11 @@ return stats = self.app.env.stats - stats.registerTimer('JinjaTemplateEngineEnvironmentSetup', + stats.registerTimer('JinjaTemplateEngine_setup', raise_if_registered=False) - with stats.timerScope('JinjaTemplateEngineEnvironmentSetup'): + stats.registerTimer('JinjaTemplateEngine_extensions', + raise_if_registered=False) + with stats.timerScope('JinjaTemplateEngine_setup'): self._load() def _load(self): @@ -104,12 +97,7 @@ ext_names = [ext_names] # Turn on autoescape by default. - autoescape = get_config('twig/auto_escape') - if autoescape is not None: - logger.warning("The `twig/auto_escape` setting is now called " - "`jinja/auto_escape`.") - else: - autoescape = get_config('jinja/auto_escape', True) + autoescape = get_config('jinja/auto_escape', True) if autoescape: ext_names.append('autoescape') @@ -160,4 +148,3 @@ def _make_segment_part_path(path, start): return '$part=%s:%d' % (path, start) -