EgzEst EgzEst - 6 months ago 141
CSS Question

python ternary in jinja2 gives TemplateSyntaxError: tag name expected

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 :)

Answer

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