Jekyll 中的递增循环计数器
Increment loop counter in Jekyll
因此,我正在尝试创建一个页面,其中在开始时仅显示前 10 个 post。
这样做,很简单:
<ul>
{% for post in site.posts limit:10 %}
<li>//show post</li>
{% endfor %}
</ul>
但它变得棘手,因为我想添加一个按钮,使接下来的 10 post 秒可见。然后在接下来的 10 post 秒内再显示一个,直到显示网站上的每个 post。
基本上,所有post都会在索引上生成,因为jekyll是静态的。但是第十个post之后的所有内容都会被js和css隐藏。但是,我仍然需要生成那些 post 十个十个对吗?
所以我正在尝试做类似的事情
{% for post in site.posts limit:10 offset:9%}
//show post
{% endfor %}
{% for post in site.posts limit:10 offset:19%}
//show post
{% endfor %}
它仍然很糟糕,因为我必须每十 post 秒编写一次循环,所以它非常糟糕。所以基本上我必须创建一个循环,每 10 posts 创建我的 UL 10 posts。归结为,我怎样才能每次都将偏移量加十?我必须使用一个变量,但我不确定这里的语法是如何工作的?
这是我想要的 html 渲染图:
<div id="first-ten">
// ten posts
</div>
<div id="see-more-1">
// ten posts
</div>
<div id="see-more-2">
// ten posts
</div>
<div id="see-more-3">
// last six posts for instance
</div>
我同意使用 Liquid 是一种奇怪的方法,但只是为了证明它可以做到:
---
---
{% comment %}Constant, can assign in _config.yml or YAML front matter if desired{% endcomment %}
{% assign postsPerPage = 2 %}
{% assign numPages = site.posts | size | divided_by: postsPerPage %}
{% comment %}Sketchy ceiling function since Liquid does integer division{% endcomment %}
{% if site.posts.size | modulo: postsPerPage > 0 %}
{% assign numPages = numPages | plus: 1 %}
{% endif %}
Total pages: {{ numPages }}<br><br>
{% assign endIndex = numPages | minus: 1 %}
{% comment %}Output section{% endcomment %}
{% for pageNum in (0..endIndex) %}
{% assign offset = pageNum | times: postsPerPage %}
<div id="see-more-{{ pageNum }}" style="border-style: inset;">
Page {{ pageNum }}:<br>
<ol>
{% assign posts = site.posts | sort: 'title' %}
{% for post in posts limit: postsPerPage offset: offset %}
<li>{{ post.title }}</li>
{% endfor %}
</ol>
</div>
{% endfor %}
我已经用 1-11 个帖子测试了上面的页面。我尽量把它写得不言自明,所以有可能减少 variables/calculations.
的数量
因此,我正在尝试创建一个页面,其中在开始时仅显示前 10 个 post。
这样做,很简单:
<ul>
{% for post in site.posts limit:10 %}
<li>//show post</li>
{% endfor %}
</ul>
但它变得棘手,因为我想添加一个按钮,使接下来的 10 post 秒可见。然后在接下来的 10 post 秒内再显示一个,直到显示网站上的每个 post。
基本上,所有post都会在索引上生成,因为jekyll是静态的。但是第十个post之后的所有内容都会被js和css隐藏。但是,我仍然需要生成那些 post 十个十个对吗?
所以我正在尝试做类似的事情
{% for post in site.posts limit:10 offset:9%}
//show post
{% endfor %}
{% for post in site.posts limit:10 offset:19%}
//show post
{% endfor %}
它仍然很糟糕,因为我必须每十 post 秒编写一次循环,所以它非常糟糕。所以基本上我必须创建一个循环,每 10 posts 创建我的 UL 10 posts。归结为,我怎样才能每次都将偏移量加十?我必须使用一个变量,但我不确定这里的语法是如何工作的?
这是我想要的 html 渲染图:
<div id="first-ten">
// ten posts
</div>
<div id="see-more-1">
// ten posts
</div>
<div id="see-more-2">
// ten posts
</div>
<div id="see-more-3">
// last six posts for instance
</div>
我同意使用 Liquid 是一种奇怪的方法,但只是为了证明它可以做到:
---
---
{% comment %}Constant, can assign in _config.yml or YAML front matter if desired{% endcomment %}
{% assign postsPerPage = 2 %}
{% assign numPages = site.posts | size | divided_by: postsPerPage %}
{% comment %}Sketchy ceiling function since Liquid does integer division{% endcomment %}
{% if site.posts.size | modulo: postsPerPage > 0 %}
{% assign numPages = numPages | plus: 1 %}
{% endif %}
Total pages: {{ numPages }}<br><br>
{% assign endIndex = numPages | minus: 1 %}
{% comment %}Output section{% endcomment %}
{% for pageNum in (0..endIndex) %}
{% assign offset = pageNum | times: postsPerPage %}
<div id="see-more-{{ pageNum }}" style="border-style: inset;">
Page {{ pageNum }}:<br>
<ol>
{% assign posts = site.posts | sort: 'title' %}
{% for post in posts limit: postsPerPage offset: offset %}
<li>{{ post.title }}</li>
{% endfor %}
</ol>
</div>
{% endfor %}
我已经用 1-11 个帖子测试了上面的页面。我尽量把它写得不言自明,所以有可能减少 variables/calculations.
的数量