From 5d2733fce81b22fb53e63c7ea00154c63dae541f Mon Sep 17 00:00:00 2001 From: Armin Ronacher Date: Thu, 15 May 2008 23:26:52 +0200 Subject: [PATCH] void -> do --HG-- branch : trunk --- docs/extensions.rst | 12 ++++++++++++ docs/templates.rst | 10 ++++++++++ jinja2-debug.py | 2 +- jinja2/defaults.py | 3 +-- jinja2/ext.py | 13 +++++++++++++ 5 files changed, 37 insertions(+), 3 deletions(-) diff --git a/docs/extensions.rst b/docs/extensions.rst index 63e9cd9..4f9aa22 100644 --- a/docs/extensions.rst +++ b/docs/extensions.rst @@ -25,6 +25,8 @@ example creates a Jinja2 environment with the i18n extension loaded:: 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 @@ -90,6 +92,16 @@ The usage of the `i18n` extension for template designers is covered as part .. _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 diff --git a/docs/templates.rst b/docs/templates.rst index 3adf98a..935eff7 100644 --- a/docs/templates.rst +++ b/docs/templates.rst @@ -1012,3 +1012,13 @@ To use placeholders you can use the `format` filter:: 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') %} diff --git a/jinja2-debug.py b/jinja2-debug.py index f2fa94a..a250a62 100755 --- a/jinja2-debug.py +++ b/jinja2-debug.py @@ -13,7 +13,7 @@ import sys 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): diff --git a/jinja2/defaults.py b/jinja2/defaults.py index 62dfda2..e124930 100644 --- a/jinja2/defaults.py +++ b/jinja2/defaults.py @@ -25,8 +25,7 @@ LINE_STATEMENT_PREFIX = None DEFAULT_NAMESPACE = { 'range': xrange, 'dict': lambda **kw: kw, - 'lipsum': generate_lorem_ipsum, - 'void': lambda *a: u'' + 'lipsum': generate_lorem_ipsum } diff --git a/jinja2/ext.py b/jinja2/ext.py index 119fe4e..bd64cce 100644 --- a/jinja2/ext.py +++ b/jinja2/ext.py @@ -277,6 +277,18 @@ class InternationalizationExtension(Extension): 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. @@ -360,3 +372,4 @@ def babel_extract(fileobj, keywords, comment_tags, options): #: nicer import names i18n = InternationalizationExtension +do = ExprStmtExtension -- 2.26.2