Jinja2模板没有正确渲染if-elif-else语句 [英] Jinja2 template not rendering if-elif-else statement properly
问题描述
我正在尝试使用jinja2模板中的css设置文本颜色。在下面的代码中,我想将输出字符串设置为以特定字体颜色打印(如果变量包含字符串)。每次生成模板虽然它由于else语句而以红色打印,但它永远不会看到前两个条件,即使输出应该匹配,我可以告诉变量的输出是什么,当表生成时它是如预期的那样。我知道我的css是正确的,因为默认情况下字符串打印为红色。
I am trying to set the text color using css in a jinja2 template. In the following code I want to set the output string to print in a specific font color if the variable contains a string. Everytime the template is generated though it prints in red due to the else statement, it never see the first two conditions even though the output should be matched, I can tell what the output from the variable is when the table generates and it is as expected. I know my css is correct due to the printing of the string in red by default.
我的第一个想法是用引号括起我正在检查的字符串但是没有不行。接下来是jinja没有扩展 RepoOutput [RepoName.index(repo)]
但是它上面的for循环工作, RepoName
正确扩展。我知道如果我添加括号,它将打印变量,我相当肯定会破坏模板或只是不工作。
My first thought was to enclose the string I was checking for in quotes but that didn't work. Next was that jinja was not expanding RepoOutput[RepoName.index(repo)]
but the for loop above it works, RepoName
is expanded upon properly. I know if I add the braces it will print the variable which I am fairly certain will either break the template or just not work.
我试着查看这些网站然后去了通过全局表达式列表,但找不到类似于我的任何示例或者进一步查看的方向。
I tried looking at these sites and went through the list of global expressions as well but couldn't find any examples similar to mine or a direction in which to look further.
http://jinja.pocoo.org/docs/templates/#if
http://wsgiarea.pocoo.org/jinja/docs/conditions。 html
{% for repo in RepoName %}
<tr>
<td> <a href="http://mongit201.be.monster.com/icinga/{{ repo }}">{{ repo }}</a> </td>
{% if error in RepoOutput[RepoName.index(repo)] %}
<td id=error> {{ RepoOutput[RepoName.index(repo)] }} </td> <!-- I want this in green if it is up-to-date, otherwise I want it in red -->
{% elif Already in RepoOutput[RepoName.index(repo) %}
<td id=good> {{ RepoOutput[RepoName.index(repo)] }} </td> <!-- I want this in green if it is up-to-date, otherwise I want it in red -->
{% else %}
<td id=error> {{ RepoOutput[RepoName.index(repo)] }} </td> <!-- I want this in green if it is up-to-date, otherwise I want it in red -->
</tr>
{% endif %}
{% endfor %}
谢谢
推荐答案
您正在测试变量的值 错误
和
已经
存在于 RepoOutput [RepoName.index(repo)]
中。如果这些变量不存在,则使用未定义的对象。
You are testing if the values of the variables error
and Already
are present in RepoOutput[RepoName.index(repo)]
. If these variables don't exist then an undefined object is used.
你的如果
和 elif
测试都是假的; RepoOutput [RepoName.index(repo)]的值中没有未定义的对象。
Both of your if
and elif
tests therefore are false; there is no undefined object in the value of RepoOutput[RepoName.index(repo)].
我想你想测试某些字符串取而代之的是:
I think you wanted to test if certain strings are in the value instead:
{% if "error" in RepoOutput[RepoName.index(repo)] %}
<td id="error"> {{ RepoOutput[RepoName.index(repo)] }} </td>
{% elif "Already" in RepoOutput[RepoName.index(repo) %}
<td id="good"> {{ RepoOutput[RepoName.index(repo)] }} </td>
{% else %}
<td id="error"> {{ RepoOutput[RepoName.index(repo)] }} </td>
{% endif %}
</tr>
我做的其他更正:
- 使用
{%elif ...%}
而不是{$ elif ...%}
。 - 将
< / tr>
标记移出,如果
条件结构,它必须始终存在。 - 在
id
属性周围加上引号
- Used
{% elif ... %}
instead of{$ elif ... %}
. - moved the
</tr>
tag out of theif
conditional structure, it needs to be there always. - put quotes around the
id
attribute
请注意,您很可能在此处使用 class
属性,而不是 id
,后者必须具有在HTML文档中必须唯一的值。
Note that most likely you want to use a class
attribute instead here, not an id
, the latter must have a value that must be unique across your HTML document.
就我个人而言在这里设置类值并减少重复:
Personally, I'd set the class value here and reduce the duplication a little:
{% if "Already" in RepoOutput[RepoName.index(repo)] %}
{% set row_class = "good" %}
{% else %}
{% set row_class = "error" %}
{% endif %}
<td class="{{ row_class }}"> {{ RepoOutput[RepoName.index(repo)] }} </td>
这篇关于Jinja2模板没有正确渲染if-elif-else语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!