在 liquid 的集合页面中每第 n 次迭代渲染一些东西
Render something every nth iteration inside a collection page in liquid
如何添加不同的列表项,例如源自 collection.products
? 的第三个、第七个和第十一个列表项之后的图像
我试过 jQuery 但没有成功。有什么想法吗?
<ul id="collection-grid" data-id="{{ section.id }}" class="grid">
{%- for product in collection.products -%}
<li class="grid-item">
{% render 'product-wrapper' %}
</li>
{%- endfor -%}
</ul>
<script>
$( "ul:nth-child(3)" ).append( "<li class="info-card card-wrapper"><img...></li>" );
</script>
我不知道 HTML 你想添加什么以及在哪里添加,但我在下面做了一个猜测。
使用 cycle
tag and capture
it in a variable and then use if
显示您想要的内容。
(不需要jQuery)
<ul id="collection-grid" data-id="{{ section.id }}" class="grid">
{%- for product in collection.products -%}
{% capture thisIteration %}{% cycle '1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12' %}{% endcapture %}
<li class="grid-item">
{% render 'product-wrapper' %}
{% if thisIteration == '3' %}
<span>Here is the HTML that is added only on every 3rd iteration</span>
{% endif %}
{% if thisIteration == '7' %}
<span>Here is the HTML that is added only on every 7th iteration</span>
{% endif %}
{% if thisIteration == '11' %}
<span>Here is the HTML that is added only on every 11th iteration</span>
{% endif %}
</li>
{%- endfor -%}
</ul>
还有一个 modulo filter 您可以使用,但是对于第 7 次和第 11 次这样的迭代,我觉得上面的内容更具可读性。
<ul id="collection-grid" data-id="{{ section.id }}" class="grid">
{%- for product in collection.products -%}
{% assign mod3 = forloop.index | modulo: 3 %}
{% assign mod7 = forloop.index | modulo: 7 %}
{% assign mod11 = forloop.index | modulo: 11 %}
<li class="grid-item">
{% render 'product-wrapper' %}
{% if mod3 == 0 %}
<span>Here is the HTML that is added only on every 3rd iteration</span>
{% endif %}
{% if mod7 == 0 %}
<span>Here is the HTML that is added only on every 7th iteration</span>
{% endif %}
{% if mod11 == 0 %}
<span>Here is the HTML that is added only on every 11th iteration</span>
{% endif %}
</li>
{%- endfor -%}
</ul>
如何添加不同的列表项,例如源自 collection.products
? 的第三个、第七个和第十一个列表项之后的图像
我试过 jQuery 但没有成功。有什么想法吗?
<ul id="collection-grid" data-id="{{ section.id }}" class="grid">
{%- for product in collection.products -%}
<li class="grid-item">
{% render 'product-wrapper' %}
</li>
{%- endfor -%}
</ul>
<script>
$( "ul:nth-child(3)" ).append( "<li class="info-card card-wrapper"><img...></li>" );
</script>
我不知道 HTML 你想添加什么以及在哪里添加,但我在下面做了一个猜测。
使用 cycle
tag and capture
it in a variable and then use if
显示您想要的内容。
(不需要jQuery)
<ul id="collection-grid" data-id="{{ section.id }}" class="grid">
{%- for product in collection.products -%}
{% capture thisIteration %}{% cycle '1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12' %}{% endcapture %}
<li class="grid-item">
{% render 'product-wrapper' %}
{% if thisIteration == '3' %}
<span>Here is the HTML that is added only on every 3rd iteration</span>
{% endif %}
{% if thisIteration == '7' %}
<span>Here is the HTML that is added only on every 7th iteration</span>
{% endif %}
{% if thisIteration == '11' %}
<span>Here is the HTML that is added only on every 11th iteration</span>
{% endif %}
</li>
{%- endfor -%}
</ul>
还有一个 modulo filter 您可以使用,但是对于第 7 次和第 11 次这样的迭代,我觉得上面的内容更具可读性。
<ul id="collection-grid" data-id="{{ section.id }}" class="grid">
{%- for product in collection.products -%}
{% assign mod3 = forloop.index | modulo: 3 %}
{% assign mod7 = forloop.index | modulo: 7 %}
{% assign mod11 = forloop.index | modulo: 11 %}
<li class="grid-item">
{% render 'product-wrapper' %}
{% if mod3 == 0 %}
<span>Here is the HTML that is added only on every 3rd iteration</span>
{% endif %}
{% if mod7 == 0 %}
<span>Here is the HTML that is added only on every 7th iteration</span>
{% endif %}
{% if mod11 == 0 %}
<span>Here is the HTML that is added only on every 11th iteration</span>
{% endif %}
</li>
{%- endfor -%}
</ul>