我想你在很大程度上是自己想出来的,但我还是会用我自己的话解释一下。
你说得对,{{ content }} 是布局文件中实际页面内容所在的占位符。
您可能会感到困惑的是,您可以构建一组嵌套布局文件,其中一个“继承”另一个,每个布局都有自己的{{ content }}。
是的,我在文档中没有找到任何关于此的内容,我自己或通过查看示例找到了更好的方法。
下面是一个给你的例子。
一、默认布局和页面:
/_layouts/default.html:
<!DOCTYPE html>
<html>
<head>
<title>{{ page.title }}</title>
</head>
<body>
<h1>{{ page.title }}</h1>
{{ content }}
</body>
</html>
/index.md:
---
title: example page
layout: default
---
This is the page content.
生成的 HTML 将如下所示:
<!DOCTYPE html>
<html>
<head>
<title>example page</title>
</head>
<body>
<h1>example page</h1>
<p>This is the page content.</p>
</body>
</html>
现在让我们创建另一个“继承”第一个布局文件的布局文件。
如果您正在使用 Jekyll 构建博客,您可能会想要使用类似的东西。
上面显示的布局文件是所有页面、博客文章和常规页面的默认布局文件。
当您希望所有博客帖子都包含发布日期和用户、标签等附加信息时。
为此,您可以创建使用第一个布局文件的第二个布局文件:
/_layouts/post.html:
---
layout: default
---
<div class="blogpost">
<i>post date: {{ page.date }}</i>
{{ content }}
</div>
还有一篇使用这种布局的博文:
/_posts\2015-04-08-example-post.md:
---
title: example post
layout: post
---
This is the post content.
以及生成的 HTML:
<!DOCTYPE html>
<html>
<head>
<title>example post</title>
</head>
<body>
<h1>example post</h1>
<div class="blogpost">
<i>post date: 2015-04-08 00:00:00 +0200</i>
<p>This is the post content.</p>
</div>
</body>
</html>
换句话说,发生了这样的事情:
- Jekyll 使用
post 布局,将帖子内容放入{{ content }}
- Jekyll 使用
default 布局并将步骤1 中生成的完整HTML 放入{{ content }}
(不知道 Jekyll 是否真的按照这个顺序做事,但你明白了)
如Jekyll site首页的“快速入门说明”所示新建Jekyll项目,可以看另一个例子。
Jekyll (我的机器上的版本为 2.1.1) 创建的示例站点具有 三个 布局文件,其中两个(page 和 post)继承自默认一个。