Search code examples
if-statementjinja2statements

Correct syntax for multiple if not + OR in Jinja2 if statement


I want to do X if my asnitem.asn is neither one of the values I gave.

What I thought would work:

{%- if not asnitem.asn == 45102 or if not asnitem.asn == 24429 or if not asnitem.asn == 132203 %}

But that just gives me a syntax error. So I also tried:

{%- (if not asnitem.asn == 45102) or (if not asnitem.asn == 24429) or (if not asnitem.asn == 132203) %}

But that also does not work. So im currently confused how to do multiple or's in a Jinja2 if statemements. Thanks to everyone who gives a answer. Also if there is a way better way to do something like this, please tell me. Maybe something like?:

{%- if not asnitem.asn == 45102 || 24429 || 132203 %}


Solution

  • The in logical operator should help you in this case. Try this sample:

    {%- if not asnitem.asn in [45102, 24429, 132203] %}
    

    in operator here checks presence of the left-hand value (asnitem.asn) in the right-hand list ([45102, 24429, 132203]). And not inverts the result of the check.