"Elif statement" causes error in jinja template

My friends, I have a code block like below and it works without errors.

                {% if message.message_type == "text" %}
                    <p>{{ message.message }}</p>
                {% else %}
                    <p>else message</p>
                {% endif %}

But when I add an “elif” line to it, I get an error.

                {% if message.message_type == "text" %}
                    <p>{{ message.message }}</p>
                {% elif message.message_type == "audiompeg" %}
                    <p>elif message</p>
                {% else %}
                    <p>else message</p>
                {% endif %}

What’s my mistake?

I think
Direct elif condition inside elif condition will not work.

You can write like this
{% if condition1 %}
# Code to execute when condition1 is true
{% elif condition2 %}
# Code to execute when condition2 is true
{% if nested_condition1 %}
# Code to execute when nested_condition1 is true
{% elif nested_condition2 %}
# Code to execute when nested_condition2 is true
{% else %}
# Code to execute if none of the nested conditions are true
{% endif %}
{% else %}
# Code to execute if none of the outer conditions are true
{% endif %}

Thank your answer @ganureddy

But, my code does not include direct elif condition inside the elif condition.
"<p>elif message</p>" is <p> element for sample. :slight_smile:

Do you have else idea?

I asked the same question to chatgbt. He/She told me that jinja2 does not support “elif” expression. Is this true?

No jinja2 support elif condition

Does anyone have an idea about this? My problem persists.

I receive an error when I use the “elif” line within the “if else” statement. Another point is that I still get the same error when I assign a variable with “set.”

You could try to reduce complexity, for instance replacing the conditions with “true”, “True”, “False”, change the texts (like, no keywords in the rest of the html), etc., until the error goes away, then take it from there.

Hello friends, @Peer @ganureddy
To show you my issue, I’ve created a very simple HTML page and recorded a video. Please watch it.

@gelveri the code you are working on looks like Jinja, but it’s not!

It uses the Micro Templating from John Resig
John Resig - JavaScript Micro-Templating

Frappe team did a nice work around that to make it closer to Jinja, but theres limitations elif is one of those!

Use instead <% } else if (condition) { %> , that should work!

1 Like

I tried this

        {% if page_id == "id1"}
        <p>this page is id1</p>
        % else if (page_id == "id2") { %
        <p>this page is id2</p> }
        {% else %}
        <p>this page is another</p>
        {% endif %}

and i tried this

 <p>{{if page_id === "id1"}}this page is id1{{else if page_id === "id2"}}this page is id2{{else}}this page is another{{/if}}</p>

and this

        { % if (page_id == "id1") { %}
            <p>this page is id1</p>
        % } else if (page_id == "id2") { %
            <p>this page is id2</p>
        % } else { %
            <p>this page is another</p>
        % } 

but they are all wrong. What is the truth about this? :sweat_smile: :sweat_smile: :sweat_smile:

@gelveri take attention that I’m pointing the syntax as <% instead of {%