The Python Oracle

python ternary in jinja2 gives TemplateSyntaxError: tag name expected

--------------------------------------------------
Rise to the top 3% as a developer or hire one of them at Toptal: https://topt.al/25cXVn
--------------------------------------------------

Music by Eric Matyas
https://www.soundimage.org
Track title: Realization

--

Chapters
00:00 Python Ternary In Jinja2 Gives Templatesyntaxerror: Tag Name Expected
00:33 Accepted Answer Score 22
00:54 Answer 2 Score 2
01:11 Thank you

--

Full question
https://stackoverflow.com/questions/3591...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#python #css #python27 #jinja2

#avk47



ACCEPTED ANSWER

Score 22


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



ANSWER 2

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