目次
HugoのLayoutの中身について理解していきます。
Layoutの中身は要はテンプレートで、各種ページのHTMLがレンダリングされる際にこれらのテンプレートが使用されます。
Hugoのページ構成
Contentディレクトリ
HTMLレンダリング前の構成は以下のようになります。
content/
  ├── _index.html(ホームページ)
  ├── page-1.md(通常ページ)
  ├── page-2.md(通常ページ)
  ├── section-1/
  │     ├── _index.html(セクションページ)
  │     ├── page-1-1.html(通常ページ)
  │     └── page-1-2.html(通常ページ)
  └── section-2/
        ├── _index.html(セクションページ)
        ├── page-2-1.html(通常ページ)
        └── page-2-2.html(通常ページ)
これらの各ページに対してHugoが適用するテンプレートを優先度付けしています。
ページの種類
テンプレートが適用されて、HTMLレンダリング後のページの種類は以下のようになります。
| 種類 | 説明 | 例 | 
|---|---|---|
| home | ホームページ | /index.html | 
| page | 通常ページ | /posts/{投稿名}/index.html | 
| section | セクションページ | /{セクション名}/index.html | 
| taxonomy | 分類一覧ページ | /tags/index.html や /categories/index.html | 
| term | 分類絞り込みページ | /tags/{タグ名}/index.html や /categories/{カテゴリ名}/index.html | 
分類一覧ページや分類絞り込みページは、通常ページの設定を読み取ってレンダリングされるページです。
ホームページ
ホームページ(content/_index.html)をレンダリングする場合の優先度です。
- /layouts/index.html
 - /layouts/home.html
 - /layouts/list.html
 - /layouts/_default/index.html
 - /layouts/_default/home.html
 - /layouts/_default/list.html
 - /themes/{テーマ名}/layouts/index.html
 - /themes/{テーマ名}/layouts/home.html
 - /themes/{テーマ名}/layouts/list.html
 - /themes/{テーマ名}/layouts/_default/index.html
 - /themes/{テーマ名}/layouts/_default/home.html
 - /themes/{テーマ名}/layouts/_default/list.html
 
基本ルールは以下のようになります。
- テーマより、独自で定義したテンプレートが優先される
 - _defaultフォルダ内より、layouts直下のテンプレートが優先される
 - 汎用のリストテンプレートなどより、より特化したテンプレートが優先される
 
セクションページ
セクションページ(content/{section_name}/_index.md)をレンダリングする場合の優先度です。
- /layouts/section/{セクション名}.html ← セクション特化型テンプレート
 - /layouts/{セクション名}/list.html ← セクション特化型リストテンプレート
 - /layouts/_default/section.html
 - /layouts/_default/list.html
 - 省略(テーマ内のテンプレートが上記の優先度と同様使用される)
 
{セクション名} は特定のセクション名の時だけ使用されるテンプレートです。
リストテンプレート
ホームページやセクションページの優先度でも、リストテンプレートが出てきましたね。
リストテンプレートは、特定のグループ内の記事一覧を表示するためのテンプレートです。 例えば、セクションページでリストテンプレートが使用された場合、そのセクションの記事リストを表示するような結果になります。
シングルページ
通常ページ(content/{投稿名}.md)に適用されるテンプレートです。
- /layouts/{タイプ名}/{レイアウト名}.html
 - /layouts/{セクション名}/{レイアウト名}.html
 - /layouts/{タイプ名}/single.html
 - /layouts/{セクション名}/single.html
 - /layouts/_default/single.html
 - 省略(テーマ内のテンプレートが上記の優先度と同様使用される)
 
タクソノミーテンプレート
分類一覧ページとして表示されるテンプレートです。
- layouts/_default/category.terms.html
 - layouts/_default/terms.html
 - layouts/_default/taxonomy.html
 - layouts/_default/list.html
 - 省略(テーマ内のテンプレートが上記の優先度と同様使用される)