逐步教學
8. 部落格
你可能會好奇,在沒有資料庫的情況下,你怎麼能擁有部落格。以真正的 Jekyll 風格來說,部落格僅由文字檔案提供支援。
文章
部落格文章存在於名為 _posts
的資料夾中。文章的檔案名稱有特殊格式:發佈日期、標題,然後是副檔名。
在 _posts/2018-08-20-bananas.md
建立你的第一篇文章,內容如下
---
layout: post
author: jill
---
A banana is an edible fruit – botanically a berry – produced by several
kinds of large herbaceous flowering plants in the genus Musa.
In some countries, bananas used for cooking may be called "plantains",
distinguishing them from dessert bananas. The fruit is variable in size,
color, and firmness, but is usually elongated and curved, with soft
flesh rich in starch covered with a rind, which may be green, yellow,
red, purple, or brown when ripe.
這就像你之前建立的 about.md
,只不過它有作者和不同的版面配置。 author
是自訂變數,它不是必要的,而且可以命名為 creator
之類的名稱。
版面配置
post
版面配置不存在,因此你需要在 _layouts/post.html
建立它,內容如下
---
layout: default
---
<h1>{{ page.title }}</h1>
<p>{{ page.date | date_to_string }} - {{ page.author }}</p>
{{ content }}
這是版面配置繼承的範例。文章版面配置會輸出標題、日期、作者和內容主體,這些內容會由預設版面配置包覆。
另請注意 date_to_string
篩選器,它會將日期格式化為較佳的格式。
列出文章
目前沒有辦法導覽至部落格文章。一般來說,部落格會有一個列出所有文章的頁面,我們接下來就來做這件事。
Jekyll 會在 site.posts
提供文章。
在根目錄中建立 blog.html
(/blog.html
),並輸入以下內容
---
layout: default
title: Blog
---
<h1>Latest Posts</h1>
<ul>
{% for post in site.posts %}
<li>
<h2><a href="{{ post.url }}">{{ post.title }}</a></h2>
{{ post.excerpt }}
</li>
{% endfor %}
</ul>
這段程式碼有幾點需要注意
post.url
會由 Jekyll 自動設定為文章的輸出路徑post.title
會從文章檔名取得,並可以在 front matter 中設定title
來覆寫post.excerpt
預設為內容的第一段
您也需要一個方式透過主要導覽來導覽至這個頁面。開啟 _data/navigation.yml
並為部落格頁面新增一個項目
- name: Home
link: /
- name: About
link: /about.html
- name: Blog
link: /blog.html
更多文章
部落格只有一篇文章並不好玩。新增幾篇吧
_posts/2018-08-21-apples.md
:
---
layout: post
author: jill
---
An apple is a sweet, edible fruit produced by an apple tree.
Apple trees are cultivated worldwide, and are the most widely grown
species in the genus Malus. The tree originated in Central Asia, where
its wild ancestor, Malus sieversii, is still found today. Apples have
been grown for thousands of years in Asia and Europe, and were brought
to North America by European colonists.
_posts/2018-08-22-kiwifruit.md
:
---
layout: post
author: ted
---
Kiwifruit (often abbreviated as kiwi), or Chinese gooseberry is the
edible berry of several species of woody vines in the genus Actinidia.
The most common cultivar group of kiwifruit is oval, about the size of
a large hen's egg (5–8 cm (2.0–3.1 in) in length and 4.5–5.5 cm
(1.8–2.2 in) in diameter). It has a fibrous, dull greenish-brown skin
and bright green or golden flesh with rows of tiny, black, edible
seeds. The fruit has a soft texture, with a sweet and unique flavor.
開啟 https://#:4000,瀏覽您的部落格文章。
接下來,我們會專注於為每個文章作者建立一個頁面。