【问题标题】:Why does this where filter not work in Jekyll?为什么这 where 过滤器在 Jekyll 中不起作用?
【发布时间】:2018-01-21 07:24:17
【问题描述】:

我有一个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!

输出如下:

找到数据!
编号:
课程编号:
名称:
链接:
RHCSA
/rhcsa

所以,node 变量显然不是空的,但是当我使用 where 子句选择数组的正确元素时,我无法访问任何属性。

但是,当在 for 循环中使用 if 语句的第二部分时,这有效。如何修复 where 子句?!


编辑

@JJJ 的建议确实解决了我的问题,但我现在有一个相关的问题。我不能用变量替换表达式where: "id","1" 中的常量1!我尝试了不起作用的正常 where 子句(带引号和不带引号)。所以,我尝试了一个 where 表达式,它也不起作用:

{% assign node = site.data.courses | where: "id",courseID %}

没用!

{% assign node = site.data.courses | where_exp: "selNode","selNode.id == courseID" %}

这个也不行。

我做错了什么,我该如何解决?

【问题讨论】:

  • 请在不同的问题中发布不同的问题。

标签: jekyll liquid github-pages


【解决方案1】:

首先,与大多数编程语言一样,数组是零索引的。 node[1] 包含第二个节点,而不是第一个。你可能指的是{% assign node = node[0] %}

其次,if node.id == empty 不是您检查值是否存在的方式。只需执行unless nodenode.size == 0

【讨论】:

  • 那么如何将变量评估为 empty 呢?在什么情况下?
  • 请查看我对问题的编辑(在底部)。我尝试了您的解决方案,但似乎无法适应我的问题!
  • 那是一个完全不同的问题。请将其作为单独的问题发布。
猜你喜欢
  • 2022-01-24
  • 1970-01-01
  • 1970-01-01
  • 2021-10-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多