Limit related pages for functions to other functions

This commit is contained in:
Joe Mooring
2023-09-26 00:43:39 -07:00
committed by GitHub
parent 9fa67d981b
commit 407dd5c475
155 changed files with 1882 additions and 580 deletions
+6 -4
View File
@@ -1,13 +1,15 @@
---
title: .GetPage
description: "Gets a `Page` of a given `path`."
description: Gets a `Page` of a given `path`.
categories: [functions]
menu:
docs:
parent: functions
keywords: [sections,lists,indexes]
signature: [".GetPage PATH"]
relatedfuncs: []
keywords: []
namespace:
relatedFuncs: []
signature:
- .GetPage PATH
---
`.GetPage` returns a page of a given `path`. Both `Site` and `Page` implements this method. The `Page` variant will, if given a relative path -- i.e. a path without a leading `/` -- try look for the page relative to the current page.
+6 -3
View File
@@ -1,12 +1,15 @@
---
title: .RenderString
description: "Renders markup to HTML."
description: sRenders markup to HTML.
categories: [functions]
menu:
docs:
parent: functions
keywords: [markdown,goldmark,render]
signature: [".RenderString MARKUP"]
keywords: []
namespace:
relatedFuncs: []
signature:
- .RenderString MARKUP [OPTIONS]
---
`.RenderString` is a method on `Page` that renders some markup to HTML using the content renderer defined for that page (if not set in the options).
+8 -2
View File
@@ -5,8 +5,14 @@ categories: [functions]
menu:
docs:
parent: functions
keywords: [urls, multilingual,i18n]
signature: ["absLangURL INPUT"]
keywords: []
namespace: urls
relatedFuncs:
- urls.AbsLangURL
- urls.AbsURL
- urls.RelLangURL
- urls.RelURL
signature: [absLangURL INPUT]
---
Use this function with both monolingual and multilingual configurations. The URL returned by this function depends on:
+9 -3
View File
@@ -5,8 +5,14 @@ categories: [functions]
menu:
docs:
parent: functions
keywords: [urls]
signature: ["absURL INPUT"]
keywords: []
namespace: urls
relatedFuncs:
- urls.AbsLangURL
- urls.AbsURL
- urls.RelLangURL
- urls.RelURL
signature: [absURL INPUT]
---
With multilingual configurations, use the [`absLangURL`] function instead. The URL returned by this function depends on:
@@ -34,7 +40,7 @@ With `baseURL = https://example.org/docs/`
{{ absURL "style.css" }} → https://example.org/docs/style.css
```
### Input begins with a slash
#### Input begins with a slash
If the input begins with a slash, the resulting URL will be incorrect when the `baseURL` includes a subdirectory. With a leading slash, the function returns a URL relative to the protocol+host section of the `baseURL`.
+4 -3
View File
@@ -5,9 +5,10 @@ categories: [functions]
menu:
docs:
parent: functions
keywords: [dates,time]
signature: [".AddDate YEARS MONTHS DAYS"]
relatedfuncs: [now]
keywords: []
namespace:
relatedFuncs: []
signature: [.AddDate YEARS MONTHS DAYS]
---
```go-html-template
+8 -4
View File
@@ -1,13 +1,17 @@
---
title: after
description: "`after` slices an array to only the items after the <em>N</em>th item."
description: Slices an array to the items after the Nth item.
categories: [functions]
menu:
docs:
parent: functions
keywords: [iteration]
signature: ["after INDEX COLLECTION"]
relatedfuncs: [last,first,seq]
keywords: []
namespace: collections
relatedFuncs:
- collections.After
- collections.First
- collections.Last
signature: [after INDEX COLLECTION]
---
The following shows `after` being used in conjunction with the [`slice` function][slice]:
+8 -3
View File
@@ -5,9 +5,14 @@ categories: [functions]
menu:
docs:
parent: functions
keywords: [markdown,strings]
signature: ["anchorize INPUT"]
relatedfuncs: [humanize]
keywords: []
namespace: urls
relatedFuncs:
- urls.Anchorize
- urls.URLize
signature:
- urls.Anchorize INPUT
- anchorize INPUT
---
If [Goldmark](/getting-started/configuration-markup#goldmark) is set as `defaultMarkdownHandler`, the sanitizing logic adheres to the setting [`markup.goldmark.parser.autoHeadingIDType`](/getting-started/configuration-markup#goldmark).
+10 -3
View File
@@ -5,9 +5,16 @@ categories: [functions]
menu:
docs:
parent: functions
keywords: [collections]
signature: ["COLLECTION | append ELEMENT [ELEMENT]...", "COLLECTION | append COLLECTION"]
relatedfuncs: [last,first,where,slice]
keywords: []
namespace: collections
relatedFuncs:
- collections.Append
- collections.Merge
signature:
- COLLECTION | collections.Append ELEMENT [ELEMENT]...
- COLLECTION | collections.Append COLLECTION
- COLLECTION | append ELEMENT [ELEMENT]...
- COLLECTION | append COLLECTION
---
This function appends all elements, excluding the last, to the last element. This allows [pipe](/getting-started/glossary/#pipeline) constructs as shown below.
+12 -3
View File
@@ -5,9 +5,18 @@ categories: [functions]
menu:
docs:
parent: functions
keywords: [advanced]
signature: ["apply COLLECTION FUNCTION [PARAM...]"]
relatedfuncs: []
keywords: []
namespace: collections
relatedFuncs:
- collections.Apply
- collections.Delimit
- collections.In
- collections.Reverse
- collections.Seq
- collections.Slice
signature:
- collections.Apply COLLECTION FUNCTION [PARAM...]
- apply COLLECTION FUNCTION [PARAM...]
---
`apply` expects at least three arguments, depending on the function being applied.
-28
View File
@@ -1,28 +0,0 @@
---
title: base64
description: "`base64Encode` and `base64Decode` let you easily decode content with a base64 encoding and vice versa through pipes."
categories: [functions]
menu:
docs:
parent: functions
keywords: []
relatedfuncs: []
signature: ["base64Decode INPUT", "base64Encode INPUT"]
---
```go-html-template
{{ "Hugo" | base64Encode }} → "SHVnbw=="
{{ "SHVnbw==" | base64Decode }} → "Hugo"
```
## `base64` with APIs
Using base64 to decode and encode becomes really powerful if we have to handle
responses from APIs.
```go-html-template
{{ $resp := getJSON "https://api.github.com/repos/gohugoio/hugo/readme" }}
{{ $resp.content | base64Decode | markdownify }}
```
The response of the GitHub API contains the base64-encoded version of the [README.md](https://github.com/gohugoio/hugo/blob/master/README.md) in the Hugo repository. Now we can decode it and parse the Markdown. The final output will look similar to the rendered version on GitHub.
+43
View File
@@ -0,0 +1,43 @@
---
title: base64Decode
description: Returns the base64 decoding of the given content.
categories: [functions]
menu:
docs:
parent: functions
keywords: []
namespace: encoding
relatedFuncs:
- encoding.Base64Decode
- encoding.Base64Encode
signature:
- encoding.Base64Decode INPUT
- base64Decode INPUT
---
```go-html-template
{{ "SHVnbw==" | base64Decode }} → "Hugo"
```
Use the `base64Decode` function to decode responses from APIs. For example, the result of this call to GitHub's API contains the base64-encoded representation of the repository's README file:
```text
https://api.github.com/repos/gohugoio/hugo/readme
```
To retrieve and render the content:
```go-html-template
{{ $u := "https://api.github.com/repos/gohugoio/hugo/readme" }}
{{ with resources.GetRemote $u }}
{{ with .Err }}
{{ errorf "%s" . }}
{{ else }}
{{ with . | transform.Unmarshal }}
{{ .content | base64Decode | markdownify }}
{{ end }}
{{ end }}
{{ else }}
{{ errorf "Unable to get remote resource %q" $u }}
{{ end }}
```
+21
View File
@@ -0,0 +1,21 @@
---
title: base64Encode
description: Returns the base64 decoding of the given content.
categories: [functions]
menu:
docs:
parent: functions
keywords: []
namespace: encoding
relatedFuncs:
- encoding.Base64Decode
- encoding.Base64Encode
signature:
- encoding.Base64Encode INPUT
- base64Encode INPUT
aliases: ['/functions/base64']
---
```go-html-template
{{ "Hugo" | base64Encode }} → "SHVnbw=="
```
+11 -4
View File
@@ -6,11 +6,18 @@ categories: [functions]
menu:
docs:
parent: functions
keywords: [trim]
keywords: []
namespace: strings
relatedFuncs:
- strings.Chomp
- strings.Trim
- strings.TrimLeft
- strings.TrimPrefix
- strings.TrimRight
- strings.TrimSuffix
signature:
- "chomp INPUT"
- "strings.Chomp INPUT"
relatedfuncs: [truncate]
- chomp STRING
- strings.Chomp STRING
---
Useful in a pipeline to remove newlines added by other processing (e.g., [`markdownify`](/functions/markdownify/)).
@@ -0,0 +1,24 @@
---
title: collections.Reverse
description: Reverses the order of a collection.
categories: [functions]
menu:
docs:
parent: functions
keywords: []
namespace: collections
relatedFuncs:
- collections.Apply
- collections.Delimit
- collections.In
- collections.Reverse
- collections.Seq
- collections.Slice
signature:
- collections.Reverse COLLECTION
---
```go-html-template
{{ slice 1 3 2 1 | uniq }} → [1 3 2]
```
+8 -4
View File
@@ -5,11 +5,15 @@ categories: [functions]
menu:
docs:
parent: functions
keywords: [collections]
namespace: collections
relatedFuncs:
- collections.Complement
- collections.Intersect
- collections.SymDiff
- collections.Union
signature:
- "complement COLLECTION [COLLECTION]..."
- "collections.Complement COLLECTION [COLLECTION]..."
relatedfuncs: [intersect,symdiff,union]
- collections.Complement COLLECTION [COLLECTION]...
- complement COLLECTION [COLLECTION]...
---
To find the elements within `$c3` that do not exist in `$c1` or `$c2`:
+8 -3
View File
@@ -2,12 +2,17 @@
title: cond
description: Returns one of two arguments depending on the value of the control argument.
categories: [functions]
keywords: [conditional, ternary]
menu:
docs:
parent: functions
signature: [cond CONTROL ARG1 ARG2]
relatedfuncs: [default]
keywords: []
namespace: compare
relatedFuncs:
- compare.Conditional
- compare.Default
signature:
- compare.Conditional CONTROL ARG1 ARG2
- cond CONTROL ARG1 ARG2
---
The CONTROL argument is a boolean value that indicates whether the function should return ARG1 or ARG2. If CONTROL is `true`, the function returns ARG1. Otherwise, the function returns ARG2.
+10 -4
View File
@@ -5,11 +5,17 @@ categories: [functions]
menu:
docs:
parent: functions
keywords: [counting, word count]
keywords: []
namespace: strings
relatedFuncs:
- len
- strings.Count
- strings.CountRunes
- strings.CountWords
- strings.RuneCount
signature:
- "countrunes INPUT"
- "strings.CountRunes INPUT"
relatedfuncs: []
- strings.CountRunes INPUT
- countrunes INPUT
---
In contrast with `countwords` function, which counts every word in a string, the `countrunes` function determines the number of runes in the content and excludes any whitespace. This has specific utility if you are dealing with CJK-like languages.
+11 -3
View File
@@ -5,9 +5,17 @@ categories: [functions]
menu:
docs:
parent: functions
keywords: [counting, word count]
signature: ["countwords INPUT"]
relatedfuncs: [countrunes]
keywords: []
namespace: strings
relatedFuncs:
- len
- strings.Count
- strings.CountRunes
- strings.CountWords
- strings.RuneCount
signature:
- strings.CountWords INPUT
- countwords INPUT
---
The template function works similar to the [.WordCount page variable][pagevars].
+13 -3
View File
@@ -5,10 +5,20 @@ categories: [functions]
menu:
docs:
parent: functions
keywords: [crypto hash FNV32 Fowler-Noll-Vo]
signature: ["crypto.FNV32a STRING"]
keywords: []
namespace: crypto
relatedFuncs:
- crypto.FNV32a
- crypto.HMAC
- crypto.MD5
- crypto.SHA1
- crypto.SHA256
signature:
- crypto.FNV32a STRING
---
This function calculates the 32 bit [FNV1a hash](https://en.wikipedia.org/wiki/Fowler%E2%80%93Noll%E2%80%93Vo_hash_function#FNV-1a_hash) of a given string according to the [specification](https://datatracker.ietf.org/doc/html/draft-eastlake-fnv-12):
{{ crypto.FNV32a "Hello world" }} → 1498229191
```go-html-template
{{ crypto.FNV32a "Hello world" }} → 1498229191
```
+10 -4
View File
@@ -5,11 +5,17 @@ categories: [functions]
menu:
docs:
parent: functions
keywords: [dates,time,strings]
keywords: []
namespace: time
relatedFuncs:
- time.AsTime
- time.Duration
- time.Format
- time.Now
- time.ParseDuration
signature:
- "time.Format LAYOUT INPUT"
- "dateFormat LAYOUT INPUT"
relatedfuncs: [Format,now,Unix,time]
- time.Format LAYOUT INPUT
- dateFormat LAYOUT INPUT
---
`time.Format` (alias `dateFormat`) converts either a `time.Time` object (e.g. `.Date`) or a timestamp string `INPUT` into the format specified by the `LAYOUT` string.
+8 -5
View File
@@ -1,15 +1,18 @@
---
title: default
description: Allows setting a default value that can be returned if a first value is not set.
qref: "Returns a default value if a value is not set when checked."
keywords: [defaults]
categories: [functions]
menu:
docs:
parent: functions
toc:
signature: ["default DEFAULT INPUT"]
relatedfuncs: []
keywords: []
namespace: compare
relatedFuncs:
- compare.Conditional
- compare.Default
signature:
- compare.Default DEFAULT INPUT
- default DEFAULT INPUT
---
`default` checks whether a given value is set and returns a default value if it is not. *Set* in this context means different things depending on the data type:
+13 -3
View File
@@ -5,9 +5,19 @@ categories: [functions]
menu:
docs:
parent: functions
keywords: [iteration]
signature: ["delimit COLLECTION DELIMITER [LAST]"]
relatedfuncs: []
keywords: []
namespace: collections
relatedFuncs:
- collections.Apply
- collections.Delimit
- collections.In
- collections.Reverse
- collections.Seq
- collections.Slice
- strings.Split
signature:
- collections.Delimit COLLECTION DELIMITER [LAST]
- delimit COLLECTION DELIMITER [LAST]
---
Delimit a slice:
+11 -3
View File
@@ -5,9 +5,17 @@ categories: [functions]
menu:
docs:
parent: functions
keywords: [collections]
signature: ["dict KEY VALUE [KEY VALUE]..."]
relatedfuncs: []
keywords: []
namespace: collections
relatedFuncs:
- collections.Dictionary
- collections.Group
- collections.Index
- collections.IsSet
- collections.Where
signature:
- collections.Dictionary KEY VALUE [KEY VALUE]...
- dict KEY VALUE [KEY VALUE]...
---
`dict` is especially useful for passing more than one value to a partial template.
+11 -2
View File
@@ -5,8 +5,17 @@ categories: [functions]
menu:
docs:
parent: functions
keywords: [time duration]
signature: ["duration TIME_UNIT DURATION_NUMBER"]
keywords: []
namespace: time
relatedFuncs:
- time.AsTime
- time.Duration
- time.Format
- time.Now
- time.ParseDuration
signature:
- time.Duration TIME_UNIT DURATION_NUMBER
- duration TIME_UNIT DURATION_NUMBER
---
`time.Duration` converts a given number into a [`time.Duration`](https://pkg.go.dev/time#Duration) structure so you can access its fields. E.g. you can perform [time operations](https://pkg.go.dev/time#Duration) on the returned `time.Duration` value:
+25 -3
View File
@@ -6,11 +6,33 @@ menu:
docs:
parent: functions
keywords: []
signature: ["echoParam MAP KEY"]
relatedfuncs: []
namespace: collections
relatedFuncs: []
signature:
- collections.EchoParam COLLECTION KEY
- echoParam COLLECTION KEY
---
For example, consider this site configuration:
{{< code-toggle file=hugo copy=false >}}
[params.footer]
poweredBy = 'Hugo'
{{< /code-toggle >}}
To print the value of `poweredBy`:
```go-html-template
{{ echoParam .Params "project_url" }}
{{ echoParam site.Params.footer "poweredby" }} → Hugo
```
{{% note %}}
When using the `echoParam` function you must reference the key using lower case. See the previous example.
The `echoParam` function may be deprecated in a future release. Instead, use either of the constructs below.
{{% /note %}}
```go-html-template
{{ site.Params.footer.poweredBy }} → Hugo
{{ index site.Params.footer "poweredBy" }} → Hugo
```
+6 -3
View File
@@ -5,9 +5,12 @@ categories: [functions]
menu:
docs:
parent: functions
keywords: [strings,emojis]
signature: ["emojify INPUT"]
relatedfuncs: []
keywords: []
namespace: transform
relatedFuncs: []
signature:
- transform.Emojify INPUT
- emojify INPUT
---
`emojify` runs a passed string through the Emoji emoticons processor.
+12 -3
View File
@@ -5,9 +5,18 @@ categories: [functions]
menu:
docs:
parent: functions
keywords: [comparison,operators,logic]
signature: ["eq ARG1 ARG2 [ARG...]"]
relatedfuncs: []
keywords: []
namespace: compare
relatedFuncs:
- compare.Eq
- compare.Ge
- compare.Gt
- compare.Le
- compare.Lt
- compare.Ne
signature:
- compare.Eq ARG1 ARG2 [ARG...]
- eq ARG1 ARG2 [ARG...]
---
```go-html-template
+18 -32
View File
@@ -1,45 +1,31 @@
---
title: errorf and warnf
description: Log ERROR or WARNING from the templates.
title: errorf
description: Log an ERROR from a template.
categories: [functions]
menu:
docs:
parent: functions
keywords: [strings, log, error]
signature: ["errorf FORMAT INPUT"]
relatedfuncs: [printf]
keywords: []
namespace: fmt
relatedFuncs:
- fmt.Errorf
- fmt.Erroridf
- fmt.Warnf
signature:
- fmt.Errorf FORMAT [INPUT]
- errorf FORMAT [INPUT]
---
`errorf` or `warnf` will evaluate a format string, then output the result to the ERROR or WARNING log (and only once per error message to avoid flooding the log).
The documentation for [Go's fmt package] describes the structure and content of the format string.
Any ERROR will also cause the build to fail (the `hugo` command will `exit -1`).
Both functions return an empty string, so the messages are only printed to the console.
Like the [`printf`] function, the `errorf` function evaluates the format string. It then prints the result to the ERROR log and fails the build. Hugo prints each unique message once to avoid flooding the log with duplicate errors.
```go-html-template
{{ errorf "Failed to handle page %q" .Path }}
{{ errorf "The %q shortcode requires a src parameter. See %s" .Name .Position }}
```
```go-html-template
{{ warnf "You should update the shortcodes in %q" .Path }}
```
Use the [`erroridf`] function to allow optional supression of specific errors.
Note that `errorf`, `erroridf`, and `warnf` support all the formatting verbs of the [fmt](https://pkg.go.dev/fmt) package.
## Suppress errors
Sometimes it may make sense to let the user suppress an ERROR and make the build succeed.
You can do this by using the `erroridf` function. This functions takes an error ID as the first argument.
```go-html-template
{{ erroridf "my-custom-error" "You should consider fixing this." }}
```
This will produce:
```
ERROR 2021/06/07 17:47:38 You should consider fixing this.
If you feel that this should not be logged as an ERROR, you can ignore it by adding this to your site configuration:
ignoreErrors = ["my-custom-error"]
```
[`erroridf`]: /functions/erroridf/
[`printf`]: /functions/printf/
[Go's fmt package]: https://pkg.go.dev/fmt
+46
View File
@@ -0,0 +1,46 @@
---
title: erroridf
description: Log a suppressable ERROR from a template.
categories: [functions]
menu:
docs:
parent: functions
keywords: []
namespace: fmt
relatedFuncs:
- fmt.Errorf
- fmt.Erroridf
- fmt.Warnf
signature:
- fmt.Erroridf ID FORMAT [INPUT]
- erroridf ID FORMAT [INPUT]
---
The documentation for [Go's fmt package] describes the structure and content of the format string.
Like the [`errorf`] function, the `erroridf` function evaluates the format string, prints the result to the ERROR log, then fails the build. Hugo prints each unique message once to avoid flooding the log with duplicate errors.
Unlike the `errorf` function, you may surpress errors logged by the `erroridf` function by adding the messsage ID to the `ignoreErrors` array in your site configuration.
This template code:
```go-html-template
{{ erroridf "error-42" "You should consider fixing this." }}
```
Produces this console log:
```text
ERROR You should consider fixing this.
If you feel that this should not be logged as an ERROR, you can ignore it by adding this to your site config:
ignoreErrors = ["error-42"]
```
To suppress this message:
{{< code-toggle file=hugo copy=false >}}
ignoreErrors = ["error-42"]
{{< /code-toggle >}}
[`errorf`]: /functions/errorf/
[Go's fmt package]: https://pkg.go.dev/fmt
+11 -2
View File
@@ -5,9 +5,18 @@ categories: [functions]
menu:
docs:
parent: functions
signature: ["os.FileExists PATH","fileExists PATH"]
relatedfuncs: ['os.ReadDir','os.ReadFile','os.Stat']
namespace: os
relatedFuncs:
- os.FileExists
- os.Getenv
- os.ReadDir
- os.ReadFile
- os.Stat
signature:
- os.FileExists PATH
- fileExists PATH
---
The `os.FileExists` function attempts to resolve the path relative to the root of your project directory. If a matching file or directory is not found, it will attempt to resolve the path relative to the [`contentDir`](/getting-started/configuration#contentdir). A leading path separator (`/`) is optional.
With this directory structure:
+9 -4
View File
@@ -5,11 +5,16 @@ categories: [functions]
menu:
docs:
parent: functions
keywords: [regex]
keywords: []
namespace: strings
relatedFuncs:
- strings.FindRE
- strings.FindRESubmatch
- strings.Replace
- strings.ReplaceRE
signature:
- "findRE PATTERN INPUT [LIMIT]"
- "strings.FindRE PATTERN INPUT [LIMIT]"
relatedfuncs: [findRESubmatch, replaceRE]
- strings.FindRE PATTERN INPUT [LIMIT]
- findRE PATTERN INPUT [LIMIT]
---
By default, `findRE` finds all matches. You can limit the number of matches with an optional LIMIT argument.
+9 -4
View File
@@ -5,11 +5,16 @@ categories: [functions]
menu:
docs:
parent: functions
keywords: [regex]
keywords: []
namespace: strings
relatedFuncs:
- strings.FindRE
- strings.FindRESubmatch
- strings.Replace
- strings.ReplaceRE
signature:
- "findRESubmatch PATTERN INPUT [LIMIT]"
- "strings.FindRESubmatch PATTERN INPUT [LIMIT]"
relatedfuncs: [findRE, replaceRE]
- strings.FindRESubmatch PATTERN INPUT [LIMIT
- findRESubmatch PATTERN INPUT [LIMIT]
---
By default, `findRESubmatch` finds all matches. You can limit the number of matches with an optional LIMIT argument. A return value of nil indicates no match.
+8 -4
View File
@@ -1,13 +1,17 @@
---
title: first
description: "Slices an array to only the first _N_ elements."
description: Slices an array to the first N elements.
categories: [functions]
menu:
docs:
parent: functions
keywords: [iteration]
signature: ["first LIMIT COLLECTION"]
relatedfuncs: [after,last]
keywords: []
namespace: collections
relatedFuncs:
- collections.After
- collections.First
- collections.Last
signature: [first LIMIT COLLECTION]
---
`first` works in a similar manner to the [`limit` keyword in
+9 -3
View File
@@ -5,9 +5,15 @@ categories: [functions]
menu:
docs:
parent: functions
keywords: [cast,strings,floats]
signature: ["float INPUT"]
relatedfuncs: []
keywords: []
namespace: cast
relatedFuncs:
- cast.ToFloat
- cast.ToInt
- cast.ToString
signature:
- cast.ToFloat INPUT
- float INPUT
---
With a decimal (base 10) input:
+5 -3
View File
@@ -5,9 +5,11 @@ categories: [functions]
menu:
docs:
parent: functions
keywords: [dates,time]
signature: [".Format FORMAT"]
relatedfuncs: [dateFormat,now,Unix,time]
keywords: []
namespace:
relatedFuncs: []
signature:
- .Format FORMAT
toc: true
---
+12 -3
View File
@@ -5,9 +5,18 @@ categories: [functions]
menu:
docs:
parent: functions
keywords: [comparison,operators,logic]
signature: ["ge ARG1 ARG2 [ARG...]"]
relatedfuncs: []
keywords: []
namespace: compare
relatedFuncs:
- compare.Eq
- compare.Ge
- compare.Gt
- compare.Le
- compare.Lt
- compare.Ne
signature:
- compare.Ge ARG1 ARG2 [ARG...]
- ge ARG1 ARG2 [ARG...]
---
```go-html-template
+6 -3
View File
@@ -5,9 +5,12 @@ categories: [functions]
menu:
docs:
parent: functions
keywords: [shortcodes]
signature: [".Get INDEX", ".Get KEY"]
relatedfuncs: []
keywords: []
namespace:
relatedFuncs: []
signature:
- .Get INDEX
- .Get KEY
---
`.Get` is specifically used when creating your own [shortcode template][sc], to access the [positional and named](/templates/shortcode-templates/#positional-vs-named-parameters) parameters passed to it. When used with a numeric INDEX, it queries positional parameters (starting with 0). With a string KEY, it queries named parameters.
+11 -2
View File
@@ -6,9 +6,18 @@ menu:
docs:
parent: functions
keywords: []
signature: ["os.Getenv VARIABLE", "getenv VARIABLE"]
relatedfuncs: []
namespace: os
relatedFuncs:
- os.FileExists
- os.Getenv
- os.ReadDir
- os.ReadFile
- os.Stat
signature:
- os.Getenv VARIABLE
- getenv VARIABLE
---
Examples:
```go-html-template
+19 -10
View File
@@ -5,8 +5,17 @@ categories: [functions]
menu:
docs:
parent: functions
keywords: [collections]
signature: ["PAGES | group KEY"]
keywords: []
namespace: collections
relatedFuncs:
- collections.Dictionary
- collections.Group
- collections.Index
- collections.IsSet
- collections.Where
signature:
- PAGES | collections.Group KEY
- PAGES | group KEY
---
{{< code file="layouts/partials/groups.html" >}}
@@ -14,16 +23,16 @@ signature: ["PAGES | group KEY"]
{{ $old := .Site.RegularPages | last 10 | group "Old" }}
{{ $groups := slice $new $old }}
{{ range $groups }}
<h3>{{ .Key }}{{/* Prints "New", "Old" */}}</h3>
<ul>
<h3>{{ .Key }}{{/* Prints "New", "Old" */}}</h3>
<ul>
{{ range .Pages }}
<li>
<a href="{{ .Permalink }}">{{ .Title }}</a>
<div class="meta">{{ .Date.Format "Mon, Jan 2, 2006" }}</div>
</li>
<li>
<a href="{{ .Permalink }}">{{ .Title }}</a>
<div class="meta">{{ .Date.Format "Mon, Jan 2, 2006" }}</div>
</li>
{{ end }}
</ul>
</ul>
{{ end }}
{{< /code >}}
The page group you get from `group` is of the same type you get from the built-in [group methods](/templates/lists#group-content) in Hugo. The above example can even be [paginated](/templates/pagination/#list-paginator-pages).
The page group you get from `group` is of the same type you get from the built-in [group methods](/templates/lists#group-content) in Hugo. The above example can be [paginated](/templates/pagination/#list-paginator-pages).
+12 -3
View File
@@ -5,9 +5,18 @@ categories: [functions]
menu:
docs:
parent: functions
keywords: [comparison,operators,logic]
signature: ["gt ARG1 ARG2 [ARG...]"]
relatedfuncs: []
keywords: []
namespace: compare
relatedFuncs:
- compare.Eq
- compare.Ge
- compare.Gt
- compare.Le
- compare.Lt
- compare.Ne
signature:
- compare.Gt ARG1 ARG2 [ARG...]
- gt ARG1 ARG2 [ARG...]
---
```go-html-template
+7 -3
View File
@@ -5,9 +5,13 @@ categories: [functions]
menu:
docs:
parent: functions
keywords: [menus]
signature: ["PAGE.HasMenuCurrent MENU MENUENTRY"]
relatedfuncs: ["IsMenuCurrent"]
keywords: []
namespace:
relatedFuncs:
- .HasMenuCurrent
- .IsMenuCurrent
signature:
- PAGE.HasMenuCurrent MENU MENUENTRY
---
`.HasMenuCurrent` is a method in `Page` object returning a _boolean_ value. It
+7 -3
View File
@@ -5,11 +5,15 @@ categories: [functions]
menu:
docs:
parent: functions
keywords: [highlighting,code blocks,syntax]
signature: ["transform.Highlight INPUT LANG [OPTIONS]","highlight INPUT LANG [OPTIONS]"]
relatedfuncs: []
keywords: []
namespace: transform
relatedFuncs: []
signature:
- transform.Highlight INPUT LANG [OPTIONS]
- highlight INPUT LANG [OPTIONS]
toc: true
---
The `highlight` function uses the [Chroma] syntax highlighter, supporting over 200 languages with more than 40 available styles.
## Arguments
+11 -3
View File
@@ -5,9 +5,17 @@ categories: [functions]
menu:
docs:
parent: functions
keywords: [hmac,checksum]
signature: ["crypto.HMAC HASH_TYPE KEY MESSAGE [ENCODING]","hmac HASH_TYPE KEY MESSAGE [ENCODING]" ]
relatedfuncs: [hmac]
keywords: []
namespace: crypto
relatedFuncs:
- crypto.FNV32a
- crypto.HMAC
- crypto.MD5
- crypto.SHA1
- crypto.SHA256
signature:
- crypto.HMAC HASH_TYPE KEY MESSAGE [ENCODING]
- hmac HASH_TYPE KEY MESSAGE [ENCODING]
---
Set the `HASH_TYPE` argument to `md5`, `sha1`, `sha256`, or `sha512`.
+8 -3
View File
@@ -5,9 +5,14 @@ categories: [functions]
menu:
docs:
parent: functions
keywords: [strings, html]
signature: ["htmlEscape INPUT"]
relatedfuncs: [htmlUnescape]
keywords: []
namespace: transform
relatedFuncs:
- transform.HTMLEscape
- transform.HTMLUnescape
signature:
- transform.HTMLEscape INPUT
- htmlEscape INPUT
---
In the result `&` becomes `&amp;` and so on. It escapes only: `<`, `>`, `&`, `'` and `"`.
+7 -2
View File
@@ -6,8 +6,13 @@ menu:
docs:
parent: functions
keywords: []
signature: ["htmlUnescape INPUT"]
relatedfuncs: [htmlEscape]
namespace: transform
relatedFuncs:
- transform.HTMLEscape
- transform.HTMLUnescape
signature:
- transform.HTMLUnescape INPUT
- htmlUnescape INPUT
---
`htmlUnescape` returns the given string with HTML escape codes un-escaped.
+4 -3
View File
@@ -6,9 +6,10 @@ categories: [functions]
menu:
docs:
parent: functions
toc:
signature: ["hugo"]
relatedfuncs: []
keywords: []
namespace: global
relatedFuncs: []
signature: [hugo]
---
`hugo` returns an instance that contains the following functions:
+9 -3
View File
@@ -5,9 +5,15 @@ categories: [functions]
menu:
docs:
parent: functions
keywords: [strings,casing]
signature: ["humanize INPUT"]
relatedfuncs: [anchorize]
keywords: []
namespace: inflect
relatedFuncs:
- inflect.Humanize
- inflect.Pluralize
- inflect.Singularize
signature:
- inflect.Humanize INPUT
- humanize INPUT
---
If the input is either an int64 value or the string representation of an integer, humanize returns the number with the proper ordinal appended.
+102 -26
View File
@@ -1,49 +1,125 @@
---
title: i18n
description: Translates a piece of content based on your i18n configuration files.
description: Translates a string using the translation tables in the i18n directory.
categories: [functions]
menu:
docs:
parent: functions
keywords: [internationalization,i18n,multilingual]
keywords: []
namespace: lang
relatedFuncs: []
signature:
- "i18n KEY"
- "T KEY"
- "lang.Translate KEY"
relatedfuncs: []
- lang.Translate KEY [CONTEXT]
- i18n KEY [CONTEXT]
- T KEY [CONTEXT]
---
This translates a piece of content based on your `i18n/en-US.toml` files. You can use the [go-i18n](https://github.com/nicksnyder/go-i18n) tools to manage your translations. The translations can exist in both the theme and at the root of your repository.
Let's say your multilingual site supports two languages, English and Polish. Create a translation table for each language in the `i18n` directory.
```go-html-template
{{ i18n "translation_id" }}
```
i18n/
├── en.toml
└── pl.toml
```
{{% note %}}
`T` is an alias to `i18n`. E.g. `{{ T "translation_id" }}`.
{{% /note %}}
The translation tables can contain both:
### Query a flexible translation with variables
- Simple translations
- Translations with pluralization
Often you will want to use the page variables in the translation strings. To do so, pass the `.` context when calling `i18n`:
The Unicode [CLDR Plural Rules chart] describes the pluralization categories for each language.
```go-html-template
{{ i18n "wordCount" . }}
```
[CLDR Plural Rules chart]: https://www.unicode.org/cldr/charts/43/supplemental/language_plural_rules.html
The function will pass the `.` context to the `"wordCount"` id:
The English translation table:
{{< code-toggle file="i18n/en-US" >}}
[wordCount]
other = "This article has {{ .WordCount }} words."
{{< code-toggle file=i18n/en copy=false >}}
# simple translations
privacy = 'privacy'
security = 'security'
# translations with pluralization
[day]
one = 'day'
other = 'days'
[day_with_count]
one = '{{ . }} day'
other = '{{ . }} days'
{{< /code-toggle >}}
Assume `.WordCount` in the context has value is 101. The result will be:
The Polish translation table:
```
This article has 101 words.
{{< code-toggle file=i18n/pl copy=false >}}
# simple translations
privacy = 'prywatność'
security = 'bezpieczeństwo'
# translations with pluralization
[day]
one = 'miesiąc'
few = 'miesiące'
many = 'miesięcy'
other = 'miesiąca'
[day_with_count]
one = '{{ . }} miesiąc'
few = '{{ . }} miesiące'
many = '{{ . }} miesięcy'
other = '{{ . }} miesiąca'
{{< /code-toggle >}}
{{% note %}}
The examples below use the `T` alias for brevity.
{{% /note %}}
When viewing the English language site:
```go-html-template
{{ T "privacy" }} --> privacy
{{ T "security" }} --> security
{{ T "day" 0 }} --> days
{{ T "day" 1 }} --> day
{{ T "day" 2 }} --> days
{{ T "day" 5 }} --> days
{{ T "day_with_count" 0 }} --> 0 days
{{ T "day_with_count" 1 }} --> 1 day
{{ T "day_with_count" 2 }} --> 2 days
{{ T "day_with_count" 5 }} --> 5 days
````
When viewing the Polish language site:
```go-html-template
{{ T "privacy" }} --> prywatność
{{ T "security" }} --> bezpieczeństwo
{{ T "day" 0 }} --> miesięcy
{{ T "day" 1 }} --> miesiąc
{{ T "day" 2 }} --> miesiące
{{ T "day" 5 }} --> miesięcy
{{ T "day_with_count" 0 }} --> 0 miesięcy
{{ T "day_with_count" 1 }} --> 1 miesiąc
{{ T "day_with_count" 2 }} --> 2 miesiące
{{ T "day_with_count" 5 }} --> 5 miesięcy
```
For more information about string translations, see [Translation of Strings in Multilingual Mode][multistrings].
In the pluralization examples above, we passed an integer in context (the second argument). You can also pass a map in context, creating a `count` key to control pluralization.
[multistrings]: /content-management/multilingual/#translation-of-strings
Translation table:
{{< code-toggle file=i18n/en copy=false >}}
[age]
one = '{{ .name }} is {{ .count }} year old.'
other = '{{ .name }} is {{ .count }} years old.'
{{< /code-toggle >}}
Template:
```go-html-template
{{ T "age" (dict "name" "Will" "count" 1) }} → Will is 1 year old.
{{ T "age" (dict "name" "John" "count" 3) }} → John is 3 years old.
```
+12 -4
View File
@@ -1,13 +1,21 @@
---
title: in
description: Checks if an element is in an array or slice--or a substring in a string---and returns a boolean.
description: Checks if an element is in an array or slice---or a substring in a string---and returns a boolean.
categories: [functions]
menu:
docs:
parent: functions
keywords: [strings]
signature: ["in SET ITEM"]
relatedfuncs: []
keywords: []
namespace: collections
relatedFuncs:
- strings.Contains
- strings.ContainsAny
- strings.ContainsNonSpace
- strings.HasPrefix
- strings.HasSuffix
signature:
- collections.In SET ITEM
- in SET ITEM
---
The elements supported are strings, integers and floats, although only float64 will match as expected.
+13 -2
View File
@@ -6,8 +6,19 @@ menu:
docs:
parent: functions
keywords: []
signature: ["index COLLECTION INDEXES", "index COLLECTION KEYS"]
relatedfuncs: []
namespace: collections
relatedFuncs:
- collections.Dictionary
- collections.EchoParam
- collections.Group
- collections.Index
- collections.IsSet
- collections.Where
signature:
- collections.Index COLLECTION INDEXES
- collections.Index COLLECTION KEYS
- index COLLECTION INDEXES
- index COLLECTION KEYS
aliases: [/functions/index/]
---
+9 -3
View File
@@ -5,9 +5,15 @@ categories: [functions]
menu:
docs:
parent: functions
keywords: [cast,strings,integers]
signature: ["int INPUT"]
relatedfuncs: []
keywords: []
namespace: cast
relatedFuncs:
- cast.ToFloat
- cast.ToInt
- cast.ToString
signature:
- cast.ToInt INPUT
- int INPUT
---
With a decimal (base 10) input:
+9 -3
View File
@@ -5,9 +5,15 @@ categories: [functions]
menu:
docs:
parent: functions
keywords: [collections,intersect,union,complement,symdiff]
signature: ["intersect SET1 SET2"]
relatedfuncs: []
namespace: collections
relatedFuncs:
- collections.Complement
- collections.Intersect
- collections.SymDiff
- collections.Union
signature:
- collections.Intersect SET1 SET2
- intersect SET1 SET2
---
A useful example is to use it as `AND` filters when combined with where:
+7 -3
View File
@@ -5,9 +5,13 @@ categories: [functions]
menu:
docs:
parent: functions
keywords: [menus]
signature: ["PAGE.IsMenuCurrent MENU MENUENTRY"]
relatedfuncs: ["HasMenuCurrent"]
keywords: []
namespace:
relatedFuncs:
- .HasMenuCurrent
- .IsMenuCurrent
signature:
- PAGE.IsMenuCurrent MENU MENUENTRY
---
`.IsMenuCurrent` is a method in `Page` object returning a _boolean_ value. It
+35 -7
View File
@@ -1,22 +1,50 @@
---
title: isset
description: Returns true if the parameter is set.
description: Returns true if the key exists within the collection.
categories: [functions]
menu:
docs:
parent: functions
keywords: []
signature: ["isset COLLECTION INDEX", "isset COLLECTION KEY"]
relatedfuncs: []
namespace: collections
relatedFuncs:
- collections.Dictionary
- collections.Group
- collections.Index
- collections.IsSet
- collections.Where
signature:
- collections.IsSet COLLECTION KEY
- isset COLLECTION KEY
---
Takes either a slice, array, or channel and an index or a map and a key as input.
For example, consider this site configuration:
{{< code-toggle file=hugo copy=false >}}
[params]
showHeroImage = false
{{< /code-toggle >}}
It the value of `showHeroImage` is `true`, we can detect that it exists using either `if` or `with`:
```go-html-template
{{ if isset .Params "project_url" }} {{ index .Params "project_url" }}{{ end }}
{{ if site.Params.showHeroImage }}
{{ site.Params.showHeroImage }} → true
{{ end }}
{{ with site.Params.showHeroImage }}
{{ . }} → true
{{ end }}
```
But if the value of `showHeroImage` is `false`, we can't use either `if` or `with` to detect its existence. In this case, you must use the `isset` function:
```go-html-template
{{ if isset site.Params "showheroimage" }}
<p>The showHeroImage parameter is set to {{ site.Params.showHeroImage }}.<p>
{{ end }}
```
{{% note %}}
All site-level configuration keys are stored as lower case. Therefore, a `myParam` key-value set in your [site configuration file](/getting-started/configuration/) needs to be accessed with `{{ if isset .Site.Params "myparam" }}` and *not* with `{{ if isset .Site.Params "myParam" }}`. Note that you can still access the same configuration key with `.Site.Params.myParam` *or* `.Site.Params.myparam`, for example, when using [`with`](/functions/with).
This restriction also applies when accessing page-level front matter keys from within [shortcodes](/content-management/shortcodes/).
When using the `isset` function you must reference the key using lower case. See the previous example.
{{% /note %}}
+11 -3
View File
@@ -5,9 +5,17 @@ categories: [functions]
menu:
docs:
parent: functions
keywords: [strings,json]
signature: ["jsonify INPUT", "jsonify OPTIONS INPUT"]
relatedfuncs: [plainify]
keywords: []
namespace: encoding
relatedFuncs:
- encoding.Jsonify
- transform.Remarshal
- transform.Unmarshal
signature:
- encoding.Jsonify INPUT
- encoding.Jsonify OPTIONS INPUT
- jsonify INPUT
- jsonify OPTIONS INPUT
---
To customize the printing of the JSON, pass a map of options as the first
+6 -5
View File
@@ -1,14 +1,15 @@
---
title: lang.Merge
description: "Merge missing translations from other languages."
description: Merge missing translations from other languages.
categories: [functions]
keywords: [multilingual]
menu:
docs:
parent: functions
signature: ["lang.Merge FROM TO"]
relatedfuncs: []
comments:
keywords: []
namespace: lang
relatedFuncs: []
signature:
- lang.Merge FROM TO
---
As an example:
+4 -3
View File
@@ -1,13 +1,14 @@
---
title: lang
package: lang
description: "TODO.."
description: "Functions in the language namespace."
categories: [functions]
keywords: [numbers]
menu:
docs:
parent: functions
signature: ["lang.NumFmt PRECISION NUMBER [OPTIONS [DELIMITER]]"]
keywords: []
relatedFuncs: []
signature: []
aliases: ['/functions/numfmt/']
type: 'template-func'
---
+8 -5
View File
@@ -1,14 +1,17 @@
---
title: last
description: "slices an array to only the last <em>N</em>th elements."
keywords: []
description: Slices an array to the last N elements.
categories: [functions]
menu:
docs:
parent: functions
toc:
signature: ["last INDEX COLLECTION"]
relatedfuncs: []
keywords: []
namespace: collections
relatedFuncs:
- collections.After
- collections.First
- collections.Last
signature: [last INDEX COLLECTION]
---
```go-html-template
+12 -3
View File
@@ -5,9 +5,18 @@ categories: [functions]
menu:
docs:
parent: functions
keywords: [comparison,operators,logic]
signature: ["le ARG1 ARG2 [ARG...]"]
relatedfuncs: []
keywords: []
namespace: compare
relatedFuncs:
- compare.Eq
- compare.Ge
- compare.Gt
- compare.Le
- compare.Lt
- compare.Ne
signature:
- compare.Le ARG1 ARG2 [ARG...]
- le ARG1 ARG2 [ARG...]
---
```go-html-template
+8 -2
View File
@@ -5,9 +5,15 @@ categories: [functions]
menu:
docs:
parent: functions
keywords: [length]
keywords: []
namespace: text/template
relatedFuncs:
- len
- strings.Count
- strings.CountRunes
- strings.CountWords
- strings.RuneCount
signature: ["len INPUT"]
relatedfuncs: []
---
With a string:
+9 -4
View File
@@ -5,11 +5,16 @@ categories: [functions]
menu:
docs:
parent: functions
keywords: [strings,casing]
keywords: []
namespace: strings
relatedFuncs:
- strings.FirstUpper
- strings.Title
- strings.ToLower
- strings.ToUpper
signature:
- "lower INPUT"
- "strings.ToLower INPUT"
relatedfuncs: []
- strings.ToLower INPUT
- lower INPUT
---
+12 -3
View File
@@ -5,9 +5,18 @@ categories: [functions]
menu:
docs:
parent: functions
keywords: [comparison,operators,logic]
signature: ["lt ARG1 ARG2 [ARG...]"]
relatedfuncs: []
keywords: []
namespace: compare
relatedFuncs:
- compare.Eq
- compare.Ge
- compare.Gt
- compare.Le
- compare.Lt
- compare.Ne
signature:
- compare.Lt ARG1 ARG2 [ARG...]
- lt ARG1 ARG2 [ARG...]
---
```go-html-template
+6 -3
View File
@@ -1,13 +1,16 @@
---
title: markdownify
description: Renders markdown to HTML.
keywords: [markdown,content]
categories: [functions]
menu:
docs:
parent: functions
signature: ["markdownify INPUT"]
relatedfuncs: []
keywords: []
namespace: transform
relatedFuncs: []
signature:
- transform.Markdownify INPUT
- markdownify INPUT
---
```go-html-template
+3 -4
View File
@@ -1,14 +1,13 @@
---
title: Math
title: math
description: Hugo provides mathematical operators in templates.
keywords: [math, operators]
categories: [functions]
menu:
docs:
parent: functions
toc:
keywords: []
relatedFuncs: []
signature: []
relatedfuncs: []
---
| Function | Description | Example |
+13 -4
View File
@@ -6,13 +6,22 @@ menu:
docs:
parent: functions
keywords: []
signature: ["md5 INPUT"]
relatedfuncs: [sha]
namespace: crypto
relatedFuncs:
- crypto.FNV32a
- crypto.HMAC
- crypto.MD5
- crypto.SHA1
- crypto.SHA256
signature:
- crypto.MD5 INPUT
- md5 INPUT
---
```go-html-template
{{ md5 "Hello world, gophers!" }}
<!-- returns the string "b3029f756f98f79e7f1b7f1d1f0dd53b" -->
{{ md5 "Hello world" }} → 3e25960a79dbc69b674cd4ec67a72c62
```
This can be useful if you want to use [Gravatar](https://en.gravatar.com/) for generating a unique avatar:
+8 -3
View File
@@ -5,9 +5,14 @@ categories: [functions]
menu:
docs:
parent: functions
keywords: [collections]
signature: ["collections.Merge MAP MAP...", "merge MAP MAP..."]
relatedfuncs: [dict, append, reflect.IsMap, reflect.IsSlice]
keywords: []
namespace: collections
relatedFuncs:
- collections.Append
- collections.Merge
signature:
- collections.Merge MAP MAP...
- merge MAP MAP...
---
Returns the result of merging two or more maps from left to right. If a key already exists, `merge` updates its value. If a key is absent, `merge` inserts the value under the new key.
+11 -3
View File
@@ -5,9 +5,17 @@ categories: [functions]
menu:
docs:
parent: functions
keywords: [comparison,operators,logic]
signature: ["ne ARG1 ARG2 [ARG...]"]
relatedfuncs: []
namespace: compare
relatedFuncs:
- compare.Eq
- compare.Ge
- compare.Gt
- compare.Le
- compare.Lt
- compare.Ne
signature:
- compare.Ne ARG1 ARG2 [ARG...]
- ne ARG1 ARG2 [ARG...]
---
```go-html-template
+12 -3
View File
@@ -5,9 +5,18 @@ categories: [functions]
menu:
docs:
parent: functions
keywords: [dates,time]
signature: ["now"]
relatedfuncs: [Unix,dateFormat]
keywords: []
namespace: time
relatedFuncs:
- time.AsTime
- time.Duration
- time.Format
- time.Now
- time.ParseDuration
signature:
- time.Now
- now
---
See [`time.Time`](https://godoc.org/time#Time).
+11 -3
View File
@@ -5,10 +5,18 @@ categories: [functions]
menu:
docs:
parent: functions
keywords: [files]
signature: ["os.Stat PATH"]
relatedfuncs: ['os.FileExists','os.ReadDir','os.ReadFile']
keywords: []
namespace: os
relatedFuncs:
- os.FileExists
- os.Getenv
- os.ReadDir
- os.ReadFile
- os.Stat
signature:
- os.Stat PATH
---
The `os.Stat` function attempts to resolve the path relative to the root of your project directory. If a matching file or directory is not found, it will attempt to resolve the path relative to the [`contentDir`](/getting-started/configuration#contentdir). A leading path separator (`/`) is optional.
```go-html-template
+105
View File
@@ -0,0 +1,105 @@
---
title: page
description: Provides global access to the .Page object.
categories: [functions]
menu:
docs:
parent: functions
keywords: []
namespace: global
relatedFuncs:
- page
- site
signature: [page]
---
At the top level of a template that receives the `Page` object in context, these are equivalent:
```go-html-template
{{ .Params.foo }}
{{ .Page.Params.foo }}
{{ page.Params.foo }}
```
When the `Page` object is not in context, you can use the global `page` function:
```go-html-template
{{ page.Params.foo }}
```
{{% note %}}
Do not use the global `page` function in shortcodes, partials called by shortcodes, or cached partials. See [warnings](#warnings) below.
{{% /note %}}
## Explanation
Hugo almost always passes a `Page` as the data context into the top level template (e.g., `single.html`). The one exception is the multihost sitemap template. This means that you can access the current page with the `.` variable in the template.
But when you are deeply nested inside of a [content view], [partial], or [render hook], it isn't always practical or possible to access the `Page` object.
Use the global `page` function to access the `Page` object from anywhere in any template.
## Warnings
### Be aware of top-level context
The global `page` function accesses the `Page` object passed into the top-level template.
With this content structure:
```text
content/
├── posts/
│ ├── post-1.md
│ ├── post-2.md
│ └── post-3.md
└── _index.md <-- title is "My Home Page"
```
And this code in the home page template:
```go-html-template
{{ range site.Sections }}
{{ range .Pages }}
{{ page.Title }}
{{ end }}
{{ end }}
```
The rendered output will be:
```text
My Home Page
My Home Page
My Home Page
```
In the example above, the global `page` function accesses the `Page` object passed into the home page template; it does not access the `Page` object of the iterated pages.
### Be aware of caching
Do not use the global `page` function in:
- Shortcodes
- Partials called by shortcodes
- Partials cached by the `partialCached` function
Hugo caches rendered shortcodes. If you use the `global` page function within a shortcode, and the page content is rendered in two or more templates, the cached shortcodes may be incorrect.
Consider this section template:
```go-html-template
{{ range .Pages }}
<h2><a href="{{ .RelPermalink }}">{{ .LinkTitle }}</a></h2>
{{ .Summary }}
{{ end }}
```
When you call the `.Summary` method, Hugo renders the page `.Content` including shortcodes. In this case, within a shortcode, the global `page` function accesses the `Page` object of the section page, not the content page.
If Hugo renders the section page before a content page, the cached rendered shortcode will be incorrect. You cannot control the rendering sequence due to concurrency.
[content view]: /getting-started/glossary/#content-view
[partial]: /getting-started/glossary/#partial
[render hook]: /getting-started/glossary/#render-hook
[shortcode]: getting-started/glossary/#shortcode
+6 -2
View File
@@ -1,12 +1,16 @@
---
title: .Param
description: Returns a page parameter, falling back to a site parameter if present.
signature: ['.Param KEY']
categories: [functions]
keywords: ['front matter', 'params']
menu:
docs:
parent: functions
keywords: []
namespace:
relatedFuncs: []
signature:
- .Param KEY
---
The `.Param` method on `.Page` looks for the given `KEY` in page parameters, and returns the corresponding value. If it cannot find the `KEY` in page parameters, it looks for the `KEY` in site parameters. If it cannot find the `KEY` in either location, the `.Param` method returns `nil`.
+65
View File
@@ -0,0 +1,65 @@
---
title: partial
description: Executes the named partial template. If the partial contains a return statement, returns that value, else returns the rendered output.
categories: [functions]
menu:
docs:
parent: functions
keywords: []
namespace: partials
relatedFuncs:
- partials.Include
- partials.IncludeCached
signature:
- partials.Include LAYOUT [CONTEXT]
- partial LAYOUT [CONTEXT]
---
In this example we have three partial templates:
```text
layouts/
└── partials/
├── average.html
├── breadcrumbs.html
└── footer.html
```
The "average" partial returns the average of one or more numbers. We pass the numbers in context:
```go-html-template
{{ $numbers := slice 1 6 7 42 }}
{{ $average := partial "average.html" $numbers }}
```
The "breadcrumbs" partial renders [breadcrumb navigation], and needs to receive the current page in context:
```go-html-template
{{ partial "breadcrumbs.html" . }}
```
The "footer" partial renders the site footer. In this contrived example, the footer does not need access to the current page, so we can omit context:
```go-html-template
{{ partial "breadcrumbs.html" }}
```
You can pass anything in context: a page, a page collection, a scalar value, a slice, or a map. For example:
```go-html-template
{{ $student := dict
"name" "John Doe"
"major" "Finance"
"gpa" 4.0
}}
{{ partial "render-student-info.html" $student }}
```
Then, within the partial template:
```go-html-template
<p>{{ .name }} is majoring in {{ .major }}. Their grade point average is {{ .gpa }}.</p>
```
[breadcrumb navigation]: /content-management/sections/#ancestors-and-descendants
+8 -3
View File
@@ -5,9 +5,14 @@ categories: [functions]
menu:
docs:
parent: functions
keywords: [performance]
signature: ["partialCached LAYOUT INPUT [VARIANT...]"]
relatedfuncs: []
keywords: []
namespace: partials
relatedFuncs:
- partials.Include
- partials.IncludeCached
signature:
- partials.IncludeCached LAYOUT CONTEXT [VARIANT...]
- partialCached LAYOUT CONTEXT [VARIANT...]
---
The `partialCached` template function can offer significant performance gains for complex templates that don't need to be re-rendered on every invocation.
+12 -3
View File
@@ -5,9 +5,18 @@ categories: [functions]
menu:
docs:
parent: functions
keywords: [path, base]
signature: ["path.Base PATH"]
relatedfuncs: [path.BaseName, path.Clean, path.Dir, path.Ext, path.Join, path.Split]
keywords: []
namespace: path
relatedFuncs:
- path.Base
- path.BaseName
- path.Clean
- path.Dir
- path.Ext
- path.Join
- path.Split
signature:
- path.Base PATH
---
`path.Base` returns the last element of `PATH`.
+12 -3
View File
@@ -5,9 +5,18 @@ categories: [functions]
menu:
docs:
parent: functions
keywords: [path, base]
signature: ["path.BaseName PATH"]
relatedfuncs: [path.Base, path.Clean, path.Dir, path.Ext, path.Join, path.Split]
keywords: []
namespace: path
relatedFuncs:
- path.Base
- path.BaseName
- path.Clean
- path.Dir
- path.Ext
- path.Join
- path.Split
signature:
- path.BaseName PATH
---
If `PATH` is empty, `.` is returned.
+12 -3
View File
@@ -5,9 +5,18 @@ categories: [functions]
menu:
docs:
parent: functions
keywords: [path, clean]
signature: ["path.Clean PATH"]
relatedfuncs: [path.Base, path.BaseName, path.Dir, path.Ext, path.Join, path.Split]
keywords: []
namespace: path
relatedFuncs:
- path.Base
- path.BaseName
- path.Clean
- path.Dir
- path.Ext
- path.Join
- path.Split
signature:
- path.Clean PATH
---
`path.Clean` replaces path separators with slashes (`/`) and removes extraneous separators, including trailing separators.
+12 -3
View File
@@ -5,9 +5,18 @@ categories: [functions]
menu:
docs:
parent: functions
keywords: [path, dir]
signature: ["path.Dir PATH"]
relatedfuncs: [path.Base, path.BaseName, path.Clean, path.Ext, path.Join, path.Split]
keywords: []
namespace: path
relatedFuncs:
- path.Base
- path.BaseName
- path.Clean
- path.Dir
- path.Ext
- path.Join
- path.Split
signature:
- path.Dir PATH
---
`path.Dir` returns all but the last element of `PATH`, typically `PATH`'s directory.
+12 -3
View File
@@ -5,9 +5,18 @@ categories: [functions]
menu:
docs:
parent: functions
keywords: [path, ext, extension]
signature: ["path.Ext PATH"]
relatedfuncs: [path.Base, path.BaseName, path.Clean, path.Dir, path.Join, path.Split]
keywords: []
namespace: path
relatedFuncs:
- path.Base
- path.BaseName
- path.Clean
- path.Dir
- path.Ext
- path.Join
- path.Split
signature:
- path.Ext PATH
---
`path.Ext` returns the file name extension `PATH`.
+13 -3
View File
@@ -5,9 +5,19 @@ categories: [functions]
menu:
docs:
parent: functions
keywords: [path, join]
signature: ["path.Join ELEMENT..."]
relatedfuncs: [path.Base, path.BaseName, path.Clean, path.Dir, path.Ext, path.Split]
keywords: []
namespace: path
relatedFuncs:
- path.Base
- path.BaseName
- path.Clean
- path.Dir
- path.Ext
- path.Join
- path.Split
- urls.JoinPath
signature:
- path.Join ELEMENT...
---
`path.Join` joins path elements into a single path, adding a separating slash if necessary.
+12 -3
View File
@@ -5,9 +5,18 @@ categories: [functions]
menu:
docs:
parent: functions
keywords: [path, split]
signature: ["path.Split PATH"]
relatedfuncs: [path.Base, path.BaseName, path.Clean, path.Dir, path.Ext, path.Join]
keywords: []
namespace: path
relatedFuncs:
- path.Base
- path.BaseName
- path.Clean
- path.Dir
- path.Ext
- path.Join
- path.Split
signature:
- path.Split PATH
---
`path.Split` splits `PATH` immediately following the final slash, separating it into a directory and a base component.
+6 -3
View File
@@ -5,9 +5,12 @@ categories: [functions]
menu:
docs:
parent: functions
keywords: [strings]
signature: ["plainify INPUT"]
relatedfuncs: [jsonify]
keywords: []
namespace: transform
relatedFuncs: []
signature:
- transform.Plainify
- plainify INPUT
---
```go-html-template
+9 -3
View File
@@ -5,9 +5,15 @@ categories: [functions]
menu:
docs:
parent: functions
keywords: [strings]
signature: ["pluralize INPUT"]
relatedfuncs: []
keywords: []
namespace: inflect
relatedFuncs:
- inflect.Humanize
- inflect.Pluralize
- inflect.Singularize
signature:
- inflect.Pluralize INPUT
- pluralize INPUT
---
```go-html-template
+9 -5
View File
@@ -5,13 +5,17 @@ categories: [functions]
menu:
docs:
parent: functions
keywords: [strings]
signature: ["print INPUT"]
relatedfuncs: []
keywords: []
namespace: fmt
relatedFuncs:
- fmt.Print
- fmt.Printf
- fmt.Println
signature:
- fmt.Print INPUT
- print INPUT
---
See [the go doc](https://golang.org/pkg/fmt/) for additional information.
```go-html-template
{{ print "foo" }} → "foo"
{{ print "foo" "bar" }} → "foobar"
+29 -6
View File
@@ -5,17 +5,40 @@ categories: [functions]
menu:
docs:
parent: functions
keywords: [strings]
signature: ["printf FORMAT INPUT"]
relatedfuncs: []
keywords: []
namespace: fmt
relatedFuncs:
- fmt.Print
- fmt.Printf
- fmt.Println
signature:
- fmt.Printf FORMAT [INPUT]
- printf FORMAT [INPUT]
---
See [the go doc](https://golang.org/pkg/fmt/) for additional information.
The documentation for [Go's fmt package] describes the structure and content of the format string.
[Go's fmt package]: https://pkg.go.dev/fmt
```go-html-template
{{ i18n ( printf "combined_%s" $var ) }}
{{ $var := "world" }}
{{ printf "Hello %s." $var }} → Hello world.
```
```go-html-template
{{ printf "formatted %.2f" 3.1416 }}
{{ $pi := 3.14159265 }}
{{ printf "Pi is approximately %.2f." $pi }} → 3.14
```
Use the `printf` function with the `safeHTMLAttr` function:
```go-html-template
{{ $desc := "Eat at Joe's" }}
<meta name="description" {{ printf "content=%q" $desc | safeHTMLAttr }}>
```
Hugo renders this to:
```html
<meta name="description" content="Eat at Joe's">
```
+9 -5
View File
@@ -5,13 +5,17 @@ categories: [functions]
menu:
docs:
parent: functions
keywords: [strings]
signature: ["println INPUT"]
relatedfuncs: []
keywords: []
namespace: fmt
relatedFuncs:
- fmt.Print
- fmt.Printf
- fmt.Println
signature:
- fmt.Println INPUT
- println INPUT
---
See [the go doc](https://golang.org/pkg/fmt/) for additional information. `\n` denotes the linebreak but isn't printed in the templates as seen below:
```go-html-template
{{ println "foo" }} → "foo\n"
```
+10 -3
View File
@@ -5,9 +5,16 @@ categories: [functions]
menu:
docs:
parent: functions
keywords: [urls]
signature: ["querify KEY VALUE [KEY VALUE]...", "querify COLLECTION"]
relatedfuncs: []
keywords: []
namespace: collections
relatedFuncs:
- collections.Querify
- urlquery
signature:
- collections.Querify KEY VALUE [KEY VALUE]...
- collections.Querify COLLECTION
- querify KEY VALUE [KEY VALUE]...
- querify COLLECTION
---
`querify` takes a set or slice of key-value pairs and returns a [query string](https://en.wikipedia.org/wiki/Query_string) that can be appended to a URL.
+7 -3
View File
@@ -5,9 +5,13 @@ categories: [functions]
menu:
docs:
parent: functions
keywords: [iteration]
signature: ["range COLLECTION"]
relatedfuncs: []
keywords: []
namespace: text/template
relatedFuncs:
- with
- range
signature:
- range COLLECTION
---
Just like in the Go programming language, Go and Hugo templates make heavy use of `range` to iterate over a map, array or slice. Other templating languages use a foreach for the equivalent functionality.
+12 -3
View File
@@ -5,10 +5,19 @@ categories: [functions]
menu:
docs:
parent: functions
keywords: [files]
signature: ["os.ReadDir PATH", "readDir PATH"]
relatedfuncs: ['os.FileExists','os.ReadFile','os.Stat']
keywords: []
namespace: os
relatedFuncs:
- os.FileExists
- os.Getenv
- os.ReadDir
- os.ReadFile
- os.Stat
signature:
- os.ReadDir PATH
- readDir PATH
---
The `os.ReadDir` function resolves the path relative to the root of your project directory. A leading path separator (`/`) is optional.
With this directory structure:
+12 -3
View File
@@ -5,10 +5,19 @@ categories: [functions]
menu:
docs:
parent: functions
keywords: [files]
signature: ["os.ReadFile PATH", "readFile PATH"]
relatedfuncs: ['os.FileExists','os.ReadDir','os.Stat']
keywords: []
namespace: os
relatedFuncs:
- os.FileExists
- os.Getenv
- os.ReadDir
- os.ReadFile
- os.Stat
signature:
- os.ReadFile PATH
- readFile PATH
---
The `os.ReadFile` function attempts to resolve the path relative to the root of your project directory. If a matching file is not found, it will attempt to resolve the path relative to the [`contentDir`](/getting-started/configuration#contentdir). A leading path separator (`/`) is optional.
With a file named README.md in the root of your project directory:
+8 -3
View File
@@ -5,9 +5,14 @@ categories: [functions]
menu:
docs:
parent: functions
keywords: [cross references, anchors]
signature: ["ref . PAGE"]
relatedfuncs: [relref]
keywords: []
namespace: urls
relatedFuncs:
- urls.Ref
- urls.RelRef
signature:
- urls.Ref . PAGE
- ref . PAGE
---
This function takes two arguments:
+7 -3
View File
@@ -5,9 +5,13 @@ categories: [functions]
menu:
docs:
parent: functions
keywords: [reflect, reflection, kind]
signature: ["reflect.IsMap INPUT"]
relatedfuncs: [reflect.IsSlice]
keywords: []
namespace: reflect
relatedFuncs:
- reflect.IsMap
- reflect.IsSlice
signature:
- reflect.IsMap INPUT
---
`reflect.IsMap` reports if `VALUE` is a map. Returns a boolean.
+8 -3
View File
@@ -5,9 +5,14 @@ categories: [functions]
menu:
docs:
parent: functions
keywords: [reflect, reflection, kind]
signature: ["reflect.IsSlice INPUT"]
relatedfuncs: [reflect.IsMap]
keywords: []
namespace: reflect
relatedFuncs:
- reflect.IsMap
- reflect.IsSlice
signature:
- reflect.IsSlice INPUT
---
`reflect.IsSlice` reports if `VALUE` is a slice. Returns a boolean.
+9 -3
View File
@@ -5,8 +5,14 @@ categories: [functions]
menu:
docs:
parent: functions
keywords: [urls, multilingual,i18n]
signature: ["relLangURL INPUT"]
keywords: []
namespace: urls
relatedFuncs:
- urls.AbsLangURL
- urls.AbsURL
- urls.RelLangURL
- urls.RelURL
signature: [relLangURL INPUT]
---
Use this function with both monolingual and multilingual configurations. The URL returned by this function depends on:
@@ -37,7 +43,7 @@ With `baseURL = https://example.org/docs/`
{{ relLangURL "style.css" }} → /docs/en/style.css
```
### Input begins with a slash
#### Input begins with a slash
If the input begins with a slash, the resulting URL will be incorrect when the `baseURL` includes a subdirectory. With a leading slash, the function returns a URL relative to the protocol+host section of the `baseURL`.
+8 -3
View File
@@ -5,9 +5,14 @@ categories: [functions]
menu:
docs:
parent: functions
keywords: [cross references, anchors]
signature: ["relref . PAGE"]
relatedfuncs: [ref]
keywords: []
namespace: urls
relatedFuncs:
- urls.Ref
- urls.RelRef
signature:
- urls.RelRef . PAGE
- relref . PAGE
---
This function takes two arguments:
+9 -3
View File
@@ -5,8 +5,14 @@ categories: [functions]
menu:
docs:
parent: functions
keywords: [urls]
signature: ["relURL INPUT"]
keywords: []
namespace: urls
relatedFuncs:
- urls.AbsLangURL
- urls.AbsURL
- urls.RelLangURL
- urls.RelURL
signature: [relURL INPUT]
---
With multilingual configurations, use the [`relLangURL`] function instead. The URL returned by this function depends on:
@@ -34,7 +40,7 @@ With `baseURL = https://example.org/docs/`
{{ relURL "style.css" }} → /docs/style.css
```
### Input begins with a slash
#### Input begins with a slash
If the input begins with a slash, the resulting URL will be incorrect when the `baseURL` includes a subdirectory. With a leading slash, the function returns a URL relative to the protocol+host section of the `baseURL`.
+5 -3
View File
@@ -5,9 +5,11 @@ categories: [functions]
menu:
docs:
parent: functions
keywords: [views]
signature: [".Render LAYOUT"]
relatedfuncs: []
keywords: []
namespace:
relatedFuncs: []
signature:
- .Render LAYOUT
---
The view is an alternative layout and should be a file name that points to a template in one of the locations specified in the documentation for [Content Views](/templates/views).
+9 -4
View File
@@ -5,11 +5,16 @@ categories: [functions]
menu:
docs:
parent: functions
keywords: [replace]
keywords: []
namespace: strings
relatedFuncs:
- strings.FindRE
- strings.FindRESubmatch
- strings.Replace
- strings.ReplaceRE
signature:
- "replace INPUT OLD NEW [LIMIT]"
- "strings.Replace INPUT OLD NEW [LIMIT]"
relatedfuncs: [replaceRE]
- strings.Replace INPUT OLD NEW [LIMIT]
- replace INPUT OLD NEW [LIMIT]
---
Replace returns a copy of `INPUT` with all occurrences of `OLD` replaced with `NEW`.
+9 -4
View File
@@ -5,11 +5,16 @@ categories: [functions]
menu:
docs:
parent: functions
keywords: [regex]
keywords: []
namespace: strings
relatedFuncs:
- strings.FindRE
- strings.FindRESubmatch
- strings.Replace
- strings.ReplaceRE
signature:
- "replaceRE PATTERN REPLACEMENT INPUT [LIMIT]"
- "strings.ReplaceRE PATTERN REPLACEMENT INPUT [LIMIT]"
relatedfuncs: [findRE, FindRESubmatch, replace]
- strings.ReplaceRE PATTERN REPLACEMENT INPUT [LIMIT]
- replaceRE PATTERN REPLACEMENT INPUT [LIMIT]
---
By default, `replaceRE` replaces all matches. You can limit the number of matches with an optional LIMIT argument.
+11 -3
View File
@@ -5,9 +5,17 @@ categories: [functions]
menu:
docs:
parent: functions
keywords: [style,css,strings]
signature: ["safeCSS INPUT"]
relatedfuncs: [safeHTML,safeHTMLAttr,]
keywords: []
namespace: safe
relatedFuncs:
- safe.CSS
- safe.HTML
- safe.HTMLAttr
- safe.JS
- safe.URL
signature:
- safe.CSS INPUT
- safeCSS INPUT
---
In this context, *safe* means CSS content that matches any of the following:
+12 -3
View File
@@ -5,9 +5,18 @@ categories: [functions]
menu:
docs:
parent: functions
keywords: [strings]
signature: ["safeHTML INPUT"]
relatedfuncs: []
keywords: []
namespace: safe
relatedFuncs:
- safe.CSS
- safe.HTML
- safe.HTMLAttr
- safe.JS
- safe.URL
signature:
- safe.HTML INPUT
- safeHTML INPUT
---
It should not be used for HTML from a third-party, or HTML with unclosed tags or comments.

Some files were not shown because too many files have changed in this diff Show More