为什么这个 where 过滤器在 Jekyll 中不起作用?
Why does this where filter not work in Jekyll?
我有一个 github 页面,其 _include
目录有一个文件 courses.html:
{% assign id = include.lessonID | split: '.' %}
{% assign courseID = id | first %}
{% assign node = site.data.courses | where: "id","1" %}
{% assign node = node[1] %}
{%- if node.id == empty -%}
<h1> EMPTY NODE Warning</h1>
{%- else -%}
<h2> DATA Found! </h2>
ID: {{ node.id }}
{%- endif -%}
<p>CourseID: {{node.id}}</p>
<p>Name: {{ node.name }}</p>
<p>Link: {{ node.permalink }}</p>
{%- for node in site.data.courses -%}
{%- if node.id == 1 -%}
<p>{{ node.name }}</p>
<p>{{ node.permalink }}</p>
{%- endif -%}
{%- endfor -%}
它正被 _layout
中名为 courses.html 的文件使用:
{% include courses.html post=page.lessonInfo.lessonID post=page %}
最后,文件 lister.md
包含以下内容:
---
layout: courses
title: 'Test'
lessonInfo:
lessonID : 1.1
modName: 'Installing RHEL Server'
chapterName: 'Using Essential Tools'
---
# There should be some course list around here!
输出结果如下:
DATA Found!
ID:
CourseID:
Name:
Link:
RHCSA
/rhcsa
所以,显然 node
变量不为空,但是当我使用 where
子句选择数组的正确元素时,我无法访问任何属性。
但是,这在使用 for
循环中的 if
语句的第二部分时有效。如何修复 where 子句?!
编辑
@JJJ的建议确实解决了我的问题,但我现在有一个相关的问题。我不能用变量替换表达式 where: "id","1"
中的常量 1
!我尝试了不起作用的普通 where 子句(带引号和不带引号)。所以,我尝试了一个 where 表达式,它也不起作用:
{% assign node = site.data.courses | where: "id",courseID %}
Doesn't work!
{% assign node = site.data.courses | where_exp: "selNode","selNode.id == courseID" %}
Neither does this.
我做错了什么,我该如何解决?
首先,与大多数编程语言一样,数组是零索引的。 node[1]
包含第二个节点,而不是第一个节点。您可能指的是 {% assign node = node[0] %}
。
其次,if node.id == empty
不是检查值是否存在的方式。只需 unless node
或 node.size == 0
.
我有一个 github 页面,其 _include
目录有一个文件 courses.html:
{% assign id = include.lessonID | split: '.' %}
{% assign courseID = id | first %}
{% assign node = site.data.courses | where: "id","1" %}
{% assign node = node[1] %}
{%- if node.id == empty -%}
<h1> EMPTY NODE Warning</h1>
{%- else -%}
<h2> DATA Found! </h2>
ID: {{ node.id }}
{%- endif -%}
<p>CourseID: {{node.id}}</p>
<p>Name: {{ node.name }}</p>
<p>Link: {{ node.permalink }}</p>
{%- for node in site.data.courses -%}
{%- if node.id == 1 -%}
<p>{{ node.name }}</p>
<p>{{ node.permalink }}</p>
{%- endif -%}
{%- endfor -%}
它正被 _layout
中名为 courses.html 的文件使用:
{% include courses.html post=page.lessonInfo.lessonID post=page %}
最后,文件 lister.md
包含以下内容:
---
layout: courses
title: 'Test'
lessonInfo:
lessonID : 1.1
modName: 'Installing RHEL Server'
chapterName: 'Using Essential Tools'
---
# There should be some course list around here!
输出结果如下:
DATA Found!
ID:
CourseID:
Name:
Link:
RHCSA
/rhcsa
所以,显然 node
变量不为空,但是当我使用 where
子句选择数组的正确元素时,我无法访问任何属性。
但是,这在使用 for
循环中的 if
语句的第二部分时有效。如何修复 where 子句?!
编辑
@JJJ的建议确实解决了我的问题,但我现在有一个相关的问题。我不能用变量替换表达式 where: "id","1"
中的常量 1
!我尝试了不起作用的普通 where 子句(带引号和不带引号)。所以,我尝试了一个 where 表达式,它也不起作用:
{% assign node = site.data.courses | where: "id",courseID %}
Doesn't work!
{% assign node = site.data.courses | where_exp: "selNode","selNode.id == courseID" %}
Neither does this.
我做错了什么,我该如何解决?
首先,与大多数编程语言一样,数组是零索引的。 node[1]
包含第二个节点,而不是第一个节点。您可能指的是 {% assign node = node[0] %}
。
其次,if node.id == empty
不是检查值是否存在的方式。只需 unless node
或 node.size == 0
.