Ahmed Nuaman Ahmed Nuaman - 3 years ago 269
Python Question

Python jinja2 shorthand conditional

Say I have this:

{% if files %}
{% else %}
{% endif %}

In PHP, say, I can write a shorthand conditional, like:

<?php echo $foo ? 'yes' : 'no'; ?>

Is there then a way I can translate this to work in a jinja2 template:

'yes' if foo else 'no'

Answer Source

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

{{ 'Update' if files else 'Continue' }}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download