# now return a `CallBlock` node that calls our _cache_support
# helper method on this extension.
- return nodes.CallBlock(
- nodes.Call(self.attr('_cache_support'), args, [], None, None),
- [], [], body
- ).set_lineno(lineno)
+ return nodes.CallBlock(self.call_method('_cache_support', args),
+ [], [], body).set_lineno(lineno)
def _cache_support(self, name, timeout, caller):
"""Helper callback."""
Extensions always have to extend the :class:`jinja2.ext.Extension` class:
.. autoclass:: Extension
- :members: parse, attr
+ :members: parse, attr, call_method
.. attribute:: identifier
--- /dev/null
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+"""
+ Jinja2 Debug Interface
+ ~~~~~~~~~~~~~~~~~~~~~~
+
+ Helper script for internal Jinja2 debugging. Requires Werkzeug.
+
+ :copyright: Copyright 2008 by Armin Ronacher.
+ :license: BSD.
+"""
+import sys
+import jinja2
+from werkzeug import script
+
+env = jinja2.Environment()
+
+def shell_init_func():
+ def _compile(x):
+ print env.compile(x, raw=True)
+ result = {
+ 'e': env,
+ 'c': _compile,
+ 't': env.from_string,
+ 'p': env.parse
+ }
+ for key in jinja2.__all__:
+ result[key] = getattr(jinja2, key)
+ return result
+
+
+def action_compile():
+ print env.compile(sys.stdin.read(), raw=True)
+
+action_shell = script.make_shell(shell_init_func)
+
+
+if __name__ == '__main__':
+ script.run()
# if this extends statement was in the root level we can take
# advantage of that information and simplify the generated code
# in the top level from this point onwards
- self.has_known_extends = True
+ if frame.rootlevel:
+ self.has_known_extends = True
# and now we have one more
self.extends_so_far += 1
from jinja2.optimizer import optimize
from jinja2.compiler import generate
from jinja2.runtime import Undefined, Context
-from jinja2.debug import translate_exception, translate_syntax_error
from jinja2.exceptions import TemplateSyntaxError
from jinja2.utils import import_string, LRUCache, Markup, missing, concat
try:
return Parser(self, source, filename).parse()
except TemplateSyntaxError, e:
+ from jinja2.debug import translate_syntax_error
exc_type, exc_value, tb = translate_syntax_error(e)
raise exc_type, exc_value, tb
try:
return concat(self._generate(*args, **kwargs))
except:
+ from jinja2.debug import translate_exception
exc_type, exc_value, tb = translate_exception(sys.exc_info())
raise exc_type, exc_value, tb
for item in self._generate(*args, **kwargs):
yield item
except:
+ from jinja2.debug import translate_exception
exc_type, exc_value, tb = translate_exception(sys.exc_info())
raise exc_type, exc_value, tb
is the name token that matched. This method has to return one or a
list of multiple nodes.
"""
+ raise NotImplementedError()
def attr(self, name, lineno=None):
"""Return an attribute node for the current extension. This is useful
"""
return nodes.ExtensionAttribute(self.identifier, name, lineno=lineno)
+ def call_method(self, name, args=None, kwargs=None, dyn_args=None,
+ dyn_kwargs=None, lineno=None):
+ """Call a method of the extension."""
+ if args is None:
+ args = []
+ if kwargs is None:
+ kwargs = []
+ return nodes.Call(self.attr(name, lineno=lineno), args, kwargs,
+ dyn_args, dyn_kwargs, lineno=lineno)
+
class InternationalizationExtension(Extension):
"""This extension adds gettext support to Jinja2."""