Twig 有等效的 'append' 吗?
Does Twig have equivalent 'append'?
{% block page %}
<ul class="gallery">
{% set foo = 0 %}
{% for products in prodArr %}
{% if foo % 4 == 0 %}
<ul class="slides">
<li>Product</li>
</ul>
{% else %}
<li>Another Product<li> // this
{% endif %}
{% set foo = foo + 1 %}
{% endfor %}
</ul>
{% endblock %}
在其他情况下,我希望 li
使用 class 幻灯片附加到 ul
。这可能吗?
我希望我的 HTML 看起来像这样:
<ul class="gallery">
<ul class="slides">
<li></li>
<li></li>
<li></li>
<li></li>
</ul>
<ul class="slides">
<li></li>
<li></li>
<li></li>
<li></li>
</ul>
</ul>
Twig 有 embed
和 include
,但在您的情况下,您可以像这样重构代码:
// UPDATED:
<ul class="gallery">
{% for chunk in products|batch(4) %}
<ul class="slides">
{% for product in chunk %}
<li>{{ product.title }}</li> // assuming you have title property
{% endfor %}
</ul>
{% endfor %}
</ul>
P.S。不幸的是,我无法对其进行测试,但你会得到大致的想法;)
是的,这是可能的。但在那种情况下,您必须这样更正:
{% block page %}
<ul class="gallery">
{% set foo = 0 %}
{% for products in prodArr %}
{% if foo % 4 == 0 %}
**<ul class="slides">**
{% endif %}
<li>Product</li>
{% if foo % 4 == 3 or foo == (prodArr|length - 1) %}
**</ul>**
{% endif %}
{% set foo = foo + 1 %}
{% endfor %}
</ul>
{% endblock %}
{% block page %}
<ul class="gallery">
{% set foo = 0 %}
{% for products in prodArr %}
{% if foo % 4 == 0 %}
<ul class="slides">
<li>Product</li>
</ul>
{% else %}
<li>Another Product<li> // this
{% endif %}
{% set foo = foo + 1 %}
{% endfor %}
</ul>
{% endblock %}
在其他情况下,我希望 li
使用 class 幻灯片附加到 ul
。这可能吗?
我希望我的 HTML 看起来像这样:
<ul class="gallery">
<ul class="slides">
<li></li>
<li></li>
<li></li>
<li></li>
</ul>
<ul class="slides">
<li></li>
<li></li>
<li></li>
<li></li>
</ul>
</ul>
Twig 有 embed
和 include
,但在您的情况下,您可以像这样重构代码:
// UPDATED:
<ul class="gallery">
{% for chunk in products|batch(4) %}
<ul class="slides">
{% for product in chunk %}
<li>{{ product.title }}</li> // assuming you have title property
{% endfor %}
</ul>
{% endfor %}
</ul>
P.S。不幸的是,我无法对其进行测试,但你会得到大致的想法;)
是的,这是可能的。但在那种情况下,您必须这样更正:
{% block page %}
<ul class="gallery">
{% set foo = 0 %}
{% for products in prodArr %}
{% if foo % 4 == 0 %}
**<ul class="slides">**
{% endif %}
<li>Product</li>
{% if foo % 4 == 3 or foo == (prodArr|length - 1) %}
**</ul>**
{% endif %}
{% set foo = foo + 1 %}
{% endfor %}
</ul>
{% endblock %}