在 jinja 中设置变量

发布于 2024-09-19 03:35:59 字数 173 浏览 11 评论 0原文

我想知道如何在 jinja 中设置一个变量与另一个变量。我会解释一下,我有一个子菜单,我想显示哪个链接处于活动状态。我尝试了这个:

{% set active_link = {{recordtype}} -%}

其中 recordtype 是为我的模板指定的变量。

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}} -%}

where recordtype is a variable given for my template.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

佼人 2024-09-26 03:35:59

{{ }} 告诉模板打印值,这在您尝试执行的表达式中不起作用。相反,请使用 {% set %} 模板标记,然后按照与普通 Python 代码相同的方式分配值。

{% set testing = 'it worked' %}
{% set another = testing %}
{{ another }}

结果:

it worked

{{ }} 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 }}

Result:

it worked
海拔太高太耀眼 2024-09-26 03:35:59

多变量赋值的良好简写

{% set label_cls, field_cls = "col-md-7", "col-md-3" %}

Nice shorthand for Multiple variable assignments

{% set label_cls, field_cls = "col-md-7", "col-md-3" %}
木落 2024-09-26 03:35:59

只需像这样设置即可

{% set active_link = recordtype -%}

Just Set it up like this

{% set active_link = recordtype -%}
土豪我们做朋友吧 2024-09-26 03:35:59

您可以使用 set 标签来完成此操作。请参阅官方文档

例如,

{% set foo = "bar" %}
{{ foo }}

输出

bar

注意:存在范围问题,这意味着变量值在循环迭代之间不会持续存在,例如,如果您希望某些输出以先前循环值和当前循环值之间的比较为条件:

{# **DOES NOT WORK AS INTENDED** #}

{% set prev = 0 %}
{% for x in [1, 2, 3, 5] %}
{%- if prev != x - 1 %}⋮ (prev was {{ prev }})
{% endif -%}
{{ x }}
{%- set prev = x %}
{% endfor %}

打印

1
⋮ (prev was 0)
2
⋮ (prev was 0)
3
⋮ (prev was 0)
5

是因为变量没有持久化。相反,您可以使用可变的命名空间包装器:

{% set ns = namespace(prev=0) %}
{% for x in [1, 2, 3, 5] %}
{%- if ns.prev != x - 1 %}⋮ (ns.prev was {{ ns.prev }})
{% endif -%}
{{ x }}
{%- set ns.prev = x %}
{% endfor %}

打印。

1
2
3
⋮ (ns.prev was 3)
5

它按预期

You can do this with the set tag. See the official documentation.

For example,

{% set foo = "bar" %}
{{ foo }}

outputs

bar

Note: there are scoping issues which means that variable values don’t persist between loop iterations, for example, if you want some output to be conditional on a comparison between previous and current loop values:

{# **DOES NOT WORK AS INTENDED** #}

{% set prev = 0 %}
{% for x in [1, 2, 3, 5] %}
{%- if prev != x - 1 %}⋮ (prev was {{ prev }})
{% endif -%}
{{ x }}
{%- set prev = x %}
{% endfor %}

prints

1
⋮ (prev was 0)
2
⋮ (prev was 0)
3
⋮ (prev was 0)
5

because the variable isn’t persisted. Instead you can use a mutable namespace wrapper:

{% set ns = namespace(prev=0) %}
{% for x in [1, 2, 3, 5] %}
{%- if ns.prev != x - 1 %}⋮ (ns.prev was {{ ns.prev }})
{% endif -%}
{{ x }}
{%- set ns.prev = x %}
{% endfor %}

which prints

1
2
3
⋮ (ns.prev was 3)
5

as intended.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文