From: Armin Ronacher Date: Sun, 18 Mar 2007 22:10:15 +0000 (+0100) Subject: [svn] added filters "capture" and "format" X-Git-Tag: 2.0rc1~424 X-Git-Url: http://git.tremily.us/?a=commitdiff_plain;h=f0f4debbce0442b7ecdda493f72148a7416d5b25;p=jinja2.git [svn] added filters "capture" and "format" --HG-- branch : trunk --- 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 }