Symfony - 如何省略树枝模板循环中的第一项?

Geo*_*net 6 php symfony twig

我想在没有第一个的情况下显示新闻.我怎么能做到这一点?

这是我必须改变的代码:

<div class="home-box-news carousel-news slide home-box-shadow" id="news" style="clear: both;">
    <ol class="carousel-indicators news-box">

    {% for i in 0..news|length-1 %}
        {% if loop.index is not divisibleby (2) %}
            <li data-target="#news" data-slide-to="{{ i / 2 }}" {% if loop.first %}class="active"{% endif %}></li>
        {% endif %}
    {% endfor %}

    </ol>
    <div class="carousel-inner">
    {% for item in news %}
        {% if loop.index is not divisibleby (2) %}
            <div class="item{% if loop.first %} active{% endif %}">
            <div class="carousel-caption">
        {% endif %}
            <h3><a href="{{ path('home_news_index') }}">{{ item.name }}</a></h3>
            <p class="date">{{ item.createdAt|date('d.m.Y, G:i') }}</p>
            <p {% if loop.index is divisibleby (2) %}style="border-bottom: 0;" {% endif %}>{{ item.content[:110]|nl2br }}{% if item.content|length > 110 %}... <a class="more" href="{{ path('home_news_index') }}">czytaj dalej</a>{% endif %}</p>
        {% if loop.index is divisibleby (2) or loop.last %}
            </div>
            </div>
        {% endif %}
    {% endfor %}
    </div>
</div>
Run Code Online (Sandbox Code Playgroud)

小智 8

罗伯特的建议有更简洁的版本:

{% for item in news[1:] %}
Run Code Online (Sandbox Code Playgroud)


Rob*_*ert 6

您可以使用切片过滤器,它类似于PHP中的array_slice()函数.

{% for item in news[1, news.length -1 ] %}
Run Code Online (Sandbox Code Playgroud)


小智 5

将其放入循环中以省略第一个新闻

{% if loop.index0 > 0 %}
 {# display your news #}
{% endif %}
Run Code Online (Sandbox Code Playgroud)

  • 详细一点:`{% if not loop.first %}` (4认同)