Twig if/else不工作

Ale*_*Ale 2 php symfony twig

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

在调试中,我知道描述不是null,描述显示那些拥有它的描述.

{% 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 %}
Run Code Online (Sandbox Code Playgroud)

Mat*_*teo 7

您可以使用for语句的else子句进行简化:

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

希望这有帮助