jinja2中的python ternary提供TemplateSyntaxError:预期标签名称 [英] python ternary in jinja2 gives TemplateSyntaxError: tag name expected
本文介绍了jinja2中的python ternary提供TemplateSyntaxError:预期标签名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一张桌子,并且如果person.storyPublished的值为true,我想更改tr的背景颜色,否则不执行任何操作.
I have a table and I want to change background color of tr if value of person.storyPublished is true otherwise do nothing.
我的代码如下:
{% for person in people %}
<tr class="row-person {% '.row-story-published' if person.storyPublished else ' ' %}" >
<td>
{{ person.name }}
</td>
...
我收到此错误:
jinja2.exceptions.TemplateSyntaxError
TemplateSyntaxError: tag name expected
而CSS部分在这里:
<style>
.row-story-published{
background-color: #b3ffb3;
}
</style>
为什么会这样?我没有注意到的我想念的是什么?任何帮助:)
why is this happening? What i miss that I don't notice? Any help :)
推荐答案
您使用了"{%%}",该标签想要获取if,endif等标签. 如果您只想执行一段python代码(例如三元表达式),则应使用像这样的双括号
You used "{% %}" which wants to get a tag like if, endif etc. If you just want to execute a piece of python code, like your ternary expression, you should use double braces like so
{{ 'row-story-published' if person.storyPublished else ' ' }}
这篇关于jinja2中的python ternary提供TemplateSyntaxError:预期标签名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文