Jekyll 博客中 series.html 中的 If 语句

If statement in series.html within Jekyll blog

我在我的 jekyll 博客的 _includes 部分的 series.html 文件中有一些语言,粘贴在下面:

{% if page.series %}
{% assign count = '0' %}
{% assign idx = '0' %}
{% for post in site.posts reversed %}
    {% if post.series == page.series %}
        {% capture count %}{{ count | plus: '1' }}{% endcapture %}
        {% if post.url == page.url %}
            {% capture idx %}{{count}}{% endcapture %}
        {% endif %}
    {% endif %}
{% endfor %}
{% endif %}

虽然它确实连接了 post 被指定为 post 的 YAML 布局中的系列的一部分,但它也连接了其他 post 作为系列的一部分尽管有 if 语句,但它们在 post 的 YAML 前面内容中未指定为 'series'。这导致我所有的 post 都成为我不想要的一般 "series" 的一部分。我只想指定一些 post 作为系列的一部分,而其他的只是一次性的 post。

我试过以下方法:

  1. 根本不在 YAML 前端内容中指定系列
  2. 设置系列:False
  3. 设置系列:None

编辑:

您的 post.html 模板中实际发生的情况是,并非所有 html 都是有条件打印的。

_includes/series.html 中的所有内容均以 page.series != null 为条件,但您的 <div class="panel seriesNote"> 总是被打印出来。

您需要在 _includes/series.html 中移动此代码(所有 <div class="panel seriesNote">[...]</div>) .

{% if page.series != null %}
[...]
{% endfor %}
<div class="panel seriesNote">  <<<< moved code
[...]
</div>                          <<<< end moved code
{% endif %}

编辑结束

如果你不在前面设置seriespage.series就是nil

如果留空,也是nil

因此,{% if page.series != null %} 将帮助您 select 仅将系列设置为某个值的页面。

关于计数器的注意事项:

{% assign count = '0' %} => count 是一个字符串

{% capture count %}{{ count | plus: 1 }}{% endcapture %} => 计数是一个字符串

如果你这样做:

{% assign count = 0 %} => count 是一个整数

{% 分配计数 = 计数 | plus: 1 %} => count 是一个整数

在比较中使用计数器时请注意这一点,尝试比较字符串和整数可能会导致错误。