HUGO ja 非公式

  • ニュース
  • ドキュメント
  • テーマ
  • コミュニティ
  • GitHub
gohugoio Star づけ
  暫定公開中 2024/09/16 (94d7f576a 対応, 2024/09/15)
  • Hugo について
    • 本節
    • Introduction
    • Hugo の機能
    • Privacy
    • セキュリティ
    • ライセンス
  • インストール
    • 本節
    • macOS
    • Linux
    • Windows
    • BSD
  • はじめよう
    • 本節
    • クイックスタート
    • ディレクトリ構造
    • 基本操作
    • 設定
    • Configure markup
    • 用語集
    • 本書以外の学習リソース
  • クイックリファレンス
    • 本節
    • Emojis
    • Functions
    • Methods
    • Page collections
  • コンテント管理
    • 本節
    • コンテントの構成
    • ページバンドル
    • コンテントフォーマット
    • フロントマター
    • ビルドオプション
    • ページリソース
    • イメージ処理
    • ショートコード
    • 関連コンテント
    • Sections
    • Content types
    • アーキタイプ
    • 分類
    • Summaries
    • Links and cross references
    • URL 管理
    • メニュー
    • コメント
    • マルチ言語
    • Markdown attributes
    • シンタックスハイライト
    • Diagrams
    • Mathematics
    • Data sources
    • Content adapters
  • テンプレート
    • 本節
    • はじめに
    • Template types
    • Lookup order
    • 基本テンプレート
    • Home templates
    • Single templates
    • Section templates
    • Taxonomy templates
    • Term templates
    • 部分テンプレート
    • コンテントビューテンプレート
    • ショートコードテンプレート
    • サイトマップテンプレート
    • RSS テンプレート
    • 404 テンプレート
    • robots.txt templates
    • メニュー
    • ページネーション
    • Embedded templates
    • Custom output formats
  • 関数
    • 本節
    • cast
    • collections
    • compare
    • crypto
    • css
    • data
    • debug
    • diagrams
    • encoding
    • fmt
    • global
    • go template
    • hash
    • hugo
    • images
    • inflect
    • js
    • lang
    • math
    • openapi3
    • os
    • partials
    • path
    • reflect
    • resources
    • safe
    • strings
    • templates
    • time
    • transform
    • urls
  • メソッド
    • 本節
    • Duration
    • Menu
    • Menu entry
    • Page
    • Pager
    • Pages
    • Resource
    • Shortcode
    • Site
    • Taxonomy
    • Time
  • レンダーフック
    • 本節
    • Introduction
    • Blockquotes
    • Code blocks
    • Headings
    • Images
    • Links
    • Passthrough
    • Tables
  • Hugo モジュール
    • In this section
    • Configure Hugo modules
    • Use Hugo Modules
    • Theme components
  • Hugo パイプ
    • 本節
    • Introduction
    • Transpile Sass to CSS
    • PostCSS
    • PostProcess
    • JavaScript building
    • Babel
    • Asset minification
    • Concatenating assets
    • Fingerprinting and SRI hashing
    • Resource from string
    • Resource from template
  • CLI
  • トラブルシューティング
    • 本節
    • Logging
    • Inspection
    • Deprecation
    • Performance
    • FAQs
  • 開発ツール
    • 本節
    • Editor plugins
    • Front-ends
    • 検索
    • Migrations
    • Other projects
  • ホスティングと開発
    • 本節
    • Hugo Deploy
    • Deploy with Rclone
    • Deploy with Rsync
    • Host on 21YunBox
    • Host on AWS Amplify
    • Host on Azure Static Web Apps
    • Host on Cloudflare Pages
    • Host on Firebase
    • Host on GitHub Pages
    • Host on GitLab Pages
    • Host on KeyCDN
    • Host on Netlify
    • Host on Render
  • 貢献
    • 本節
    • Development
    • ドキュメント
    • Themes
  • メンテナンス
テンプレート 基本構成

テンプレート処理の基本

コンテント、リソース、データをレンダリングするためのテンプレートを生成します。

テンプレートとは一つのファイルに表されるものであり、プロジェクト、テーマ、モジュールに対応したレイアウトディレクトリ内に置きます。 テンプレート内においては 変数、関数、メソッド を利用して、コンテント、リソース、データの内容を公開ページへと変換します。

Hugo では Go 言語の text/template および html/template の各パッケージを用いています。

The text/template package implements data-driven templates for generating textual output, while the html/template package implements data-driven templates for generating HTML output safe against code injection.

デフォルトにおいて Hugo は html/template パッケージを使って HTML ファイルをレンダリングしています。

たとえば以下の HTML テンプレートでは、まず $v1 と $v2 という変数を初期化して、HTML の段落内にそれを含めたコードを生成しています。

{{ $v1 := 6 }}
{{ $v2 := 7 }}
<p>The product of {{ $v1 }} and {{ $v2 }} is {{ mul $v1 $v2 }}.</p>

HTML テンプレートが最もよく利用されていますが、これ以外にも CSV、JSON、RSS、プレーンテキストなどのように、どのような 出力書式 に対してもテンプレートを生成することができます。

コンテキスト

テンプレートを作るにあたって理解しておくべき重要な考え方は コンテキスト (context) です。 これは各テンプレートに受け渡されるデータのことです。 このデータとは単純な値の場合もありますが、一般的には オブジェクト やそれに関連する メソッド のこともあります。

たとえば一つのページに対応するテンプレートは Page オブジェクトを受け取ります。 そしてその Page オブジェクトは、値を返したりアクションを実行したりするメソッドを提供します。

カレントコンテキスト

テンプレート内において、ドット (.) はカレントコンテキスト (current context) を表します。

layouts/_default/single.html
<h2>{{ .Title }}</h2>

上の例におけるドットは Page オブジェクトを表しています。 そして Title メソッドを呼び出すことによって、フロントマター に定義されているタイトルを返すものです。

カレントコンテキストはテンプレート内において変わります。 たとえばテンプレートの上部において、コンテキストは Page オブジェクトとなるはずです。 しかし range や with のブロック内においてコンテキストへのバインドが再度行われるため、別の値や別のオブジェクトに変わります。

layouts/_default/single.html
<h2>{{ .Title }}</h2>

{{ range slice "foo" "bar" }}
  <p>{{ . }}</p>
{{ end }}

{{ with "baz" }}
  <p>{{ . }}</p>
{{ end }}

In the example above, the context changes as we range through the slice of values. In the first iteration the context is “foo”, and in the second iteration the context is “bar”. Inside of the with block the context is “baz”. Hugo renders the above to:

<h2>My Page Title</h2>
<p>foo</p>
<p>bar</p>
<p>baz</p>

テンプレートコンテキスト

range ブロックや with ブロック内においてそのテンプレートに元々受け渡されていたコンテキストにアクセスする場合は、ドットの前にドル記号 ($) をつけます。

layouts/_default/single.html
{{ with "foo" }}
  <p>{{ $.Title }} - {{ . }}</p>
{{ end }}

Hugo はこれを以下のようにレンダリングします。

<p>My Page Title - foo</p>

Make sure that you thoroughly understand the concept of context before you continue reading. The most common templating errors made by new users relate to context.

Actions

In the examples above the paired opening and closing braces represent the beginning and end of a template action, a data evaluation or control structure within a template.

A template action may contain literal values (boolean, string, integer, and float), variables, functions, and methods.

layouts/_default/single.html
{{ $convertToLower := true }}
{{ if $convertToLower }}
  <h2>{{ strings.ToLower .Title }}</h2>
{{ end }}

In the example above:

  • $convertToLower is a variable
  • true is a literal boolean value
  • strings.ToLower is a function that converts all characters to lowercase
  • Title is a method on a the Page object

Hugo renders the above to:



    <h2>my page title</h2>

Whitespace

Notice the blank lines and indentation in the previous example? Although irrelevant in production when you typically minify the output, you can remove the adjacent whitespace by using template action delimiters with hyphens:

layouts/_default/single.html
{{- $convertToLower := true -}}
{{- if $convertToLower -}}
  <h2>{{ strings.ToLower .Title }}</h2>
{{- end -}}

Hugo renders this to:

<h2>my page title</h2>

Whitespace includes spaces, horizontal tabs, carriage returns, and newlines.

Pipes

Within a template action you may pipe a value to function or method. The piped value becomes the final argument to the function or method. For example, these are equivalent:

{{ strings.ToLower "Hugo" }} → hugo
{{ "Hugo" | strings.ToLower }} → hugo

You can pipe the result of one function or method into another. For example, these are equivalent:

{{ strings.TrimSuffix "o" (strings.ToLower "Hugo") }} → hug
{{ "Hugo" | strings.ToLower | strings.TrimSuffix "o" }} → hug

These are also equivalent:

{{ mul 6 (add 2 5) }} → 42
{{ 5 | add 2 | mul 6 }} → 42

Remember that the piped value becomes the final argument to the function or method to which you are piping.

Line splitting

You can split a template action over two or more lines. For example, these are equivalent:

{{ $v := or .Site.Language.LanguageName .Site.Language.Lang }}

{{ $v := or
  .Site.Language.LanguageName
  .Site.Language.Lang
}}

You can also split raw string literals over two or more lines. For example, these are equivalent:

{{ $msg := "This is line one.\nThis is line two." }}

{{ $msg := `This is line one.
This is line two.`
}}

Variables

A variable is a user-defined identifier prepended with a dollar sign ($), representing a value of any data type, initialized or assigned within a template action. For example, $foo and $bar are variables.

Variables may contain scalars, slices, maps, or objects.

Use := to initialize a variable, and use = to assign a value to a variable that has been previously initialized. For example:

{{ $total := 3 }}
{{ range slice 7 11 21 }}
  {{ $total = add $total . }}
{{ end }}
{{ $total }} → 42

Variables initialized inside of an if, range, or with block are scoped to the block. Variables initialized outside of these blocks are scoped to the template.

With variables that represent a slice or map, use the index function to return the desired value.

{{ $slice := slice "foo" "bar" "baz" }}
{{ index $slice 2 }} → baz

{{ $map := dict "a" "foo" "b" "bar" "c" "baz" }}
{{ index $map "c" }} → baz

Slices and arrays are zero-based; element 0 is the first element.

With variables that represent a map or object, chain identifiers to return the desired value or to access the desired method.

{{ $map := dict "a" "foo" "b" "bar" "c" "baz" }}
{{ $map.c }} → baz

{{ $homePage := .Site.Home }}
{{ $homePage.Title }} → My Homepage

As seen above, object and method names are capitalized. Although not required, to avoid confusion we recommend beginning variable and map key names with a lowercase letter or underscore.

Functions

Used within a template action, a function takes one or more arguments and returns a value. Unlike methods, functions are not associated with an object.

Go’s text/template and html/template packages provide a small set of functions, operators, and statements for general use. See the go-templates section of the function documentation for details.

Hugo provides hundreds of custom functions categorized by namespace. For example, the strings namespace includes these and other functions:

Function Alias
strings.ToLower lower
strings.ToUpper upper
strings.Replace replace

As shown above, frequently used functions have an alias. Use aliases in your templates to reduce code length.

When calling a function, separate the arguments from the function, and from each other, with a space. For example:

{{ $total := add 1 2 3 4 }}

Methods

Used within a template action and associated with an object, a method takes zero or more arguments and either returns a value or performs an action.

The most commonly accessed objects are the Page and Site objects. This is a small sampling of the methods available to each object.

Object Method Description
Page Date Returns the date of the given page.
Page Params Returns a map of custom parameters as defined in the front matter of the given page.
Page Title Returns the title of the given page.
Site Data Returns a data structure composed from the files in the data directory.
Site Params Returns a map of custom parameters as defined in the site configuration.
Site Title Returns the title as defined in the site configuration.

Chain the method to its object with a dot (.) as shown below, remembering that the leading dot represents the current context.

layouts/_default/single.html
{{ .Site.Title }} → My Site Title
{{ .Page.Title }} → My Page Title

The context passed into most templates is a Page object, so this is equivalent to the previous example:

layouts/_default/single.html
{{ .Site.Title }} → My Site Title
{{ .Title }} → My Page Title

Some methods take an argument. Separate the argument from the method with a space. For example:

layouts/_default/single.html
{{ $page := .Page.GetPage "/books/les-miserables" }}
{{ $page.Title }} → Les Misérables

Comments

Do not attempt to use HTML comment delimiters to comment out template code.

Hugo strips HTML comments when rendering a page, but first evaluates any template code within the HTML comment delimiters. Depending on the template code within the HTML comment delimiters, this could cause unexpected results or fail the build.

Template comments are similar to template actions. Paired opening and closing braces represent the beginning and end of a comment. For example:

{{/* This is an inline comment. */}}
{{- /* This is an inline comment with adjacent whitespace removed. */ -}}

Code within a comment is not parsed, executed, or displayed. Comments may be inline, as shown above, or in block form:

{{/*
This is a block comment.
*/}}

{{- /*
This is a block comment with
adjacent whitespace removed.
*/ -}}

You may not nest one comment inside of another.

To render an HTML comment, pass a string through the safeHTML template function. For example:

{{ "<!-- I am an HTML comment. -->" | safeHTML }}
{{ printf "<!-- This is the %s site. -->" .Site.Title | safeHTML }}

Include

Use the template function to include one or more of Hugo’s embedded templates:

{{ template "_internal/google_analytics.html" . }}
{{ template "_internal/opengraph" . }}
{{ template "_internal/pagination.html" . }}
{{ template "_internal/schema.html" . }}
{{ template "_internal/twitter_cards.html" . }}

Use the partial or partialCached function to include one or more partial templates:

{{ partial "breadcrumbs.html" . }}
{{ partialCached "css.html" . }}

Create your partial templates in the layouts/partials directory.

In the examples above, note that we are passing the current context (the dot) to each of the templates.

Examples

This limited set of contrived examples demonstrates some of concepts described above. Please see the functions, methods, and templates documentation for specific examples.

Conditional blocks

See documentation for if, else, and end.

{{ $var := 42 }}
{{ if eq $var 6 }}
  {{ print "var is 6" }}
{{ else if eq $var 7 }}
  {{ print "var is 7" }}
{{ else if eq $var 42 }}
  {{ print "var is 42" }}
{{ else }}
  {{ print "var is something else" }}
{{ end }}

Logical operators

See documentation for and and or.

{{ $v1 := true }}
{{ $v2 := false }}
{{ $v3 := false }}
{{ $result := false }}

{{ if and $v1 $v2 $v3 }}
  {{ $result = true }}
{{ end }}
{{ $result }} → false

{{ if or $v1 $v2 $v3 }}
  {{ $result = true }}
{{ end }}
{{ $result }} → true

Loops

See documentation for range, else, and end.

{{ $s := slice "foo" "bar" "baz" }}
{{ range $s }}
  <p>{{ . }}</p>
{{ else }}
  <p>The collection is empty</p>
{{ end }}

Use the seq function to loop a specified number of times:

{{ $total := 0 }}
{{ range seq 4 }}
  {{ $total = add $total . }}
{{ end }}
{{ $total }} → 10

Rebind context

See documentation for with, else, and end.

{{ $var := "foo" }}
{{ with $var }}
  {{ . }} → foo
{{ else }}
  {{ print "var is falsy" }}
{{ end }}

To test multiple conditions:

{{ $v1 := 0 }}
{{ $v2 := 42 }}
{{ with $v1 }}
  {{ . }}
{{ else with $v2 }}
  {{ . }} → 42
{{ else }}
  {{ print "v1 and v2 are falsy" }}
{{ end }}

Access site parameters

See documentation for the Params method on a Site object.

With this site configuration:

hugo.
     
baseURL: https://example.org
params:
  author:
    email: jsmith@example.org
    name: John Smith
  copyright-year: "2023"
  layouts:
    rfc_1123: Mon, 02 Jan 2006 15:04:05 MST
    rfc_3339: "2006-01-02T15:04:05-07:00"
  subtitle: The Best Widgets on Earth
title: ABC Widgets
baseURL = 'https://example.org'
title = 'ABC Widgets'
[params]
  copyright-year = '2023'
  subtitle = 'The Best Widgets on Earth'
  [params.author]
    email = 'jsmith@example.org'
    name = 'John Smith'
  [params.layouts]
    rfc_1123 = 'Mon, 02 Jan 2006 15:04:05 MST'
    rfc_3339 = '2006-01-02T15:04:05-07:00'
{
   "baseURL": "https://example.org",
   "params": {
      "author": {
         "email": "jsmith@example.org",
         "name": "John Smith"
      },
      "copyright-year": "2023",
      "layouts": {
         "rfc_1123": "Mon, 02 Jan 2006 15:04:05 MST",
         "rfc_3339": "2006-01-02T15:04:05-07:00"
      },
      "subtitle": "The Best Widgets on Earth"
   },
   "title": "ABC Widgets"
}

Access the custom site parameters by chaining the identifiers:

{{ .Site.Params.subtitle }} → The Best Widgets on Earth
{{ .Site.Params.author.name }} → John Smith

{{ $layout := .Site.Params.layouts.rfc_1123 }}
{{ .Site.Lastmod.Format $layout }} → Tue, 17 Oct 2023 13:21:02 PDT

Access page parameters

See documentation for the Params method on a Page object.

With this front matter:

content/news/annual-conference.md.
     
date: 2023-10-17T15:11:37-07:00
params:
  author:
    email: jsmith@example.org
    name: John Smith
  display_related: true
title: Annual conference
date = 2023-10-17T15:11:37-07:00
title = 'Annual conference'
[params]
  display_related = true
  [params.author]
    email = 'jsmith@example.org'
    name = 'John Smith'
{
   "date": "2023-10-17T15:11:37-07:00",
   "params": {
      "author": {
         "email": "jsmith@example.org",
         "name": "John Smith"
      },
      "display_related": true
   },
   "title": "Annual conference"
}

Access the custom page parameters by chaining the identifiers:

{{ .Params.display_related }} → true
{{ .Params.author.name }} → John Smith

関連項目

  • Blockquote render hooks
  • Code block render hooks
  • Colors
  • Exif
  • File

このページ内

  • コンテキスト
  • Actions
  • Variables
  • Functions
  • Methods
  • Comments
  • Include
  • Examples
最終更新日付: 0001/01/01
ページの変更
Hugo 作者より
Hugo Logo
  • Issue 報告
  • ヘルプ
  • @GoHugoIO
  • @spf13
  • @bepsays
 

Hugo Sponsors

Route4Me
Your Company?
 

The Hugo logos are copyright © Steve Francia 2013–2024.

The Hugo Gopher is based on an original work by Renée French.

  • ニュース
  • ドキュメント
  • テーマ
  • コミュニティ
  • GitHub
  • Hugo について
  • インストール
  • はじめよう
  • クイックリファレンス
  • コンテント管理
  • テンプレート
  • 関数
  • メソッド
  • レンダーフック
  • Hugo モジュール
  • Hugo パイプ
  • CLI
  • トラブルシューティング
  • 開発ツール
  • ホスティングと開発
  • 貢献
  • メンテナンス