不能访问自定义的前项变量

时间:2019-10-22 20:31:38

标签: html jekyll liquid kramdown yaml-front-matter

正在创建我的第一个Jekyll(使用4.0.0版)供电的网站。问题是无法识别“前题”中的变量。

_includes中的HTML(writing-post-featured-image.html)

Ctrl+Shift+V

在_layout中具有基于文本的帖子页面的布局(writings-post.html)

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

最后,在.md文件中(在_posts下)以下前沿问题

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

输出为空

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

请帮助理解为什么。预先感谢。

2 个答案:

答案 0 :(得分:2)

帖子页面上的正确语法为:

{% 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 :(得分:2)

您的语法不正确。

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

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

2。)。在您的收录中,您遇到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>