class Macro(object):
"""
- Wraps a macor
+ Wraps a macro
"""
def __init__(self, func, name, arguments, defaults, catch_all):
value = self.defaults[idx - arg_count]
except IndexError:
value = Undefined(name)
- arguments['l_' + name] = arg
+ arguments['l_' + name] = value
if self.catch_all:
arguments['l_arguments'] = kwargs
return u''.join(self.func(**arguments))
+import sys
from jinja2 import Environment
from jinja2.compiler import generate
{% macro foo(a) %}[{{ a }}|{{ b }}|{{ c }}]{% endmacro %}
{% for item in seq %}
{{ foo(item) }}
- {% endfor %}
+ {%- endfor %}
{% endblock %}
""")
print ast
print
-print generate(ast, env, "foo.html")
+source = generate(ast, env, "foo.html")
+print source
+print
+
+# execute the template
+code = compile(source, 'jinja://foo.html', 'exec')
+context = {'seq': range(5), 'c': 'foobar'}
+namespace = {'global_context': context}
+exec code in namespace
+for event in namespace['root'](context):
+ sys.stdout.write(event)