The Python Oracle

Jinja2 shorthand conditional

Become part of the top 3% of the developers by applying to Toptal https://topt.al/25cXVn

--

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

--

Chapters
00:00 Question
00:30 Accepted answer (Score 507)
00:44 Answer 2 (Score 9)
01:00 Thank you

--

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

Accepted answer links:
[inline if-expressions]: http://jinja.pocoo.org/docs/templates/#i...

--

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

--

Tags
#python #jinja2 #conditionaloperator

#avk47



ACCEPTED ANSWER

Score 566


Yes, it's possible to use inline if-expressions:

{{ 'Update' if files else 'Continue' }}



ANSWER 2

Score 12


Alternative way (but it's not python style. It's JS style)

{{ files and 'Update' or 'Continue' }}