无法访问自定义 Front Matter 变量

Cant access custom Front Matter variables

正在创建我的第一个 Jekyll(使用 4.0.0 版)支持的站点。问题是无法识别 Front Matter 中的变量。

HTML in _includes (writing-post-featured-image.html)

<figure>
    <img class="feat-img" src="{{ site.baseurl }}/assets/images/{{ include.images }}" alt="{{ include.alt | default: 'lorem ipsum' }}" />
    <figcaption>{{ include.caption }}</figcaption>
</figure>

在 _layout 中具有基于文本的布局 post 页(写作-post.html)

{% include writing-post-featured-image.html image=post.featured-image alt=post.featured-image-alt %}

最后,在 .md 文件中(_posts 下)以下 Front Matter

layout: writings-post
title: my title
permalink: /writings/:title
featured-image: photo.jpg
featured-image-alt: about photo
caption: photo caption

输出为空

<figure>
    <img class="feat-img" src="" alt="lorem ipsum" />
    <figcaption></figcaption>
</figure>

请帮助理解为什么会这样。提前致谢。

post 页面上的正确语法是:

{% include writing-post-featured-image.html image=page.featured-image alt=page.featured-image-alt %}

请注意 page. 语法,而不是 post. 语法。然而,当你的布局中有一个循环时,你可以使用这个:

{% for post in site.posts %}
  {% include writing-post-featured-image.html image=post.featured-image alt=post.featured-image-alt %}
{% endfor %}

您的语法不正确。

1.) 作为您从页面传递的变量,您的包含标记应如下所示:

 {% include writing-post-featured-image.html 
    image=page.featured-image 
    alt=page.featured-image-alt 
    caption=page.caption %}

2.) 在你的 include 中你有一个语法问题 include.images 应该是 include.image.

注意 :当您传递现有变量(不是计算的变量)时,您可以跳过将它们传递给您的包含,因为从包含中您可以看到页面的变量。

 {% include writing-post-featured-image.html %}

你的包括:

<figure>
    <img class="feat-img" 
         src="{{ site.baseurl }}/assets/images/{{ page.featured-image }}" 
         alt="{{ page.featured-image | default: 'lorem ipsum' }}" />
    <figcaption>{{ page.caption }}</figcaption>
</figure>