From f0f4debbce0442b7ecdda493f72148a7416d5b25 Mon Sep 17 00:00:00 2001 From: Armin Ronacher Date: Sun, 18 Mar 2007 23:10:15 +0100 Subject: [PATCH] [svn] added filters "capture" and "format" --HG-- branch : trunk --- jinja/filters.py | 35 ++++++++++++++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/jinja/filters.py b/jinja/filters.py index dde0be6..01cab4f 100644 --- a/jinja/filters.py +++ b/jinja/filters.py @@ -548,6 +548,37 @@ def do_string(): return lambda e, c, v: e.to_unicode(v) +def do_format(*args): + """ + Apply python string formatting on an object: + + .. sourcecode:: jinja + + {{ "%s - %s"|format("Hello?", "Foo!") }} + -> Hello? - Foo! + + Note that you cannot use the mapping syntax (``%(name)s``) + like in python. + """ + def wrapped(env, context, value): + return env.to_unicode(value) % args + return wrapped + + +def do_capture(name='captured'): + """ + Store the value in a variable called ``captured`` or a variable + with the name provided. Useful for filter blocks: + + .. sourcecode:: jinja + + {% filter capture('foo') %} + .... + {% endfilter %} + {{ foo }} + """ + + FILTERS = { 'replace': do_replace, 'upper': do_upper, @@ -582,5 +613,7 @@ FILTERS = { 'int': do_int, 'float': do_float, 'string': do_string, - 'urlize': do_urlize + 'urlize': do_urlize, + 'format': do_format, + 'capture': do_capture } -- 2.26.2