【问题标题】:Can I display certain Jekyll Data File conditionally?我可以有条件地显示某些 Jekyll 数据文件吗?
【发布时间】:2015-09-08 18:19:58
【问题描述】:

我有一个使用这个 courses.yml 的课程列表

- title: Basic Residential HSI Install
  code: OSP0000111
  description: Install and Repair residential and business class POTS, HSI, and Video services
  newhire: true
  skillset: Copper
  jobtype: CST
  advanced: 

- title: PTA Testing Overview
  code: OSP0000114
  description: Teaches the technician how to close out a T1 test using the PTA system
  newhire: false
  skillset: Systems
  jobtype: CO
  advanced: no

- title: Technician Billing 
  code: OSP0000112
  description: Teaches the technician how to properly  bill the customer
  newhire: false
  skillset: Copper
  jobtype: CST
  advanced: yes

- title: Central Office Jumper
  code: OSP0000113
  description: Teaches the technician how to run jumpers
  newhire: true
  skillset: Copper
  jobtype: CO
  advanced: no

我列出的课程使用:

<ul>
{% for teds in site.data.courses %}
  <li>  Course Title: {{ teds.title }} <br />
        Course Code: {{ teds.code}} <br />
       Course Description: {{ teds.description }}
  </li>
{% endfor %}
</ul>

这很好用。但是,现在我想使用相同的数据文件创建一个列表,仅列出那些 newhire 变量为“true”的课程。

我之前没有理解任何解释如何执行此操作的文档。我尝试了 where 子句和过滤器无济于事。

有什么想法吗?

根据https://jekyllrb.com/docs/templates/ 网站,我尝试添加 where 子句:

<ul>
{% for new in site.data.courses | where "newhire","true" %}
  <li>  Course Title: {{ new.title }} <br />
        Course Code: {{ new.code }}  |  Job Type: {{ new.jobtype }} <br />
       Description: {{ new.description }} <br />
       <a href="{{ site.baseurl }}schedule.xlsx " target="_blank"> Schedule </a>
       <hr />
  </li>
{% endfor %}
</ul>

我刷新了页面,没有任何反应。我得到了相同的页面,所有课程的列表。没有过滤。我什至重新启动了 Jekyll 服务器。结果相同,没有过滤。

Jekyll 服务器也没有记录任何错误。

根据您的回答,我更改了数据文件变量。 2ea“0”,2ea“1”。然后我把html文件改成:

<ul>
{% assign teds = site.data.courses | where: 'newhire', 1 | sort: 'title' %}
{% for teds in courses %}
  <li>  Course Title: {{ teds.title }} <br />
        Course Code: {{ teds.code }} <br />
       Course Description: {{ teds.description }}
  </li>
{% endfor %}
</ul>

Jekyll 服务器没有报告任何错误,但是当我刷新页面时,它根本没有显示任何数据。我希望没有 4 个标题的列表,也没有 2 个喜欢的列表,只是网页。 :(

UGGH,我在晚餐后查看时看到了我的错误:)

以下是有效的:

<ul>
{% assign new = site.data.courses | where: 'newhire', 1 | sort: 'title' %}
{% for teds in new %}
  <li>  Course Title: {{ teds.title }} <br />
        Course Code: {{ teds.code }} <br />
       Course Description: {{ teds.description }}
  </li>
{% endfor %}
</ul>

【问题讨论】:

  • 请添加失败尝试的代码,并描述他们产生的意外结果或错误。
  • m69,感谢您抽出宝贵时间研究此问题! -乔

标签: jekyll


【解决方案1】:

UGGH,我在晚饭后查看时发现了我的错误 :) 头脑清醒有帮助!

正确的代码是:

<ul>
{% assign new = site.data.courses | where: 'newhire', 1 | sort: 'title' %}
{% for teds in new %}
  <li>  Course Title: {{ teds.title }} <br />
        Course Code: {{ teds.code }} <br />
       Course Description: {{ teds.description }}
  </li>
{% endfor %}
</ul>

非常感谢你,克里斯蒂安!!

乔伊

【讨论】:

    【解决方案2】:

    我可以向您展示来自 my blog 的工作示例,我正在做类似您想要实现的事情。
    认为我尝试使用 true/false一开始,但我没有让它工作,所以我改用0/1


    示例如下:

    我有a data file with projects。每个项目都有一个 sidebar 属性,它是 0 或 1:

    - name: Bitbucket Backup
      url: /bitbucket-backup/
      logo: /php/cache/img/bitbucket-backup-logo128x128.png
      desc: A backup tool which clones all your Bitbucket repositories to your local machine
      sidebar: 1
    

    在我的布局文件中,我有一个侧边栏,我只想用sidebar == 1 列出那些项目。 I'm doing this with the following code:

    {% assign projects = site.data.projects | where: 'sidebar', 1 | sort: 'name' %}
    {% for project in projects %}
    <li><a href="{{ project.url }}" rel="tooltip" title="{{ project.desc }}">{{ project.name }}</a></li>
    {% endfor %}
    

    请注意,仅当我将带有 where 子句的行分配给变量 (projects) 然后循环该变量时,它才对我有效。

    以下内容不起作用

    {% for project in site.data.projects | where: 'sidebar', 1 | sort: 'name' %}
    <li><a href="{{ project.url }}" rel="tooltip" title="{{ project.desc }}">{{ project.name }}</a></li>
    {% endfor %}
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-05-25
      • 1970-01-01
      • 2020-06-16
      • 2019-11-15
      • 1970-01-01
      • 2017-04-11
      • 2021-04-24
      • 1970-01-01
      相关资源
      最近更新 更多