con*_*are 2 variables dry liquid jekyll
我有一个简单的博客网站,其中首页索引是一个帖子列表,非截断和呈现与各个页面完全相同.
我有索引页面设置和工作:
---
layout: default
---
<div>
{% for post in site.posts %}
{% include post/post.html %}
{% endfor %}
</div>
Run Code Online (Sandbox Code Playgroud)
其中post/post.html包含使用post变量的帖子布局,如下所示:
<article>
<header>
{% include post/title.html %}
{% include post/metadata.html %}
</header>
<div class="entry-content" itemprop="text">
{{ post.content }}
</div>
</article>
Run Code Online (Sandbox Code Playgroud)
现在,在特定的帖子页面上,我想重用这个包含布局,以便我可以保持我的代码DRY,所以我有帖子使用post.html布局(与posts/post.html上面不同):
---
layout: default
comments: true
---
{% include post/post.html %}
Run Code Online (Sandbox Code Playgroud)
但问题是包含文件需要post存在变量,例如post.content您访问内容的方式.
我试过了:
{% assign post = page %}
Run Code Online (Sandbox Code Playgroud)
这似乎是传递变量的权利,但是页面不是正确的,因为它呈现为降价字符串而不是页面上的html.
那么,我如何传递self- 或者需要什么 - 以便不需要为索引页面或帖子页面更改包含文件,从而共享相同的代码?
虽然@David Jacquel的回答工作,我发现它是不洁净的,有点冗长,虽然它确实让我走上正轨.
_includes/post/post.html在交换{{ post.content }}为{{ body }}
<article>
<header>
{% include post/title.html %}
{% include post/metadata.html %}
</header>
<div class="entry-content" itemprop="text">
{{ body }}
</div>
</article>
Run Code Online (Sandbox Code Playgroud)
_layouts/index.html 现在用
{% assign body = post.content %}
{% include post/post.html %}
Run Code Online (Sandbox Code Playgroud)
_layouts/post.html 用途:
{% assign post = page %}
{% assign body = content %}
{% include post/post.html %}
Run Code Online (Sandbox Code Playgroud)
简单.