Jinja2 macro import “with context” and global variables: {% from file.html import macro_name with context %}

走远了吗. 提交于 2019-12-12 18:43:16

问题


In Jinja2 template engine for python, my macro "with context" could not access the global variables from the context.

Import statements:

{% import 'en-US/my_macro.html' as  all_macros %}
{% from 'en-US/my_macro.html' import  the_macro  with context  %}

Macro usage:

{{ all_macros.the_macro() }}

The macro is being called, but the globals have no value.

Here's the relevant docs for "with context"


回答1:


The issue arises because there are 2 ways to call the macro. One is from the all_macros import. The second is with the name the_macro.

If I use the call:

{{ the_macro() }}

Then the macros have access to the global variables from the context! This is really a clarification of the documentation.



来源:https://stackoverflow.com/questions/46118860/jinja2-macro-import-with-context-and-global-variables-from-file-html-impor

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!