我经常需要对画廊等动态元素进行一些复杂的布局。这是一个例子:
<ul>
<li class="slide">
<img src="img_01.jpg">
<img src="img_02.jpg">
</li>
<li class="slide">
<img src="img_03.jpg">
<img src="img_04.jpg">
</li>
<li class="slide">
<img src="img_05.jpg">
<img src="img_06.jpg">
</li>
</ul>
我已经设法用下面的代码片段做到了。但如果可能的话,我想要一些关于如何使它更灵活或更简单的建议,比如按任意数字分组。也许使用 cycle() 或任何其他方法。使用 slice() 或 array[1:2] 表示法我得到了奇怪的结果。
<ul>
{% for image in gallery %}
{% set current = loop.index %}
{% set next = current + 1 %}
{% if current is odd %}
<li class="slide">
{% for image in gallery %}
{% if loop.index in [current,next] %}
{% set th = TimberImage(image) %}
<img src="{{th.src}}">
{% endif %}
{% endfor %}
</li>
{% endif %}
{% endfor %}
</ul>
欢迎任何建议。Timber 对于使用 Timber::compile 或具有完整路由的自定义主题进行快速进出修复非常方便。这个问题的目的是创建一些可以重复使用的代码片段。
向创作者致敬。 https://github.com/timber/timber