[svn] added filters "capture" and "format"
authorArmin Ronacher <armin.ronacher@active-4.com>
Sun, 18 Mar 2007 22:10:15 +0000 (23:10 +0100)
committerArmin Ronacher <armin.ronacher@active-4.com>
Sun, 18 Mar 2007 22:10:15 +0000 (23:10 +0100)
--HG--
branch : trunk

jinja/filters.py

index dde0be6a5ec9dff2f903b7977b19ef46e79dc082..01cab4fb5d336a4eb72dc7e5c9a3a76e7b3680f9 100644 (file)
@@ -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
 }