Twig if/else not working


Twig if/else not working

我不确定为什么我的代码没有执行。我知道它应该以这种方式工作,但现在发生的只是它不做其他部分。

在调试中,我知道描述不是空的,并且描述显示给那些有它的人

{% if descriptions is not null %}
{{ dump(descriptions) }}
    {% for description in descriptions %}
        <td>{{ description.productDesciption }}</td>
    {% endfor %}
{% else %}
    <td> 
        <a href = "{{ path('description') }}">Create a Description for this Product</a>
    </td>
{% endif %}

您可以简化使用for语句的Theelse子句:

{% for description in descriptions %}
     <td>
        {{ description.productDesciption }}
     </td>
{% else %}
     <td> 
        <a href = "{{ path('description') }}">Create a Description for this Product</a>
    </td>
{% endfor %}

希望这能帮助

您可以在for循环中使用if

{% for description in descriptions if descriptions is not null %}
   <td>
    {{ description.productDesciption }}
   </td>
{% else %}
   <td> 
      <a href = "{{ path('description') }}">Create a Description for this Product</a>
   </td>
{% endfor %}