python - Accessing global attributes from inside a macro in Jinja2 -
i've been using macros in jinja2 extensively , find them dry-ish; there 1 thing bothering me: how access global stuff macros? neat if somehow access url_for() natively macro.
you can make callable available in jinja environment:
jinja_env = environment(...) jinja_env.globals['url_for'] = url_for
for example, output u'foobar' in shell:
from jinja2 import environment env = environment() env.globals['foo'] = lambda: "foobar" env.from_string('{% macro bar() %}{{ foo() }}{% endmacro %}{{ bar() }}').render()
Comments
Post a Comment