Title: {{ item.name }}<...">

Twig Empty Variable & # 8594; An exception?

I run the loop in Twig:

{% for item in items %}
<div class="description">
   Title: {{ item.name }}<br />
   Price: {{ item.price }}
</div>
{% else %}
<p>...</p>
{% endfor %}

If item.price is empty, that excludes me. Can't I just get Twig to output "nothing" when a particular value is empty?

Or do I always need {% if item.x%} {{item.x}} {% endif%} for all values?

+5
source share
4 answers

You can also try defaultfilter :

{{ item.price|default("nothing") }}
+12
source

Go to config.yml and set the following there:

twig:
    strict_variables: false
+11
source
{% if item.price is defined and item.price not in [''] %}
    {{ item.price }}
{% endif %}

, , . Twig, :)

+5

:

{{ item.price|default }}

default - - FALSE, .

+2

All Articles