【问题标题】:Why does this jekyll template not work?为什么这个 jekyll 模板不起作用?
【发布时间】:2018-01-09 18:37:00
【问题描述】:

短版:

为什么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 }}

详情

我的帖子2018-01-05-man-command.md 有一个 YAML 前题:

---
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 使用它来设置帖子的布局:

{% 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 时,我得到的只是:

下一篇是

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

【问题讨论】:

    标签: html jekyll liquid github-pages


    【解决方案1】:

    我相信capture 标签只捕获字符串,而不是帖子。请参阅here 了解更多信息。

    我不相信where 过滤器支持您正在使用的contains 语法。请参阅here 了解更多信息。

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

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

    这是我的做法:

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

    【讨论】:

      猜你喜欢
      • 2011-04-11
      • 2011-07-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-20
      相关资源
      最近更新 更多