且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

比较jinja2模板中的两个变量

更新时间:2023-11-02 16:59:58

我有同样的问题,两个变量有一个整数值当它们具有相同的值时,不等于相同。



有什么办法可以使这个工作以任何方式进行。
还尝试使用str()== str()或int()== int(),但总是有一个未定义的错误。 >

找到解决方案:
只需使用 {{var | string()}} {{var | int()}}
https://***.com/a/19993378/1232796



阅读文档可以在这里找到 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://***.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 %}