13

I got stuck in my coding project in jinja templates.

I want to show posts of a user just if 2 conditions are met:

The problematic part is if using and, conditions work perfectly individually but the moment I add and and join them together it does not work.

I have tried it with brackets and without them.

{% for post in posts %}

  {% if (session['user']['username']==post['author']) and (post["id"] | is_liked) %}

  {% else %}
    <li class="row">
      {% include "components/recommended.html" %}
    </li>
  {% endif %}
{% endfor %}

Could you please help me how to write that line, so that both conditions are checked?

5
  • 5
    look at stackoverflow.com/questions/15168831/…
    – user5639219
    Commented Nov 23, 2017 at 14:38
  • Possible duplicate of How to write a multiline Jinja statement
    – Abr001am
    Commented Nov 23, 2017 at 14:39
  • I have seen this amswer stackoverflow.com/questions/15168831/… . And I have tried it before, but it does not work. I have done this: {% if ( (session['user']['username']==post['author']) and (post["id"] | is_liked) ) %} and I have tried this as well {% if ( (session['user']['username']==post['author']) and (post["id"] | is_liked) ) : pass %} The second one gives error becouse of pass, I have deleted it, run it just with brackets but it still does not consider my conditions.
    – Viktória
    Commented Nov 24, 2017 at 10:23
  • @Viktória did you ever solve this issue? I'm having a similar problem where I want to assess multiple conditions in an if, but when I add brackets the template does not compile. If I remove the brackets then it works but I would like to keep the brackets for readability.
    – KvnH
    Commented Oct 9, 2018 at 14:45
  • 1
    Unable to replicate this error, I tried this {% if 1 == 1 and 3 ==2 %} this works {% else %} this doesn't work {% endif %} and it gave expected output, I would encourage you to check your condition Commented Mar 1, 2019 at 9:09

2 Answers 2

6

Check this nested ifs (it suggests nested-ifs can be used how you would normally use them while writing native python code) and combining if conditions (multi-line if statements can be used as long as the code has parens/brackets around it)

Both of them work well.

1
  • 2
    While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes.
    – kadamb
    Commented Nov 6, 2020 at 7:37
1

came across the same problem and my solution looks like this:

{%- set test1 = '00:00' -%}
{%- set test2 = '00:00' -%}
{%- set test3 = '00:00' -%}
{%- set allconditionsmet = 'no' %}
{%- if test1 == '00:00' -%}
{%-   if test2 == '00:00' -%}
{%-     if test3 == '00:00' -%}
{%-       set allconditionsmet = 'yes' %}
{%-     endif -%}
{%-   endif -%}
{%- endif -%}
{%- if allconditionsmet == 'yes' -%}
{{    'all conditions met' }}
{%- else -%}
{{    'some conditions not met' }}
{%- endif -%}

you could also use a bool or an int or whatever you like as the allconditionsmet. maybe you make it an int and count up, then you can determine if 5 of 7 conditions are ok. whatever floats your goat.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.