我正在使用液体模板,我想循环一个数组,并吐出一些看起来像这样的HTML,
<div class="classname">
<div>
<a>1</a>
<a>2</a>
<a>3</a>
<a>4</a>
<a>5</a>
<a>6</a>
</div>
</div>
<div class="classname">
<div>
<a>7</a>
<a>8</a>
<a>9</a>
<a>10</a>
<a>11/a>
<a>12</a>
</div>
</div>
<div class="classname">
<div>
<a>13</a>
<a>14</a>
<a>15</a>
<a>16</a>
<a>17/a>
<a>18</a>
</div>
</div>
所以基本上每6个循环我想打开2个新的div并关闭之前的2.我已经去了这是我的努力。
{% for block in content_blocks %}
{% assign mod = forloop.index | modulo: 6 %}
{% if mod / 6 == 1 %}
<div class="grid-slide">
<div>
{% endif %}
<a href="{{block.content_block_url}}" class="location-link">
<img src="{{block.content_block_image | url_for_generic_image}}" />
</a>
{% if mod / 6 == 1 %}
</div>
</div>
{% endif %}
{% endfor %}
答案 0 :(得分:0)
您可以使用此液体代码实现它:
{% assign content_blocks_length = content_blocks | size %}
{% for block in content_blocks %}
{% assign mod = forloop.index | modulo: 6 %}
{% if mod == 1 and forloop.index != 1 %}
</div>
</div>
{% endif %}
{% if mod == 1 %}
<div class="grid-slide">
<div>
{% endif %}
<a href="{{block.content_block_url}}" class="location-link">
<img src="{{block.content_block_image | url_for_generic_image}}" />
</a>
{% if forloop.index == content_blocks_length %}
</div>
</div>
{% endif %}
{% endfor %}