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?
In-line conditional statements Jinja in-line conditionals are started with a curly brace and a % symbol, like {% if condition %} and closed with {% endif %} . You can optionally include both {% elif %} and {% else %} tags.
If. Nunjucks allows you to use if as an inline expression. For example: {{ "true" if var else "false" }} outputs the string "true" if the variable var is defined, else it outputs "false".
For loops start with {% for my_item in my_collection %} and end with {% endfor %} . This is very similar to how you'd loop over an iterable in Python. Here my_item is a loop variable that will be taking values as we go over the elements.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With