首页 > 解决方案 > 页面属性上的液体过滤数组

问题描述

我有一个 jekyll 站点,它有两个页面(page1.htmlpage2.html),它们都使用相同的布局。此布局打印有关给定子目录中某些其他页面的一些信息。例如

{% for p in site.pages %}
    {{ p.title }}
{% endfor %}
---
layout: test
---
this page lists the title of my books...

这将打印我网站中每个页面的标题,但我希望它只打印子目录中页面的标题/books,所以我会将布局页面更改为

{% for p in site.pages | where: 'dir','/books/' %}
    {{ p.title }}
{% endfor %}

这很好用,但我希望另一个页面使用相同的布局并列出我的漫画内容(位于/comics文件夹内)而不是我的书籍,所以我将通过以下方式更改我的网站的结构:

{% for p in site.pages | where: 'dir','/{{ page.directory_to_scan }}/' %}
    {{ p.title }}
{% endfor %}
---
layout: test
directory_to_scan: books
---
this page lists the title of my books...
---
layout: test
directory_to_scan: comics
---
this page lists the title of my comics...

但是,这不起作用,并且根本没有打印任何标题。

标签: htmljekyllliquid

解决方案


您不能混合使用标签和过滤器(除了assign)。此外,无需将变量括filter在双括号内:

---
directory_to_scan: '/comics/'
---

布局将使用:

{% assign my_pages = site.pages | where: 'dir', page.directory_to_scan %}
{% for p in my_pages %}
  {{ p.title }}
{% endfor %}

推荐阅读