【问题标题】:Jekyll: Page not parsing custom variable from layoutJekyll:页面未从布局中解析自定义变量
【发布时间】:2017-11-06 12:05:56
【问题描述】:

我刚开始学习 Jekyll,但遇到了一点小障碍。

我的 _layout 文件夹中有 2 个布局,基本上如下所示:-

default.html

<html>
<head> <!-- Meta Tags etc --> </head>
<body>
    {{ content }}
</body>
</html>

hero.html

---
layout: default
---
<section id="hero">
    <h3>{{ hero.descr }}</h3>
</section>
{{ content }}

我的索引页面扩展英雄布局如下:-

index.html

---
layout: hero
descr: Hello there.
---

布局工作得很好,网站显示正常,除了 hero.descr 变量。标题标签是空的。

【问题讨论】:

    标签: html jekyll liquid


    【解决方案1】:

    数据的流动是单向的。 default.html => hero.html => index.html

    hero.html 将不知道index.html 中定义的内容

    但是,渲染方向相反(插入到父级的{{ content }} 变量中。

    index.html ==> hero.html ==> default.html

    【讨论】:

    • 不是反过来吗? hero.html 只是带有占位符变量的布局,该变量由 index.html 页面的前端填充。
    • 在这种情况下,几乎是真的..占位符变量是{{ content }}。其他一切都是从父级继承
    • 啊,我明白了,这更有意义。谢谢。
    【解决方案2】:

    在我看来变量{{ hero.descr }} 不存在。我觉得应该是{{ page.descr }}

    【讨论】:

    • 感谢您的回复。在阅读了您和 ashmaroli 的答案后,我回到文档并彻底研究了它们,并意识到我对 Jekyll 变量和布局如何工作的基本理解是不正确的。可能应该先熟悉 Liquid。无论如何,是的,您是绝对正确的,{{ page.descr }} 是为页面声明自定义变量的正确方法。 :)
    猜你喜欢
    • 2017-05-30
    • 1970-01-01
    • 2012-02-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-12
    • 2019-12-22
    • 1970-01-01
    相关资源
    最近更新 更多