Ahmed Nuaman
Ahmed Nuaman

Reputation: 13211

Jinja2 shorthand conditional

Say I have this:

{% if files %}
    Update
{% else %}
    Continue
{% 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'

Upvotes: 293

Views: 223000

Answers (2)

user3713526
user3713526

Reputation: 481

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

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

Upvotes: 12

bereal
bereal

Reputation: 34252

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

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

Upvotes: 577

Related Questions