参照サイト
https://gohugo.io/variables/page/
Notice: You are in development mode or could not get the remote server
gohugoio/hugo: The world’s fastest framework for building websites.
テーマをカスタマイズする際に、特定条件で挙動を変えたいシーンが多々発生するが、その際に使い勝手のよい変数。
Notice: You are in development mode or could not get the remote server
.Kind |
.Section |
.Type |
|
|---|---|---|---|
| トップページを表示するとき | home |
値なし | page |
| セクションのトップを表示するとき | section |
technologies |
original_blog |
| 記事(ページ)表示するとき | page |
technologies |
original_blog |
カテゴリ一覧 (categories/) を表示するとき |
taxonomy |
categories |
categories |
特定のカテゴリ (categories/XXX) を表示するとき |
term |
categories |
categories |
タグ一覧 (tags/) を表示するとき |
taxonomy |
tags |
tags |
特定のカテゴリ (tags/YYY) を表示するとき |
term |
tags |
tags |
関連メソッド
.IsSection : true if .Kind is section.IsPage: always true for regular content pages..IsHome: true in the context of the homepage.上記の表での前提
technologies という名前のセクション名に記事を配置しているとする。意識していなければ、デフォルトは posts に作成される。layouts/original_blog を適用しているとする。
_index.md に cascade を用いて以下の記載をするのが手っ取り早い。---
title: "Technologies (技術ブログ)"
cascade:
- type: "original_blog"
---
hugo server したときに Themeファイルのダウンロードがされる
Error: Error building site: "/Users/path/to/project/content/docs/article/_index.md:8:1": failed to extract shortcode: template for shortcode "alert" not found
このケースでは、 Docsy が提供している shortcode が見つからないとのこと(何故か)。手元の macOS環境にて発生。
hugo mod clean にてキャッシュクリアしたら起動できる(細部未確認のため再現性不明)
https://gohugo.io/templates/introduction/#the-dot
1. Define a Variable Independent of Context
The most easily overlooked concept to understand about Go Templates is that {{ . }} always refers to the current context.
{{ . }} will no longer refer to the data available to the entire page.2. Use $. to Access the Global Context
$ has special significance in your templates. $ is set to the starting value of . (“the dot”) by default. This is a documented feature of Go text/template. This means you have access to the global context from anywhere. Here is an equivalent example of the preceding code block but now using $ to grab .Site.Title from the global context:
To lazy load images within Hugo two things are required:
loading="lazy" attribute
Notice: You are in development mode or could not get the remote server
Notice: You are in development mode or could not get the remote server
Notice: You are in development mode or could not get the remote server
RSSページテンプレートで日付の逆順にソートしたい場合
$pages に表示したい対象のページを where で絞り込んで変数化range の部分では .ByParam "date" を用いてソートキーとして利用
.Reverse することで逆順(最新日付を最上部)としている{{/* $filterSections には任意のセクション名を入れておく(ホワイトリスト式) */}}
{{ $filterSections := (slice "posts") }}
{{- $pages = where (.Site.RegularPages) "Section" "in" $filterSections -}}
{{/* 中略 */}}
{{ range ($pages.ByParam "date").Reverse }}
...
リストのループでユニークな連番を付与したいケース
https://discourse.gohugo.io/t/how-counting-up-a-variable-in-range/586/8
{{ $.Scratch.Set "counter" 0 }}
{{range sort .Params.employees}}
{{ $.Scratch.Set "counter" (add ($.Scratch.Get "counter") 1) }}
I am the number {{$.Scratch.Get "counter"}} in loop!
{{end}}
Hugo で Docsy テーマのタグ一覧ページを Noindex にする方法
Hugo minify により HTMLコメントが削除されてしまうなかで PageMap 構造データをコメント表示するためのハック