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
  • メンテナンス
関数 COLLECTIONS 関数

collections.Where

指定されたコレクションから、比較条件に合致しない要素を取り除いて返します。

文法

collections.Where COLLECTION KEY [OPERATOR] VALUE

戻り値

any

エイリアス

where

where 関数は指定されたコレクションに対して、比較条件に合致しない要素を取り除いたコレクションを返します。比較条件は KEY、OPERATOR、VALUE の引数により構成されます。

collections.Where COLLECTION KEY [OPERATOR] VALUE
                             --------------------
                                 比 較 条 件

OPERATOR 引数が指定されなかった場合は、一致するかどうかが条件となります。 たとえば以下です。

{{ $pages := where .Site.RegularPages "Section" "books" }}
{{ $books := where .Site.Data.books "genres" "suspense" }}

引数

where 関数は 3 つ、または 4 つの引数をとります。 OPERATOR 引数は任意です。

COLLECTION
(any) A page collection or a slice of maps.
KEY
(string) The key of the page or map value to compare with VALUE. With page collections, commonly used comparison keys are Section, Type, and Params. To compare with a member of the page Params map, chain the subkey as shown below:
{{ $result := where .Site.RegularPages "Params.foo" "bar" }}
OPERATOR
(string) 論理比較オペレーター.
VALUE
(any) The value with which to compare. The values to compare must have comparable data types. For example:
比較 結果
"123" "eq" "123" true
"123" "eq" 123 false
false "eq" "false" false
false "eq" false true

When one or both of the values to compare is a slice, use the in, not in, or intersect operators as described below.

オペレーター

以下のいずれかの論理オペレーターを利用します。

=, ==, eq
(bool) Reports whether the given field value is equal to VALUE.
!=, <>, ne
(bool) Reports whether the given field value is not equal to VALUE.
>=, ge
(bool) Reports whether the given field value is greater than or equal to VALUE.
>, gt
true Reports whether the given field value is greater than VALUE.
<=, le
(bool) Reports whether the given field value is less than or equal to VALUE.
<, lt
(bool) Reports whether the given field value is less than VALUE.
in
(bool) Reports whether the given field value is a member of VALUE. Compare string to slice, or string to string. See details.
not in
(bool) Reports whether the given field value is not a member of VALUE. Compare string to slice, or string to string. See details.
intersect
(bool) Reports whether the given field value (a slice) contains one or more elements in common with VALUE. See details.
like v0.116.0 の新機能
(bool) Reports whether the given field value matches the regular expression specified in VALUE. Use the like operator to compare string values. The like operator returns false when comparing other data types to the regular expression.

The examples below perform comparisons within a page collection, but the same comparisons are applicable to a slice of maps.

文字列比較

Compare the value of the given field to a string:

{{ $pages := where .Site.RegularPages "Section" "eq" "books" }}
{{ $pages := where .Site.RegularPages "Section" "ne" "books" }}

数値比較

Compare the value of the given field to an int or float:

{{ $books := where site.RegularPages "Section" "eq" "books" }}

{{ $pages := where $books "Params.price" "eq" 42 }}
{{ $pages := where $books "Params.price" "ne" 42.67 }}
{{ $pages := where $books "Params.price" "ge" 42 }}
{{ $pages := where $books "Params.price" "gt" 42.67 }}
{{ $pages := where $books "Params.price" "le" 42 }}
{{ $pages := where $books "Params.price" "lt" 42.67 }}

ブール値比較

Compare the value of the given field to a bool:

{{ $books := where site.RegularPages "Section" "eq" "books" }}

{{ $pages := where $books "Params.fiction" "eq" true }}
{{ $pages := where $books "Params.fiction" "eq" false }}
{{ $pages := where $books "Params.fiction" "ne" true }}
{{ $pages := where $books "Params.fiction" "ne" false }}

メンバー比較

Compare a scalar to a slice.

For example, to return a collection of pages where the color page parameter is either “red” or “yellow”:

{{ $fruit := where site.RegularPages "Section" "eq" "fruit" }}

{{ $colors := slice "red" "yellow" }}
{{ $pages := where $fruit "Params.color" "in" $colors }}

To return a collection of pages where the “color” page parameter is neither “red” nor “yellow”:

{{ $fruit := where site.RegularPages "Section" "eq" "fruit" }}

{{ $colors := slice "red" "yellow" }}
{{ $pages := where $fruit "Params.color" "not in" $colors }}

Intersection comparison

Compare a slice to a slice, returning collection elements with common values. This is frequently used when comparing taxonomy terms.

For example, to return a collection of pages where any of the terms in the “genres” taxonomy are “suspense” or “romance”:

{{ $books := where site.RegularPages "Section" "eq" "books" }}

{{ $genres := slice "suspense" "romance" }}
{{ $pages := where $books "Params.genres" "intersect" $genres }}

正規表現比較

v0.116.0 の新機能

To return a collection of pages where the “author” page parameter begins with either “victor” or “Victor”:

{{ $pages := where .Site.RegularPages "Params.author" "like" `(?i)^victor` }}

正規表現を指定する際には、“解釈される” (interpreted) 文字列リテラル (ダブルクォートで囲む) ではなく、そのままの文字列リテラル (バッククォートで囲む) を用いることで、文法を簡易なものにしてください。 “解釈される” 文字列リテラルを用いる場合、バックスラッシュそのものはエスケープする必要があります。

Go 言語の正規表現パッケージでは RE2 文法 を実装しています。 RE2 文法とは、おおざっぱに言えば PCRE が許容する文法のサブセットであり、さまざまな 注意事項 があります。 なお RE2 のエスケープシーケンス \C はサポートされません。

Use the like operator to compare string values. Comparing other data types will result in an empty collection.

Date comparison

Predefined dates

There are four predefined front matter dates: date, publishDate, lastmod, and expiryDate. Regardless of the front matter data format (TOML, YAML, or JSON) these are time.Time values, allowing precise comparisons.

For example, to return a collection of pages that were created before the current year:

{{ $startOfYear := time.AsTime (printf "%d-01-01" now.Year) }}
{{ $pages := where .Site.RegularPages "Date" "lt" $startOfYear }}

Custom dates

With custom front matter dates, the comparison depends on the front matter data format (TOML, YAML, or JSON).

Using TOML for pages with custom front matter dates enables precise date comparisons.

With TOML, date values are first-class citizens. TOML has a date data type while JSON and YAML do not. If you quote a TOML date, it is a string. If you do not quote a TOML date value, it is time.Time value, enabling precise comparisons.

In the TOML example below, note that the event date is not quoted.

content/events/2024-user-conference.md
+++
title = '2024 User Conference"
eventDate = 2024-04-01
+++

To return a collection of future events:

{{ $events := where .Site.RegularPages "Type" "events" }}
{{ $futureEvents := where $events "Params.eventDate" "gt" now }}

When working with YAML or JSON, or quoted TOML values, custom dates are strings; you cannot compare them with time.Time values. String comparisons may be possible if the custom date layout is consistent from one page to the next. To be safe, filter the pages by ranging through the collection:

{{ $events := where .Site.RegularPages "Type" "events" }}
{{ $futureEvents := slice }}
{{ range $events }}
  {{ if gt (time.AsTime .Params.eventDate) now }}
    {{ $futureEvents = $futureEvents | append . }}
  {{ end }}
{{ end }}

Nil comparison

To return a collection of pages where the “color” parameter is present in front matter, compare to nil:

{{ $pages := where .Site.RegularPages "Params.color" "ne" nil }}

To return a collection of pages where the “color” parameter is not present in front matter, compare to nil:

{{ $pages := where .Site.RegularPages "Params.color" "eq" nil }}

In both examples above, note that nil is not quoted.

Nested comparison

These are equivalent:

{{ $pages := where .Site.RegularPages "Type" "tutorials" }}
{{ $pages = where $pages "Params.level" "eq" "beginner" }}
{{ $pages := where (where .Site.RegularPages "Type" "tutorials") "Params.level" "eq" "beginner" }}

Portable section comparison

Useful for theme authors, avoid hardcoding section names by using the where function with the MainSections method on a Site object.

{{ $pages := where .Site.RegularPages "Section" "in" .Site.MainSections }}

With this construct, a theme author can instruct users to specify their main sections in the site configuration:

hugo.
     
params:
  mainSections:
  - blog
  - galleries
[params]
  mainSections = ['blog', 'galleries']
{
   "params": {
      "mainSections": [
         "blog",
         "galleries"
      ]
   }
}

If params.mainSections is not defined in the site configuration, the MainSections method returns a slice with one element—the top level section with the most pages.

Boolean/undefined comparison

Consider this site content:

content/
├── posts/
│   ├── _index.md
│   ├── post-1.md  <-- front matter: exclude = false
│   ├── post-2.md  <-- front matter: exclude = true
│   └── post-3.md  <-- front matter: exclude not defined
└── _index.md

The first two pages have an “exclude” field in front matter, but the last page does not. When testing for equality, the third page is excluded from the result. When testing for inequality, the third page is included in the result.

Equality test

This template:

<ul>
  {{ range where .Site.RegularPages "Params.exclude" "eq" false }}
    <li><a href="{{ .RelPermalink }}">{{ .LinkTitle }}</a></li>
  {{ end }}
</ul>

Is rendered to:

<ul>
  <li><a href="/posts/post-1/">Post 1</a></li>
</ul>

This template:

<ul>
  {{ range where .Site.RegularPages "Params.exclude" "eq" true }}
    <li><a href="{{ .RelPermalink }}">{{ .LinkTitle }}</a></li>
  {{ end }}
</ul>

Is rendered to:

<ul>
  <li><a href="/posts/post-2/">Post 2</a></li>
</ul>

Inequality test

This template:

<ul>
  {{ range where .Site.RegularPages "Params.exclude" "ne" false }}
    <li><a href="{{ .RelPermalink }}">{{ .LinkTitle }}</a></li>
  {{ end }}
</ul>

Is rendered to:

<ul>
  <li><a href="/posts/post-2/">Post 2</a></li>
  <li><a href="/posts/post-3/">Post 3</a></li>
</ul>

This template:

<ul>
  {{ range where .Site.RegularPages "Params.exclude" "ne" true }}
    <li><a href="{{ .RelPermalink }}">{{ .LinkTitle }}</a></li>
  {{ end }}
</ul>

Is rendered to:

<ul>
  <li><a href="/posts/post-1/">Post 1</a></li>
  <li><a href="/posts/post-3/">Post 3</a></li>
</ul>

To exclude a page with an undefined field from a boolean inequality test:

  1. Create a collection using a boolean comparison
  2. Create a collection using a nil comparison
  3. Subtract the second collection from the first collection using the collections.Complement function.

This template:

{{ $p1 := where .Site.RegularPages "Params.exclude" "ne" true }}
{{ $p2 := where .Site.RegularPages "Params.exclude" "eq" nil  }}
<ul>
  {{ range $p1 | complement $p2 }}
    <li><a href="{{ .RelPermalink }}">{{ .LinkTitle }}</a></li>
  {{ end }}
</ul>

Is rendered to:

<ul>
  <li><a href="/posts/post-1/">Post 1</a></li>
</ul>

This template:

{{ $p1 := where .Site.RegularPages "Params.exclude" "ne" false }}
{{ $p2 := where .Site.RegularPages "Params.exclude" "eq" nil  }}
<ul>
  {{ range $p1 | complement $p2 }}
    <li><a href="{{ .RelPermalink }}">{{ .LinkTitle }}</a></li>
  {{ end }}
</ul>

Is rendered to:

<ul>
  <li><a href="/posts/post-1/">Post 2</a></li>
</ul>

このページ内

  • 引数
  • オペレーター
  • 文字列比較
  • 数値比較
  • ブール値比較
  • メンバー比較
  • Intersection comparison
  • 正規表現比較
  • Date comparison
  • Nil comparison
  • Nested comparison
  • Portable section comparison
  • Boolean/undefined comparison

このセクション内

  • collections.After
  • collections.Append
  • collections.Apply
  • collections.Complement
  • collections.Delimit
  • collections.Dictionary
  • collections.First
  • collections.Group
  • collections.In
  • collections.Index
  • collections.Intersect
  • collections.IsSet
  • collections.KeyVals
  • collections.Last
  • collections.Merge
  • collections.NewScratch
  • collections.Querify
  • collections.Reverse
  • collections.Seq
  • collections.Shuffle
  • collections.Slice
  • collections.Sort
  • collections.SymDiff
  • collections.Union
  • collections.Uniq
  • collections.Where
最終更新日付: 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
  • トラブルシューティング
  • 開発ツール
  • ホスティングと開発
  • 貢献
  • メンテナンス