Forum Discussion

Re: Jinja2 Pipelines question, so nothing if field is empty

One more question. What's the logic behind {% if variable %}? Is there a difference between:

{% if variable %}
{% if variable is defined%}
{% if variable != '' %}

?

------------------------------
Daniel Johnson
------------------------------

3 Replies

  • SeanConnaughto1's avatar
    SeanConnaughto1
    Qrew Cadet
    From what I understand (which may not be fully correct), using {% if variable is defined %} is checking if that variable exists, regardless of having data associated or not. 

    Using {% if variable %} equates the if statement to a basic true/false, true if there is any data associated, false if nothing is associated (or if the field value is 0). I think the use of {% if variable != '' %} just may not be best practice/correct usage in jinja2, but that is where I may be completely incorrect.

    ------------------------------
    Sean Connaughton
    ------------------------------
    • DanielJohnson2's avatar
      DanielJohnson2
      Qrew Trainee
      Thanks Sean, that all makes sense.

      ------------------------------
      Daniel Johnson
      ------------------------------
      • DanielJohnson2's avatar
        DanielJohnson2
        Qrew Trainee
        I posted this same question on Stack Overflow, and someone suggested:
        {% if c.call_rating not none %}
        Call Rating: {{c.call_rating}}
        {% endif %}​
        However, Pipelines wouldn't let me save that, so I tried:
        {% if c.call_rating %}
        Call Rating: {{c.call_rating}}
        {% endif %}​
        And that worked! Not defining the else seems to null the entry if {% if c.call_rating %} is false.

        ------------------------------
        Daniel Johnson
        ------------------------------