Jekyll如何使用post.html生成页面? [英] How does Jekyll use post.html to generate pages?

查看:268
本文介绍了Jekyll如何使用post.html生成页面?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很难让Jekyll使用特定主题,而且我认为{{ content }}如何与帖子配合使用时,我缺少一些基本知识.

I'm having some difficulty getting Jekyll to use a particular theme and I think there's something fundamental I'm missing about how {{ content }} works with posts.

因此,在通用的Jekyll网站中,index.html在其前件中指定了布局.生成站点时,布局将index.html作为{{ content }}.这是一种倒置,页面由页面指定布局,然后布局调用页面,但很简单.

So, in a generic Jekyll site, index.html has a layout specified in its front matter. When the site is generated, the layout includes index.html as {{ content }}. It's kind of inverted, where the page specifies the layout and then the layout calls the page, but simple enough.

帖子都是通过文件post.html生成的,该文件位于_layouts文件夹中,即使它实际上不是布局.像index.html一样,它实际上只是一个for循环.这就是我遇到麻烦的地方.

Posts, on the other hand, are all generated via a file, post.html, which resides in the _layouts folder even though it isn't really a layout. Like index.html it's essentially just a for loop. This is where I'm running into trouble.

post.html是必需文件吗?我可以重命名为story.html吗?

Is post.html a required file? Could I rename it story.html?

为什么post.html在前面需要布局?实际的帖子,即包含该帖子的文本的降价,也需要在其前母版中进行布局.是否存在post.html的布局与markdown文件中指定的布局不同的情况?

Why does post.html require a layout in the front matter? The actual post, that is, the markdown that contains the text for said post, also requires a layout in its front mater. Is there a circumstance where post.html would have a different layout than the layout specified in the markdown file?

另一个问题.为什么在多个地方调用{{ content }}? index.html和布局文件都具有{{content}}.为什么布局不只是{%包含%} index.html并让index.html调用{{content}}

one other question. Why is {{ content }} called in multiple places? index.html and the layout file both have {{ content }}. Why doesn't the layout simply {% include %} index.html and let index.html call {{ content }}

推荐答案

我认为您基本上是自己弄清楚的,但是我仍然会用自己的话来解释.

I think you largely figured it out by yourself, but I'll still explain it in my own words.

您说对了,{{ content }}是布局文件在实际页面内容所处的占位符.

You're right that {{ content }} is the placeholder in the layout file where the content of the actual page will go.

您可能会感到困惑的事实是,您可以构建一组嵌套的布局文件,其中一个相互继承,每个布局都有自己的{{ content }}.
是的,我在文档中没有发现任何有关此问题的信息,我是通过查看示例自己弄清楚的,或者更好的找到了它.

What probably confused you is the fact that you can build a set of nested layout files where one "inherits" from the other, and each layout has its own {{ content }}.
And yes, I didn't find anything about this in the docs, I figured it out by myself or better, by looking at examples.

这是给你的一个例子.
首先,使用默认布局和页面:

So here's an example for you.
First, a default layout and a page:

<!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将如下所示:

The generated HTML will look like this:

<!DOCTYPE html>
<html>
<head>
    <title>example page</title>
</head>
<body>
<h1>example page</h1>

<p>This is the page content.</p>

</body>
</html>

现在,让我们创建另一个继承自第一个布局文件.
如果您要通过Jekyll建立博客,则可能需要使用类似的内容.
上面显示的布局文件是所有页面,博客文章和常规页面的默认文件.
如果您希望所有博客帖子都包含其他信息,例如发布日期和用户,标签等.

Now let's create another layout file that "inherits" from the first one.
You'll probably want to use something like this if you're building a blog with Jekyll.
The layout file shown above is the default for all pages, blog posts and regular ones.
When you want all blog posts to contain additional information like post date and user, tags and so on.

为此,您可以创建使用第一个布局文件的第二个布局文件:

For this, you can create a second layout file which uses the first one:

---
layout: default
---

<div class="blogpost">
    <i>post date: {{ page.date }}</i>

    {{ content }}
</div>

以及使用此布局的博客文章:

And a blog post which uses this layout:

---
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>

换句话说,发生了这样的事情:

In other words, something like this happened:

  1. Jekyll使用post布局并将帖子内容放入{{ content }}
  2. Jekyll使用default布局,并将步骤1中生成的完整HTML放入{{ content }}
  1. Jekyll used the post layout and put the content of the post into {{ content }}
  2. Jekyll used the default layout and put the complete generated HTML from step 1 into {{ content }}

(不知道Jekyll是否真的在幕后按照这个顺序做事,但是你明白了)

如果您按照 Jekyll网站.
Jekyll (我的计算机上的版本2.1.1)创建的示例站点具有三个布局文件,其中两个文件(pagepost)继承自默认文件一个.

You can see another example if you create a new Jekyll project as shown in the "Quick-start Instructions" on the home page of the Jekyll site.
The example site that Jekyll (version 2.1.1 on my machine) creates has three layout files, two of which (page and post) inherit from the default one.

这篇关于Jekyll如何使用post.html生成页面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆