为什么这个 jekyll 模板不起作用?

Why does this jekyll template not work?

简短版本:

为什么当 navbox.next_article 是字符串 '2018-01-05-man-command' 时,以下代码不产生输出?!

{% capture np %} {{ site.posts | where:"post","navbox.next_article contains post.title" }} {% endcapture %}
The next post is {{ np.title }}

详情

我的 post 2018-01-05-man-command.md 有一个 YAML front matter:

---
layout : post
title : 'Man Command'
tags : [RHCSA, RHCSA_mod, Using Essential Tools, Man Command]
categories: [RHCSA]
navbox:
#  prev_article:
  next_article: 2018-01-05-understanding-globbing-and-wildcards
---

_includes/post.html 文件通过以下方式访问:

{% unless include.excerpt %}
  {{ post.content }}
  {% include navbox.html navbox=page.navbox %}    
{% endunless %}

这由 _layout/post.html 使用,它设置 post 的布局:

{% include post.html post=page link_title=false %}

我的 navbox.html 包含:

{% assign navbox = include.navbox %}

{% capture np %} {{ site.posts | where:"post","navbox.next_article contains post.title" }} {% endcapture %}
The next post is {{ np.title }}

然而,当我 运行 bundle exec jekyll serve 时我得到的是:

The next post is

为什么那条线不起作用?我是 jekyll 的新手,所以我可能在对大多数人来说很直观的地方犯了一个错误。请告诉我我能解决什么。

我相信 capture 标签只捕获字符串,而不是 posts。有关详细信息,请参阅 here

我不相信 where 过滤器支持您正在使用的 contains 语法。有关详细信息,请参阅 here

最重要的是,where returns 一个数组。您必须从该数组中获取第一项。

您需要解决这些问题。使用 assign 而不是 capture 来存储 post。并将您的 where 过滤器更改为不使用无效的 contains 语法。 (除非它是在我刚刚链接的问题之后添加的。)

这是我的做法:

{% assign post = site.posts | where:"url", targetUrl | first %}