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

时间:2018-08-14 20:04:30

标签: jekyll

我在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 %}

虽然它确实连接了在帖子的YAML布局内指定为系列的一部分的帖子,但它也将其他帖子也作为系列的一部分进行了连接,即使它们在YAML前期问题中未指定为“系列”的帖子,尽管有if语句。这导致我的所有帖子都成为我不希望的常规“系列”的一部分。我只想指定一些帖子作为系列的一部分,而其他帖子只是唯一的一次性文章。

我尝试了以下方法:

  1. 完全不指定YAML主题内的系列
  2. 设置系列:False
  3. 设置系列:无

1 个答案:

答案 0 :(得分:0)

编辑:

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

_includes / series.html 中的所有内容均受page.series != null的限制,但是<div class="panel seriesNote">的内容始终会被打印。

您需要根据情况将代码(全部<div class="panel seriesNote">[...]</div>)移动到 _includes / series.html

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

END EDIT

如果您未在头件事情中设置series,则page.seriesnil

如果您将其保留为空,则也为nil

因此,{% if page.series != null %}将帮助您仅选择将序列设置为值的页面。

关于计数器的说明:

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

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

如果您这样做:

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

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

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

相关问题