Set variable in jinja [duplicate]

倖福魔咒の 提交于 2019-11-27 09:04:52

问题


This question already has an answer here:

  • Reference template variable within Jinja expression 1 answer

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.


回答1:


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



回答2:


Nice shorthand for Multiple variable assignments

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



回答3:


Just Set it up like this

{% set active_link = recordtype -%}


来源:https://stackoverflow.com/questions/3727045/set-variable-in-jinja

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