user3768495
user3768495

Reputation: 4667

Jinja2 - how to assign value to a variable based on if else condition?

I want to do something really simple in Jinja2 - if a>100 then do nothing else let b = 5. Here's my attempt but it does not work:

{% if a|float>100 %}
{% else %}
{% set b=5 %}
{% endif %}

I got an error like this:

jinja2.exceptions.TemplatesyntaxError: expected token 'end of statement block', got '%'

I couldn't find the correct syntax from Jinja2 documentation, so please kindly help. Thanks!

Upvotes: 4

Views: 10459

Answers (1)

Dhia Djobbi
Dhia Djobbi

Reputation: 1289

If nothing happens if the first condition is true, why don't you go straight to the second one?

How about:

{% if ( a|float>100 ) == false %}
    {% set b=5 %}
{% endif %}`

Upvotes: 1

Related Questions