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

Popular posts from this blog

jQuery clickable div with working mailto link inside -

java - Getting corefrences with Standard corenlp package -

WPF: binding viewmodel property of type DateTime to Calendar inside ItemsControl -