问题
I have a table and I want to change background color of tr if value of person.storyPublished is true otherwise do nothing.
My code looks like this:
{% for person in people %}
<tr class="row-person {% '.row-story-published' if person.storyPublished else ' ' %}" >
<td>
{{ person.name }}
</td>
...
I get this error:
jinja2.exceptions.TemplateSyntaxError
TemplateSyntaxError: tag name expected
and the CSS part is here:
<style>
.row-story-published{
background-color: #b3ffb3;
}
</style>
why is this happening? What i miss that I don't notice? Any help :)
回答1:
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 ' ' }}
回答2:
Template language is different than Python, so has different syntax. You cannot use Python's idiomatic syntax in templates.
<tr class="row-person {% if person.storyPublished %} row-story-published {% endif %}" >
来源:https://stackoverflow.com/questions/35914673/python-ternary-in-jinja2-gives-templatesyntaxerror-tag-name-expected