【问题标题】:Jekyll: Using Front Matter in IncludesJekyll:在 Includes 中使用 Front Matter
【发布时间】:2015-06-04 10:38:58
【问题描述】:

是否可以在包含中使用前面的内容。我有一个名为 image.html 的包含,看起来像这样。

image.html

{% if layout == 'page' %}
  <img src="{{ site.url }}/public/img/{{ include.url }}" class="page">
{% else %}
  <img src="{{ site.url }}/public/img/{{ include.url }}">
{% endif %}

还有一个看起来像这样的about.md 文件:

about.md

---
layout: page
---

{% include image.html url="img.jpg" %}

但由于某种原因,它无法识别 if layout 语句。知道如何让它工作吗?

感谢任何帮助。提前致谢!

更新

完整问题如下:

如果页面中包含image.html 包含,则包含需要具有完全不同的布局,包含两个封装元素(containersm-col-12)。希望下面的内容比我原来的问题更有意义。

image.html

{% if include.size == 'full' and include.type != 'gif' %}
<section class="image full-width">
  <img src="{{ site.url }}/public/img/{{ include.name }}.{{ include.type }}">
</section>
{% elsif layout == 'page' and include.type != 'gif' %}
<div class="container">
  <div class="sm-col-12">
    <section class="image">
      <img src="{{ site.url }}/public/img/{{ include.name }}.{{ include.type }}">
    </section>
  </div>
</div>
{% elsif include.type != 'gif' %}
...
{% else %}
...
{% endif %}

about.md

---
layout: page
---

{% include image.html name="zifferblatt" type="jpg" %}

【问题讨论】:

    标签: if-statement jekyll liquid


    【解决方案1】:

    从包含文件中,您仍然可以使用 page 变量。

    在你的情况下,你打电话给page.layout

    <img src="{{ site.url }}/public/img/{{ include.url }}" {% if page.layout == 'page' %}class="page"{% endif %}>
    

    【讨论】:

    • 对不起。我发布的示例是我所面临问题的精简版。我实际上无法在.md 文件中使用`page` 变量,而是在包含文件中需要它。你知道这是否可能吗?
    • 你能给出完整的上下文吗?您是否处于循环中,列出帖子或页面?
    • 在原始问题旁边发布了更新。我希望这能提供更多背景信息...
    • 相同的答案 {% elsif page.layout == 'page'... 因为从 image.html 内部,您仍然在 about.md 页面上下文中。所以你仍然可以从page.layout 变量到达关于页面布局。
    • 啊啊啊啊!我在page.layout 中缺少page。谢谢你,对疏忽感到抱歉。
    猜你喜欢
    • 2018-06-25
    • 2019-01-27
    • 2015-11-23
    • 1970-01-01
    • 2015-08-17
    • 2015-05-12
    • 1970-01-01
    • 2014-06-04
    • 2012-04-30
    相关资源
    最近更新 更多