我正在使用 a为系统中存在的每个博客文章for loop
生成。cards
生成的卡片将具有不同的宽度。一张卡片可以是large
或者small
为了实现这一点,我正在使用 HubL 的cycle
语法(此处的文档)。我试图实现的卡片宽度的视觉示例可以在这里看到:
现在,第一行中的第三张卡片将不再是 for 循环的一部分,例如,它将是一张图片。
我遇到的问题:
- 卡片 1 和 2 显示最新帖子(如预期)
- 然后,for 循环停止,所以我可以得到浅蓝色卡(因为
Twig
也不HubL
支持break
语法。 - 在我已经说过之后,我开始了另一个循环
{% if loop.index >=3 %}
- 所以跳过循环中的前两个项目,因为最新的卡片显示在第一个循环中。但是,在cycle
我已经定义的情况下,它会显示错误的卡片类。下面的代码示例:
.container{
display: flex;
flex-wrap: wrap;
justify-content: space-between;
}
.card{
margin-bottom: 15px;
border: 1px solid;
padding: 20px;
}
.card--large{
width: 40%;
}
.card--small{
width: 20%;
}
.card--filler{
width: 20%;
background: lightblue;
}
<div class="container">
<!-- first loop will get two latest cards, first card will be card--large, second card--small -->
{% for content in contents %}
{% if loop.index <=2 %}
<div class="card {% cycle 'card--large','card--small' %}">
Test
</div>
{% endif %}
{% endif %}
<!-- this is outside the loop -->
<div class="card card--filler">
This card is not part of the for loop
</div>
<!-- defining new loop where first two entries should be skipped -->
{% for content in contents %}
{% if loop.index >=3 %}
<div class="card {% cycle 'card--small','card--small','card--large','card--large','card--small','card--small' %}">
Test
</div>
{% endif %}
{% endfor %}
</div>
在上面的最后一个循环中,由于我忽略了前两个帖子,因此它也忽略了cycle
参数中的前两个项目。无论循环如何,卡片都应具有以下类:
- 第一张卡片:卡片--小
- 第二张牌:card--small
- 第三张牌:card--large
- 第四张牌:card--large
- 第五张牌:卡片——小
- 第六张卡片:卡片--小
- 然后它将再次从顶部开始
但是,正在发生的事情是:
- 第一张卡片:卡片--大
- 第二张牌:card--large
- 第三张卡片:卡片——小
- 第四张卡片:卡片——小
- ...
我也尝试过{% if not loop.second %}
,{% if loop.index >=3 %}
但它只是再次获得相同的帖子,例如: