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

apache - Add omitted ? to URLs -

redirect - bbPress Forum - rewrite to wwww.mysite prohibits login -

php - How can I stop spam on my custom forum/blog? -