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?

jkdev
  • 11,360
  • 15
  • 54
  • 77
Viktória
  • 131
  • 1
  • 1
  • 3
  • 5
    look at https://stackoverflow.com/questions/15168831/how-to-write-a-multiline-jinja-statement –  Nov 23 '17 at 14:38
  • Possible duplicate of [How to write a multiline Jinja statement](https://stackoverflow.com/questions/15168831/how-to-write-a-multiline-jinja-statement) – Abr001am Nov 23 '17 at 14:39
  • I have seen this amswer https://stackoverflow.com/questions/15168831/how-to-write-a-multiline-jinja-statement . 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 Nov 24 '17 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 Oct 09 '18 at 14:45
  • 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 – anand_v.singh Mar 01 '19 at 09:09

2 Answers2

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.

akanxh2000
  • 79
  • 2
  • 8
  • 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 Nov 06 '20 at 07: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.

ElMaquinista
  • 168
  • 1
  • 11