Jinja2宏导入“具有上下文"和全局变量:{%来自具有上下文%的file.html import macro_name} [英] Jinja2 macro import "with context" and global variables: {% from file.html import macro_name with context %}

查看:66
本文介绍了Jinja2宏导入“具有上下文"和全局变量:{%来自具有上下文%的file.html import macro_name}的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在用于python的Jinja2模板引擎中,我的宏带有上下文"无法从上下文访问全局变量.

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

导入语句:

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

宏用法:

{{ all_macros.the_macro() }}

正在调用宏,但是全局变量没有值.

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

以下是具有上下文"

推荐答案

之所以出现此问题,是因为有两种调用宏的方法. 一种是从all_macros导入. 第二个是名称the_macro.

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.

如果我使用电话:

{{ the_macro() }}

然后宏可以从上下文访问全局变量! 这实际上是对文档的说明.

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

这篇关于Jinja2宏导入“具有上下文"和全局变量:{%来自具有上下文%的file.html import macro_name}的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆