Python Jinja2 模板未正确呈现 if-elif-else 语句

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22024661/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 00:06:04  来源:igfitidea点击:

Jinja2 template not rendering if-elif-else statement properly

pythoncssif-statementjinja2

提问by Matty

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.

我正在尝试在 jinja2 模板中使用 css 设置文本颜色。在以下代码中,如果变量包含字符串,我想将输出字符串设置为以特定字体颜色打印。每次生成模板时,虽然由于 else 语句而以红色打印,但它永远不会看到前两个条件,即使输出应该匹配,我可以知道表生成时变量的输出是什么,并且符合预期. 由于默认情况下以红色打印字符串,我知道我的 css 是正确的。

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, RepoNameis 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.

我的第一个想法是将我正在检查的字符串括在引号中,但这不起作用。接下来是 jinja 没有扩展,RepoOutput[RepoName.index(repo)]但它上面的 for 循环有效,RepoName被正确扩展。我知道如果我添加大括号,它会打印我相当肯定会破坏模板或不起作用的变量。

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://jinja.pocoo.org/docs/templates/#if

http://wsgiarea.pocoo.org/jinja/docs/conditions.html

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

Thanks

谢谢

采纳答案by Martijn Pieters

You are testing if the values of the variableserrorand Alreadyare present in RepoOutput[RepoName.index(repo)]. If these variables don't exist then an undefined objectis used.

您正在测试变量的值errorAlready是否存在于RepoOutput[RepoName.index(repo)]. 如果这些变量不存在,则使用未定义的对象

Both of your ifand eliftests therefore are false; there is no undefined object in the value of RepoOutput[RepoName.index(repo)].

因此,您的ifelif测试都是错误的;RepoOutput[RepoName.index(repo)] 的值中没有未定义的对象。

I think you wanted to test if certain stringsare 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>

Other corrections I made:

我所做的其他更正:

  • Used {% elif ... %}instead of {$ elif ... %}.
  • moved the </tr>tag out of the ifconditional structure, it needs to be there always.
  • put quotes around the idattribute
  • 使用{% elif ... %}代替{$ elif ... %}
  • </tr>标签移出if条件结构,它需要始终存在。
  • id属性周围加上引号

Note that most likely you want to use a classattribute instead here, not an id, the latter must have a value that must be unique across your HTML document.

请注意,您很可能希望在class此处使用属性,而不是id,后者必须具有在整个 HTML 文档中必须唯一的值。

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>