i18n Extension
--------------
+**Import name:** `jinja2.ext.i18n`
+
Jinja2 currently comes with one extension, the i18n extension. It can be
used in combination with `gettext`_ or `babel`_. If the i18n extension is
enabled Jinja2 provides a `trans` statement that marks the wrapped string as
.. _Babel: http://babel.edgewall.org/
+do
+~~
+
+**Import name:** `jinja2.ext.do`
+
+The do aka expression-statement extension adds a simple `do` tag to the
+template engine that works like a variable expression but ignores the
+return value.
+
+
.. _writing-extensions:
Writing Extensions
For multiple placeholders always use keyword arguments to `format` as other
languages may not use the words in the same order.
+
+
+do
+~~
+
+If the expression-statement extension is loaded a tag called `do` is available
+that works exactly like the regular variable expression (``{{ ... }}``) just
+that it doesn't print anything. This can be used to modify lists:
+
+ {% do navigation.append('a string') %}
import jinja2
from werkzeug import script
-env = jinja2.Environment(extensions=['jinja2.ext.i18n'])
+env = jinja2.Environment(extensions=['jinja2.ext.i18n', 'jinja2.ext.do'])
def shell_init_func():
def _compile(x):
DEFAULT_NAMESPACE = {
'range': xrange,
'dict': lambda **kw: kw,
- 'lipsum': generate_lorem_ipsum,
- 'void': lambda *a: u''
+ 'lipsum': generate_lorem_ipsum
}
return nodes.Output([node])
+class ExprStmtExtension(Extension):
+ """Adds a `do` tag to Jinja2 that works like the print statement just
+ that it doesn't print the return value.
+ """
+ tags = set(['do'])
+
+ def parse(self, parser):
+ node = nodes.ExprStmt(lineno=parser.stream.next().lineno)
+ node.node = parser.parse_tuple()
+ return node
+
+
def extract_from_ast(node, gettext_functions=GETTEXT_FUNCTIONS):
"""Extract localizable strings from the given template node.
#: nicer import names
i18n = InternationalizationExtension
+do = ExprStmtExtension