概念
模板 Template : 指的是 web 中一种生成html页面的技术,用于定义页面如何渲染展示。hugo 会基于 layout/ 中定义的 Template 将 content/ 中的内容渲染成 web 界面。
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
{{ partial "header" (dict "Kind" .Kind "Template" "Single") }}
<div style="margin:25px;">
<h1>{{.Title}}</h1>
<div style="color:grey; font-size:16px;">{{ dateFormat "Monday, Jan 2, 2006" .Date }}</div>
<div style="color:grey; font-size:16px;">{{if .Params.author}}Author: {{.Params.Author}}{{end}}</div>
<div style="font-size:18px;">{{.Content}}</div>
</div>
{{ partial "footer" . }}
</body>
</html><html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
{{ partial "header" (dict "Kind" .Kind "Template" "Single") }}
<div style="margin:25px;">
<h1>{{.Title}}</h1>
<div style="color:grey; font-size:16px;">{{ dateFormat "Monday, Jan 2, 2006" .Date }}</div>
<div style="color:grey; font-size:16px;">{{if .Params.author}}Author: {{.Params.Author}}{{end}}</div>
<div style="font-size:18px;">{{.Content}}</div>
</div>
{{ partial "footer" . }}
</body>
</html>
安装 themes
,核心就是为了使用别人已经配置好的 Template 。
Note
当 layouts/ 与 themes/layouts/ 下的模板路径重名时,hugo 优先采用 layouts/ 中的模板
类型
Hugo 通过 Template 来实现网页的生成,因此,只要知道有哪些类型的 Template 就能明确 Hugo 支持生成哪些界面
- 主模板
home: 主界面page: 文章正文section: 展示section中文章条目Taxonomy: 展示term选项Term: 展示与term关联的文章条目
- 备用模板: 当主模板类型缺失时,就会使用备用模板
Single:page的替补list:home、secton、taxonomy、term的替补all:home、secton、taxonomy、term、page的替补
- 功能模板
base: 其他的模板类型的父类Partial: 自定义组件(类似封装好的函数),可给其他模板使用View: 自定义组件(类似宏),可给其他模板使用Render hook:hook从Markdown到HTML转换过程Shortcode: 自定义组件, 直接给Markdown使用
模板匹配
默认规则
hugo 默认会按照以下命名自动将 content 文档与 layout 模板进行自动匹配
layouts/
├── _markup/
│ ├── render-image.html <-- render hook
│ └── render-link.html <-- render hook
├── _partials/
│ ├── footer.html
│ └── header.html
├── _shortcodes/
│ ├── audio.html
│ └── video.html
├── books/ <-- 对特定 `content type` 的模板定制
│ ├── page.html
│ └── section.html
├── films/ <-- 对特定 `content type` 的模板定制
│ ├── _views/
│ │ └── card.html <-- view template
│ ├── page.html
│ └── section.html
├── baseof.html <-- base template
├── home.html
├── page.html
├── section.html
├── taxonomy.html
└── term.htmllayouts/
├── _markup/
│ ├── render-image.html <-- render hook
│ └── render-link.html <-- render hook
├── _partials/
│ ├── footer.html
│ └── header.html
├── _shortcodes/
│ ├── audio.html
│ └── video.html
├── books/ <-- 对特定 `content type` 的模板定制
│ ├── page.html
│ └── section.html
├── films/ <-- 对特定 `content type` 的模板定制
│ ├── _views/
│ │ └── card.html <-- view template
│ ├── page.html
│ └── section.html
├── baseof.html <-- base template
├── home.html
├── page.html
├── section.html
├── taxonomy.html
└── term.html
front matter
content 中的文档也可以显示指定该文档应当匹配的模板
my-project/
├── layouts/
│ └── miscellaneous/
│ └── contact.html
│ └── single.html
└── content/
├── about.md
└── contact.mdmy-project/
├── layouts/
│ └── miscellaneous/
│ └── contact.html
│ └── single.html
└── content/
├── about.md
└── contact.md
-
about.md的配置TOMLtitle = 'About' type = 'miscellaneous' # 指定 content type 为 miscellaneous ,由于没有 page.html,因此会使用 single.htmltitle = 'About' type = 'miscellaneous' # 指定 content type 为 miscellaneous ,由于没有 page.html,因此会使用 single.html -
contact.md的配置TOMLtitle = 'Contact' type = 'miscellaneous' # 指定 content type 为 miscellaneous layout = 'contact' # 使用 contact.htmltitle = 'Contact' type = 'miscellaneous' # 指定 content type 为 miscellaneous layout = 'contact' # 使用 contact.html