添加自定義 HTML 標記

你可能希望包含自定義 HTML 標記,以擴展功能,這對於 HugoPress 內置鉤子和 HB 自定義鉤子來說小菜一碟。

讓我們從一個簡單的示例開始,該示例在頁面頂部顯示一條問候消息。

example

選擇正確的鉤子

本例中,body-begin 正是我們需要的鉤子。

配置

然後配置鉤子。

params.toml

1[hugopress]
2  [hugopress.modules]
3    [hugopress.modules.hb-custom]
4      [hugopress.modules.hb-custom.hooks]
5        [hugopress.modules.hb-custom.hooks.body-begin]
6          cacheable = true

params.yaml

1hugopress:
2  modules:
3    hb-custom:
4      hooks:
5        body-begin:
6          cacheable: true

params.json

 1{
 2   "hugopress": {
 3      "modules": {
 4         "hb-custom": {
 5            "hooks": {
 6               "body-begin": {
 7                  "cacheable": true
 8               }
 9            }
10         }
11      }
12   }
13}

如果一切正常,Hugo 將抱怨找不到模板:partial not found

cacheable
因爲示例 HTML 並不包含動態內容,將其標記爲 cacheable,以提升構建性能。

創建模板

緊接着創建模板以包含 HTML,模板名稱和模塊、鉤子名稱相關。

你可以通過 .Page 獲取頁面參數,若包含動態內容,請別忘記禁用 cacheable。 詳情請參閱 Hooks Context

就這樣,現在問候語將出現在頁面的頂部。