我這篇文章主要是以toml的立場去寫的,因為我自己就是用這個格式,然後把一些坑記錄下來
這個主題談論您想要用front matter做什麼事情?
當您在 single.html 或 list.html 時,您想讓front matter怎麼幫你
首先,我們要知道,你在這些頁面,都可以有辦法獲取這三個front matter
[params]
show_reading_time = true # 這個只是舉例
...
寫在config.toml中的params,
當您切換到該頁面的時候,這些front matter等於也直接寫在該頁面front matter
也就是頁面下的.Param
可以抓到的變數有
config.toml.params
而如果您只想要取得config.toml.params中的而已,要用:
.Site.
Param “xxx”
語法 | 包含 |
---|---|
.Site.Param | ● 只有config.toml.params |
.Param "xxx" | ● config.toml.params ● 自己的front matter |
寫在config.toml來控制的東西就是這些控制是比較全面的,
你不想要在每一個頁面都決定,想要統一管理的時候可以考慮加在這邊
不是那麼常態需要決定,很常要更動的項目可以考慮加在這邊
您在頁面下,如果要取得該頁面是位於哪一個section
可以用
“0.45 以前的做法” {{- $section := .Site.GetPage "section" .Section -}}
現在的做法:
{{- $section := .Site.GetPage .Section -}}
當您希望某一個Section下都套用此設定時,就加在這邊。
好處是該section底下都會套用,又不像config中的那麼霸道,全域套用。
🔔: section是跟目錄下第一層的內容,裡頭再多的子資料夾的section也都是看第一層的而已
最後用一個範例來結束
1: 取得該頁面的section
9: 由於.Param包含了:
也就是能當作 1) 和 3) 條件的判斷
至於2) 就利用
$section.Param
即可完成
本例子用來判別是否顯示「字數」和「閱讀時間」
1{{- $section := .Site.GetPage .Section -}}
2
3{{/*
4 Show "reading time" and "word count" but only if one of the following are true:
5 1) A global config `params` value is set `show_reading_time = true`
6 2) A section front matter value is set `show_reading_time = true`
7 3) A page front matter value is set `show_reading_time = true`
8*/}}
9{{ if (or (eq (.Param "show_reading_time") true) (eq $section.Params.show_reading_time true) )}}
10 <span class="f6 mv4 dib tracked"> - 🕒 {{ .ReadingTime}} minutes read</span>
11 <span class="f6 mv4 dib tracked"> - {{ .WordCount}} words</span>
12{{ end }}
Name | 說明 |
---|---|
weight | 數值越低優先權越高。 ❗ 當您設置為0表示不使用weight,不代表它比1的優先權高 |
TranslationBaseName | title = "{{ title (replace .TranslationBaseName "-" " ") }}" 可以忽略掉檔案的附檔名 title:可以讓首字母大寫其他小寫 |
一種是利用shortcodes中本來就可以提供給HTML的變數,請參考: Access Parameters
我這邊還是舉一些簡單的例子:
$.Page.Params
: 這個是只當前的Page,即便shortcodes沒有額外的參數去描述當前的Page,透過這個變數也能取得到當前Page$.Site.
: 類似一般在HTML檔案中所使用的.Site
所以可以用
{{ if .Params.xxx }} {{end}}
❗ 這個的缺點是,當您的屬性有
-
或者是一些另類的字元就沒辦法直接這樣存取
{{ if
.Params “xxx”}} 這種是錯誤的語法,他不支援這種方式
第二種方法是用Functions
❗
.Param
(沒有s)
他的語法:
.Param KEY
: Calls page or site variables into your template.
所以您可以這樣用
{{ if .Param “xxx” }} {{ end }}
小結:
Name | Syntax |
---|---|
.Param | .Param “KEY” |
.Params | .Params.KEY |
如果您要使用dict,請把dict分一「塊」,一般變數分一「塊」。
例如:
1title="..."
2description = "..."
3[my_dict_1]
4 item = "..."
5[my_dict_2]
6 item = "..."
1title="..."
2[my_dict_1]
3 item = "..."
4description = "..." # 這個元素之前有一個[my_dict_1]object(dict),所以這個元素會沒有作用
5[my_dict_2]
6 item = "..."
7description2 = "..."
description和description2都將無法顯示
實際上toml並沒有縮排的概念!
也就是
[my_dict_1]
item = "..."
description = "..."
完全等價於
[my_dict_1]
item = "..."
description = "..."
並非我們預期的
description = "..."
[my_dict_1]
item = "..."
假設您有一個朝巢狀的dict,如下:
[next_and_previous]
[next_and_previous.link]
previous_page=""
next_page=""
如果您只要設定其中一項,其實可以直接抓取該結點即可,如:
[next_and_previous.link]
previous_page=""
previous_section=""