Jekyll 对于循环,在div中每两个立柱包裹一次
基本上,我正在使用(使用模板语言)并尝试为编写一个Jekyll 对于循环,在div中每两个立柱包裹一次,jekyll,liquid,Jekyll,Liquid,基本上,我正在使用(使用模板语言)并尝试为编写一个for循环,它将每两个项包装在一个div中 这是我当前的循环: <div> {% for post in site.posts %} <a href="{{ post.url }}">{{ post.title }}</a> {% endfor %} </div> {site.posts%中的post为%s} {%endfor%} 这将产生如下四个职位: <div>
for循环,它将每两个项包装在一个div
中
这是我当前的循环:
<div>
{% for post in site.posts %}
<a href="{{ post.url }}">{{ post.title }}</a>
{% endfor %}
</div>
{site.posts%中的post为%s}
{%endfor%}
这将产生如下四个职位:
<div>
<a href="#">Title</a>
<a href="#">Title</a>
<a href="#">Title</a>
<a href="#">Title</a>
</div>
我对四个职位的期望产出是:
<div>
<a href="#">Title</a>
<a href="#">Title</a>
</div>
<div>
<a href="#">Title</a>
<a href="#">Title</a>
</div>
我怎样才能做到这一点呢?好的,我已经做了一次快速尝试,但没有合适的样式,但这应该可以:
<div>
{% for post in paginator.posts %}
{% case forloop.index %}
<div>
{% when 1 %}
<a href="">{{ post.title }}</a>
{% when 2 %}
<a href="">{{ post.title }}</a>
</div>
<div>
{% when 3 %}
<a href="">{{ post.title }}</a>
{% when 4 %}
<a href="">{{ post.title }}</a>
</div>
{% endcase %}
{% endfor %}
</div>
{paginator.posts%中的post为%s}
{%case forloop.index%}
{1%时为%1}
{2%时为%1}
{3%时为%1}
{4%时为%1}
{%endcase%}
{%endfor%}
试试这个:
<div>
{% for post in paginator.posts %}
<div>
{% if forloop.index == 1 %}
<a href="">{{ post.title }}</a>
{% endif %}
{% if forloop.index == 2 %}
<a href="">{{ post.title }}</a>
{% endif %}
</div>
<div>
{% if forloop.index == 3 %}
<a href="">{{ post.title }}</a>
{% endif %}
{% if forloop.index == 4 %}
<a href="">{{ post.title }}</a>
{% endif %}
</div>
{% endfor %}
</div>
{paginator.posts%中的post为%s}
{%if-forloop.index==1%}
{%endif%}
{%if-forloop.index==2%}
{%endif%}
{%if-forloop.index==3%}
{%endif%}
{%if-forloop.index==4%}
{%endif%}
{%endfor%}
我真的应该把注意力集中在我正在做的事情上,但是一个一岁的孩子把她的玩具都给了我D
代码现在应该可以工作了:
<div>
<div>
{% for post in paginator.posts %}
{% if forloop.index == 1 %}
<a href="">{{ post.title }}</a>
{% endif %}
{% if forloop.index == 2 %}
<a href="">{{ post.title }}</a>
{% endif %}
{% endfor %}
</div>
<div>
{% for post in paginator.posts %}
{% if forloop.index == 3 %}
<a href="">{{ post.title }}</a>
{% endif %}
{% if forloop.index == 4 %}
<a href="">{{ post.title }}</a>
{% endif %}
{% endfor %}
</div>
</div>
{paginator.posts%中的post为%s}
{%if-forloop.index==1%}
{%endif%}
{%if-forloop.index==2%}
{%endif%}
{%endfor%}
{paginator.posts%中的post为%s}
{%if-forloop.index==3%}
{%endif%}
{%if-forloop.index==4%}
{%endif%}
{%endfor%}
应提供以下内容的html:
<div>
<div>
<a href="">Title 1</a>
<a href="">Title 2</a>
</div>
<div>
<a href="">Title 3</a>
<a href="">Title 4</a>
</div>
</div>
在@smilinmonki666的帮助下,我也按照自己的意愿完成了这项工作,以下是我最终使用的代码:
{% assign current_page_posts = paginator.posts | size %}
{% if current_page_posts > 0 %}
<div>
<div>
{% for post in paginator.posts %}
{% if forloop.index == 1 %}
<a href="{{ post.url }}">{{ post.title }}</a>
{% endif %}
{% if forloop.index == 2 %}
<a href="{{ post.url }}">{{ post.title }}</a>
{% endif %}
{% endfor %}
</div>
{% if current_page_posts > 2 %}
<div>
{% for post in paginator.posts %}
{% if forloop.index == 3 %}
<a href="{{ post.url }}">{{ post.title }}</a>
{% endif %}
{% if forloop.index == 4 %}
<a href="{{ post.url }}">{{ post.title }}</a>
{% endif %}
{% endfor %}
</div>
{% endif %}
</div>
{% endif %}
{%assign current_page_posts=paginator.posts|size%}
{%如果当前页面发布>0%}
{paginator.posts%中的post为%s}
{%if-forloop.index==1%}
{%endif%}
{%if-forloop.index==2%}
{%endif%}
{%endfor%}
{%如果当前页面发布>2%}
{paginator.posts%中的post为%s}
{%if-forloop.index==3%}
{%endif%}
{%if-forloop.index==4%}
{%endif%}
{%endfor%}
{%endif%}
{%endif%}
如果
和帖子的数量是固定的(这似乎是基于您选择的答案的情况),则有一种较短的方法可以获得相同的输出-使用限制和偏移量:
(Liquid的寻呼方法)
这也会返回您想要的输出,但适用于任意数量的帖子。我刚刚遇到了这个()解决方案,它比其他答案中发布的解决方案要好得多,请记住,您需要这个插件()才能工作:
{% for post in paginator.posts %}
{% capture modulo %}{{ forloop.index0 | mod:2 }}{% endcapture %}
{% if modulo == '0' or forloop.first %}
<div>
{% endif %}
<a href="{{ post.url }}">{{ post.title }}</a>
{% if modulo == '1' or forloop.last %}
</div>
{% endif %}
{% endfor %}
{%用于paginator.posts%}
{%capture modulo%}{{forloop.index0 | mod:2}{%endcapture%}
{%if modulo=='0'或forloop.first%}
{%endif%}
{%if modulo=='1'或forloop.last%}
{%endif%}
{%endfor%}
查看Christian的解决方案后,我将我的(基于哈巴狗的)代码更新为:
我知道我比赛迟到了,但我发现我觉得这是一个相当优雅的解决方案,不会让人觉得不舒服
使用for
循环的limit
和offset
参数,我们可以一次迭代一行,每行N篇文章
首先,我们计算需要枚举的行数:
{% assign rows = site.posts.size | divided_by: 2.0 | ceil %}
Ruby的等价物是行=(posts.size/2.0).ceil
(奇数得到它们自己的行)
接下来,我们将迭代行:
{% for i in (1..rows) %}
<div>
然后我们可以从行的偏移量开始迭代POST片段(相当于Ruby中的posts[offset,2]
):
就这样
在Ruby中,这将是:
rows = (posts.size / 2.0).ceil # the number of rows
(1..rows).each do |i|
offset = (i - 1) * 2
# <div>
posts[offset, 2].each do |post|
# <a href="#{post.url}>#{post.title}</a>
end
# </div>
end
rows=(posts.size/2.0).ceil#行数
(1.行)。每行|
偏移量=(i-1)*2
#
立柱[offset,2]。每个立柱都有|立柱|
#
结束
#
结束
现在,在液体中:
{% assign rows = site.posts.size | divided_by: 2.0 | ceil %}
{% for i in (1..rows) %}
{% assign offset = forloop.index0 | times: 2 %}
<div>
{% for post in site.posts limit:2 offset:offset %}
<a href="{{ post.url }}">{{ post.title }}</a>
{% endfor %}
</div>
{% endfor %}
{%assign rows=site.posts.size |除以:2.0 | ceil%}
{(1..rows)%%中的i的%
{%assign offset=forloop.index0 |次:2%}
{站点中帖子的百分比。帖子限制:2偏移量:偏移量%}
{%endfor%}
{%endfor%}
希望这对别人有帮助 您可以使用循环
标记来执行此操作,如中所述
{%for site.posts%}
{%周期“”,''%}
{%周期“”,''%}
{%endfor%}
输出
<div>
<a href="#">Title</a>
<a href="#">Title</a>
</div>
<div>
<a href="#">Title</a>
<a href="#">Title</a>
</div>
你想用div来包装每个链接吗?@AsadMalik:不。看更新后的帖子,我想用div来包装每个帖子链接。
虽然不是最好的代码,但对于jekyll本人来说,像这样的东西很难尝试,因为(据我所知)这种类型的特定操作不太容易排序,但是,当你打算每页发表四篇文章时,这应该可以实现分页的技巧。。。虽然我还没有测试我应该做的理论…非常感谢这一点,但我认为我在使用这一点时会遇到问题。它假设一个页面上总有四篇文章,但事实并非如此。例如,当我只有不到4篇发表的文章,或者我有7篇发表的文章;第一页将有4篇文章,而第二页将有3篇,这将导致无效的HTML,因为错过了
。你显然比我有更多的关于Jekyll循环的经验,因为我来自WordPress的背景,在那里我习惯于做一些事情,比如有没有什么东西可以模仿这个?我不得不替换一些实例{%endfor%}
{% assign offset = forloop.index0 | times: 2 %}
{% for post in site.posts limit:2 offset:offset %}
<a href="{{ post.url }}">{{ post.title }}</a>
{% endfor %}
</div>
{% endfor %}
rows = (posts.size / 2.0).ceil # the number of rows
(1..rows).each do |i|
offset = (i - 1) * 2
# <div>
posts[offset, 2].each do |post|
# <a href="#{post.url}>#{post.title}</a>
end
# </div>
end
{% assign rows = site.posts.size | divided_by: 2.0 | ceil %}
{% for i in (1..rows) %}
{% assign offset = forloop.index0 | times: 2 %}
<div>
{% for post in site.posts limit:2 offset:offset %}
<a href="{{ post.url }}">{{ post.title }}</a>
{% endfor %}
</div>
{% endfor %}
{% for post in site.posts %}
{% cycle '<div>', '' %}
<a href="{{ post.url }}">{{ post.title }}</a>
{% cycle '', '</div>' %}
{% endfor %}
<div>
<a href="#">Title</a>
<a href="#">Title</a>
</div>
<div>
<a href="#">Title</a>
<a href="#">Title</a>
</div>