【问题标题】:Jekyll if statement in Front Matter在 Front Matter 中的 Jekyll if 语句
【发布时间】:2018-06-25 01:49:22
【问题描述】:

我想知道是否可以在 Front Matter 中添加 if 语句,以便根据 page.url 选择性地使用属性

页面是使用基本模板从插件虚拟生成的 .md 文件,因此如果可能,我需要在 Front Matter 中添加一些条件。

这是一个典型的 Front Matter

---
...
title: blah
description: blah blah

image: some-image.png
---

我想做以下事情:

---
...
title: blah
description: blah blah

{% if page.url == "page1" %}

image: page1-image.png

{% else %}

image: general-image.png   

{% endif %}
---

【问题讨论】:

    标签: jekyll liquid yaml-front-matter


    【解决方案1】:

    Jekyll 没有开箱即用地解析 Front Matter 中的 Liquid,但有一些插件可以为您解析 Front Matter 中的 Liquid。

    也就是说,我认为您的用例可以在页面的“布局”本身内轻松解决..

    {% capture image %}
      {% if page.url == "page1" %}
        page1-image.png
      {% else %}
        general-image.png
      {% endif %}
    {% endcapture %}
    
    {{ image | strip }}
    

    【讨论】:

    • 谢谢。 capture 是一个内置的 Jekyll 命令,它可以从页面布局向 Front Matter 注入价值吗? image | strip 是做什么的?
    • capture 是一个块类型的原生 Liquid 标签,允许您将评估 Liquid 返回的字符串值存储在标签正文中。根据您缩进封闭的 Liquid 构造的方式,该字符串将包含空格。然后可以通过 strip 过滤器删除此空格。
    猜你喜欢
    • 1970-01-01
    • 2015-05-12
    • 2015-08-17
    • 2019-01-27
    • 1970-01-01
    • 2012-04-30
    • 2014-06-04
    • 2015-11-23
    • 1970-01-01
    相关资源
    最近更新 更多