· HugoWind 团队 · 技术 · 1 min read · 61 words

Hugo 模板系统详解

掌握 Hugo 的 Go 模板语法和最佳实践

Hugo 模板基础

Hugo 使用 Go 的 text/templatehtml/template 库作为模板引擎。

基本语法

{{ .Title }}
{{ .Content }}
{{ range .Pages }}
  <h2>{{ .Title }}</h2>
{{ end }}

模板类型

  1. baseof.html - 基础模板
  2. list.html - 列表页模板
  3. single.html - 单页模板
  4. partials/ - 可复用组件

条件语句

{{ if .Params.featured }}
  <span class="badge">Featured</span>
{{ end }}

循环

{{ range .Site.RegularPages }}
  <article>{{ .Title }}</article>
{{ end }}