使用Symfony 2的Twig {%if statement%}

时间:2017-10-10 13:06:03

标签: symfony if-statement twig

如何在Twig中访问实体属性(使用OpenClassroom上的Winzou Symfony2教程)?

类别是我的班级广告的一个属性,其中包含我的所有广告。我只想在没有广告实体的情况下显示错误消息。

这是我的手风琴,列出"广告"来自我的广告实体。一个手风琴用于incident类别,另一个用于类别general

        <div class="well">
        <div class="accordion" id="accordion2">
            <div class="accordion-group">
                <div class="accordion-heading">
                    <a class="accordion-toggle" data-toggle="collapse" data-parent="#accordion2" href="#collapseGen">
                        <h3><i class="icon-chevron-right"></i> Incidents</h3>
                    </a>
                </div>
                <div id="collapseGen" class="accordion-body collapse">
                    <div class="accordion-inner">
                        <div class="accordion" id="accordion4">
                            {% for advert in listAdverts %}
                                {% if advert.category == "incident" %}
                                    <div class="accordion-group">
                                        <div class="accordion-heading decalage">
                                            <a href="{{ path('info_view', {'id': advert.id}) }}">
                                                <h3 class="{{ advert.category }}">{{ advert.title }}</h3>
                                            </a>
                                            <div>
                                                <span>{{ advert.content|truncate(100, true, '...')|raw }}</span>
                                            </div>
                                            <div>
                                                {% if (advert.UpdatedAt is empty) %}
                                                    <i>Créé par {{ advert.author }}, le {{ advert.date|date('d/m/Y') }}
                                                        à {{ advert.date|date('H:i') }}</i>
                                                {% else %}
                                                    <i>Modifié par {{ advert.author }},
                                                        le {{ advert.updatedAt|date('d/m/Y') }}
                                                        à {{ advert.updatedAt|date('H:i') }}</i>
                                                {% endif %}
                                            </div>
                                            <div>{% if advert.readers is not empty %}
                                                <i>Vu par :
                                                    {% for reader in advert.readers %}
                                                        {{ reader.username }},
                                                    {% endfor %}
                                                    {% endif %}</i>
                                            </div>
                                            <br>
                                            <div>
                                                {% if is_granted("IS_AUTHENTICATED_FULLY") %}
                                                    <p>
                                                        <a href="{{ path('info_edit', {'id': advert.id}) }}"
                                                           class="btn btn-default">
                                                            <i class="fa fa-pencil-square-o" aria-hidden="true"></i>
                                                            Modifier
                                                        </a>
                                                        <a href="#myModal{{ advert.id }}" role="button" class="btn btn-danger"
                                                           data-toggle="modal">
                                                            <i class="fa fa-trash-o" aria-hidden="true"></i>
                                                            Supprimer
                                                        </a>
                                                    </p>
                                                    <div id="myModal{{ advert.id }}" class="modal hide fade">
                                                        <div class="modal-header">
                                                            <button type="button" class="close" data-dismiss="modal"
                                                                    aria-hidden="true">&times;</button>
                                                            <h3>Suppression annonce "{{ advert.title }}"</h3>
                                                        </div>

                                                        <div class="modal-body">
                                                            <p>Voulez-vous vraiment supprimer l'annonce : "{{ advert.title }}" ? </p>
                                                        </div>

                                                        <div class="modal-footer parente">
                                                            <div><a href="#" class="btn" data-dismiss="modal">Annuler</a></div>
                                                            <div>
                                                                <form class="nomargin"
                                                                      action="{{ path('info_delete', {'id': advert.id}) }}"
                                                                      method="post">
                                                                    <input type="submit" value="Supprimer"
                                                                           class="btn btn-danger"/>{{ form_rest(form) }}
                                                                </form>
                                                            </div>
                                                        </div>
                                                    </div>
                                                {% endif %}
                                            </div>
                                        </div>
                                    </div>
{# HERE I DONT KNOW HOW TO DO IT #}
                                {% else %} No Advert !
                                {% endif %}
                            {% endfor %}

                        </div>
                    </div>
                </div>
            </div>
            <div class="accordion-group">
                <div class="accordion-heading">
                    <a class="accordion-toggle" data-toggle="collapse" data-parent="#accordion2" href="#collapseCo">
                        <h3><i class="icon-chevron-right"></i> Général</h3>
                    </a>
                </div>
                <div id="collapseCo" class="accordion-body collapse">
                    <div class="accordion-inner">
                        <div class="accordion" id="accordion4">
                            {% for advert in listAdverts %}
                                {% if advert.category == "general" %}
                                    <div class="accordion-group">
                                        <div class="accordion-heading decalage">
                                            <div class="accordion-heading">
                                                <a href="{{ path('info_view', {'id': advert.id}) }}">
                                                    <h3 class="{{ advert.category }}">{{ advert.title }}</h3>
                                                </a>
                                                <span>{{ advert.content|truncate(100, true, '...')|raw }}</span>
                                                <div>
                                                    {% if (advert.UpdatedAt is empty) %}
                                                        <i>Créé par {{ advert.author }}, le {{ advert.date|date('d/m/Y') }}
                                                            à {{ advert.date|date('H:i') }}</i>
                                                    {% else %}
                                                        <i>Modifié par {{ advert.author }},
                                                            le {{ advert.updatedAt|date('d/m/Y') }}
                                                            à {{ advert.updatedAt|date('H:i') }}</i>
                                                    {% endif %}
                                                </div>
                                                <div>{% if advert.readers is not empty %}
                                                    <i>Vu par :
                                                        {% for reader in advert.readers %}
                                                            {{ reader.username }},
                                                        {% endfor %}
                                                        {% endif %}</i>
                                                </div>
                                                <br>
                                                <div>
                                                    {% if is_granted("IS_AUTHENTICATED_FULLY") %}
                                                        <p>
                                                            <a href="{{ path('info_edit', {'id': advert.id}) }}"
                                                               class="btn btn-default">
                                                                <i class="fa fa-pencil-square-o" aria-hidden="true"></i>
                                                                Modifier
                                                            </a>
                                                            <a href="#myModal{{ advert.id }}" role="button" class="btn btn-danger"
                                                               data-toggle="modal">
                                                                <i class="fa fa-trash-o" aria-hidden="true"></i>
                                                                Supprimer
                                                            </a>
                                                        </p>

                                                        <div id="myModal{{ advert.id }}" class="modal hide fade">

                                                            <div class="modal-header">
                                                                <button type="button" class="close" data-dismiss="modal"
                                                                        aria-hidden="true">&times;</button>
                                                                <h3>Suppression annonce "{{ advert.title }}"</h3>
                                                            </div>

                                                            <div class="modal-body">
                                                                <p>Voulez-vous vraiment supprimer l'annonce : "{{ advert.title }}" ? </p>
                                                            </div>

                                                            <div class="modal-footer parente">
                                                                <div><a href="#" class="btn" data-dismiss="modal">Annuler</a></div>
                                                                <div>
                                                                    <form class="nomargin"
                                                                          action="{{ path('info_delete', {'id': advert.id}) }}"
                                                                          method="post">
                                                                        <input type="submit" value="Supprimer"
                                                                               class="btn btn-danger"/>{{ form_rest(form) }}
                                                                    </form>
                                                                </div>
                                                            </div>
                                                        </div>
                                                    {% endif %}
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                {% endif %}
                            {% endfor %}
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
{% endblock %}

我的错误讯息&#34;没有广告!&#34;没有出现..

编辑:根据Mitchel的回答,我试过这个:

{% endif %}
    {% else %}<li>No Adverts</li>
{% endfor %}

我不知道为什么当一个广告属于另一个类别时它不起作用...

{% for advert in listAdverts if advert.category == "incident" %}
    {% if listAdverts is empty%}
        <li>No Adverts</li>
    {% endif %}
{% endfor %}

该代码不起作用。

2 个答案:

答案 0 :(得分:1)

我不确定你的问题,但如果是这样,你可以根据文件尝试这样的事情:

{% for user in users %}
    <li>{{ user.username|e }}</li>
{% else %}
    <li><em>no user found</em></li>
{% endfor %}

答案 1 :(得分:0)

在Twig中,for语句可以包含关联的else。它基本上意味着如果for没有结果,则使用else。您应该将{% endif %}放在{% else %}之前,它应该有效。