Variables 如何在Jekyll中的干模板和包含之间传递变量

Variables 如何在Jekyll中的干模板和包含之间传递变量,variables,jekyll,dry,liquid,Variables,Jekyll,Dry,Liquid,我有一个简单的博客站点,其中首页索引是一个帖子列表,没有截断,呈现方式与单个页面完全相同 我已设置索引页并正在运行: --- layout: default --- <div> {% for post in site.posts %} {% include post/post.html %} {% endfor %} </div> 现在,在一个特定的帖子页面上,我想重用这个include布局,这样我就可以让我的代码保持干燥,所以我让帖子使用post.h

我有一个简单的博客站点,其中首页索引是一个帖子列表,没有截断,呈现方式与单个页面完全相同

我已设置索引页并正在运行:

---
layout: default
---

<div>
  {% for post in site.posts %}
    {% include post/post.html %}
  {% endfor %}
</div>
现在,在一个特定的帖子页面上,我想重用这个include布局,这样我就可以让我的代码保持干燥,所以我让帖子使用
post.html
布局(与上面的
posts/post.html
不同):

但问题是,include文件要求存在一个
post
变量,例如
post.content
是访问内容的方式

我试过:

{% assign post = page %}
这似乎是传递变量的正确方法,但页面不是正确的,因为它呈现为标记字符串,而不是页面上的html


那么,我如何传递
self
——或者任何需要的信息——以便不需要为索引页或帖子页修改include文件,从而共享相同的代码呢?

我们可以做到!通过一些布局和变量传递

定期发布使用
post
布局:

---
layout: post
....
---
post content ...
自定义帖子使用
custom\u post
布局(
\u layouts/custom\u post.hmtl

\u layouts/custom\u post.hmtl
布局只调用我们的post include,传递
页面变量

---
layout: default
---
{% include post/post.html page=page %}
最后,
\u includes/post/post.html
如果设置了
页面
变量,则有条件地分配
post
变量:

{% if include.page %}
    {% comment %}
       This only append in the custom_post view context
    {% endcomment %}
    {% assign post = include.page %}
{% endif %}

<article>
    <header>
    {% include post/title.html %}
    {% include post/metadata.html %}
  </header>

  <div class="entry-content" itemprop="text">
    {{ post.content }}
  </div>
</article>
{%if include.page%}
{%comment%}
这仅附加在自定义\u post视图上下文中
{%endcomment%}
{%assign post=include.page%}
{%endif%}
{%include post/title.html%}
{%include post/metadata.html%}
{{post.content}}

当@David Jacquel的答案工作时,我发现它不干净而且有点冗长,尽管它确实让我走上了正确的轨道

\u includes/post/post.html
{{body}
交换
{{post.content}

<article>
    <header>
    {% include post/title.html %}
    {% include post/metadata.html %}
  </header>

  <div class="entry-content" itemprop="text">
    {{ body }}
  </div>
</article>
\u layouts/post.html
使用:

{% assign post = page %}
{% assign body = content %}
{% include post/post.html %}

简单。

\u layouts/default.html
index.html
?您正在修改
\u layouts/post.html
,但您说只有特定的帖子需要自定义版面。我想我并不是说只有特定的帖子需要自定义版面。我说“呈现得和单独的页面一模一样”,我想让它保持干燥。这就实现了这一点。谢谢你让我走上正轨,我对你的答案投了赞成票。
{% if include.page %}
    {% comment %}
       This only append in the custom_post view context
    {% endcomment %}
    {% assign post = include.page %}
{% endif %}

<article>
    <header>
    {% include post/title.html %}
    {% include post/metadata.html %}
  </header>

  <div class="entry-content" itemprop="text">
    {{ post.content }}
  </div>
</article>
<article>
    <header>
    {% include post/title.html %}
    {% include post/metadata.html %}
  </header>

  <div class="entry-content" itemprop="text">
    {{ body }}
  </div>
</article>
{% assign body = post.content %}
{% include post/post.html %}
{% assign post = page %}
{% assign body = content %}
{% include post/post.html %}