在 jinja 中设置变量 [英] Set variable in jinja
本文介绍了在 jinja 中设置变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想知道如何在 jinja 中使用另一个变量设置变量.我会解释一下,我有一个子菜单,我想显示哪个链接处于活动状态.我试过这个:
I would like to know how can I set a variable with another variable in jinja. I will explain, I have got a submenu and I would like show which link is active. I tried this:
{% set active_link = {{recordtype}} -%}
其中 recordtype 是为我的模板提供的变量.
where recordtype is a variable given for my template.
推荐答案
{{ }}
告诉模板打印值,这在表达式中不起作用就像你正在尝试做的那样.相反,使用 {% set %}
模板标签,然后像在普通 python 代码中一样分配值.
{{ }}
tells the template to print the value, this won't work in expressions like you're trying to do. Instead, use the {% set %}
template tag and then assign the value the same way you would in normal python code.
{% set testing = 'it worked' %}
{% set another = testing %}
{{ another }}
结果:
it worked
这篇关于在 jinja 中设置变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文