从postlist array october cms获取博客文章?

时间:2017-01-05 18:23:28

标签: laravel twig octobercms

我正在创建一个博客页面,帖子列表页面采用不同大小的图像形式。我需要能够单独设置每个帖子列表项的样式,因此需要能够使用twig访问帖子列表数组并获取帖子。

例如,当您从帖子中访问特色图片时,您可以使用:

    post.featured_images[0].path

我想这样做,但要选择帖子列表中的第一篇帖子。

1 个答案:

答案 0 :(得分:1)

无论您想对第一篇文章做什么,您都可以使用循环中的迭代变量访问第一篇文章。

Twig中的迭代变量很少,我通常使用loop.index变量。 例如:

{% for post in posts %}

    {% if loop.index == 1 %}
        {{ post.title }}
        {# this is the first post title #}
    {% else %}
        {{ post.title }}
        {# this is others posts title #}
    {% endif %}

{% endfor %}

当你去loop.index == 2时,你可以访问第二篇文章。如果它等于3,你可以访问第三个帖子等。

另一种选择是loop.first

{% if loop.first %}
{# It goes here if it's the first record of the loop #}
{% endif %}
{% if loop.last %}
{# It goes here if it's the last record of the loop #}
{% endif %}

要了解有关Twig循环变量的更多信息:http://twig.sensiolabs.org/doc/2.x/tags/for.html#the-loop-variable

相关问题