比较jinja2模板中的两个变量 [英] compare two variables in jinja2 template
问题描述
由于我有两个变量 {{profile}}
,其值为test, {{element.author}}
再次输入值test。在jinja2中,当我尝试使用if来比较它们时,什么都没有显示出来。我做了比较,如下所示:
$ $ $ $ $ $ $ {$ profile $ {$ profile如果profile == element.author%} $ b $ { **和{{element.author}}是相同的
{%else%}
{{profile}}和{{element.author}}是**不同**
{ endif%}
我得到输出 test和test不一样
什么错误,我怎么能比较?
我有同样的问题,两个变量有一个整数值当它们具有相同的值时,不等于相同。
有什么办法可以使这个工作以任何方式进行。
还尝试使用str()== str()或int()== int(),但总是有一个未定义的错误。 >
找到解决方案:
只需使用 {{var | string()}}
或 {{var | int()}}
https://stackoverflow.com/a/19993378/1232796
阅读文档可以在这里找到< a href =http://jinja.pocoo.org/docs/dev/templates/#list-of-builtin-filters =nofollow noreferrer> http://jinja.pocoo.org/docs/dev/模板/#内置列表过滤器
在你的情况下,你会想要做的
{$ if if profile | string()== element.author | string()%}
{{profile}}和{{element.author}}是相同的
{%else%}
{{profile}}和{{element.author}}是**不同**
{%endif%}
Given I have two variables {{ profile }}
with a value "test" and {{ element.author }}
again with the value "test". In jinja2 when I try to compare them using an if, nothing shows up. I do the comparison as follows:
{% if profile == element.author %}
{{ profile }} and {{ element.author }} are same
{% else %}
{{ profile }} and {{ element.author }} are **not** same
{% endif %}
I get the output test and test are not same
Whats wrong, how can I compare?
I have the same problem, two variables having an integer value do not equal the same when they are the same value.
Is there any way to make this work in any way. Also tried to use str() == str() or int() == int() but there is always an undefined error.
UPDATE
Found Solution:
Simply use filters such as {{ var|string() }}
or {{ var|int() }}
https://stackoverflow.com/a/19993378/1232796
Reading the doc it can be found here http://jinja.pocoo.org/docs/dev/templates/#list-of-builtin-filters
In your case you would want to do
{% if profile|string() == element.author|string() %}
{{ profile }} and {{ element.author }} are same
{% else %}
{{ profile }} and {{ element.author }} are **not** same
{% endif %}
这篇关于比较jinja2模板中的两个变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!