summaryrefslogtreecommitdiffstats
path: root/docs/content/en/functions
diff options
context:
space:
mode:
authorBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>2022-11-17 16:16:19 +0100
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>2022-11-17 16:16:19 +0100
commitf04cc581e195bb13803c9e18ca3a3865219e17d4 (patch)
tree04a8a239f75fd81c8ac7a792d8fb96f401bfa651 /docs/content/en/functions
parentcdd83bf3c885c16dc7976ed3697e600029f82bab (diff)
parent00c4484c7092181729f6f470805bc7d72e8ad17b (diff)
Diffstat (limited to 'docs/content/en/functions')
-rw-r--r--docs/content/en/functions/GetPage.md2
-rw-r--r--docs/content/en/functions/RenderString.md5
-rw-r--r--docs/content/en/functions/abslangurl.md64
-rw-r--r--docs/content/en/functions/absurl.md75
-rw-r--r--docs/content/en/functions/complement.md77
-rw-r--r--docs/content/en/functions/dateformat.md6
-rw-r--r--docs/content/en/functions/dict.md12
-rw-r--r--docs/content/en/functions/errorf.md2
-rw-r--r--docs/content/en/functions/findRe.md46
-rw-r--r--docs/content/en/functions/format.md2
-rw-r--r--docs/content/en/functions/get.md5
-rw-r--r--docs/content/en/functions/getenv.md3
-rw-r--r--docs/content/en/functions/haschildren.md22
-rw-r--r--docs/content/en/functions/hasmenucurrent.md2
-rw-r--r--docs/content/en/functions/highlight.md4
-rw-r--r--docs/content/en/functions/hugo.md4
-rw-r--r--docs/content/en/functions/images/index.md4
-rw-r--r--docs/content/en/functions/int.md2
-rw-r--r--docs/content/en/functions/intersect.md2
-rw-r--r--docs/content/en/functions/jsonify.md2
-rw-r--r--docs/content/en/functions/markdownify.md5
-rw-r--r--docs/content/en/functions/md5.md2
-rw-r--r--docs/content/en/functions/partialCached.md2
-rw-r--r--docs/content/en/functions/ref.md2
-rw-r--r--docs/content/en/functions/relLangURL.md65
-rw-r--r--docs/content/en/functions/relref.md2
-rw-r--r--docs/content/en/functions/relurl.md74
-rw-r--r--docs/content/en/functions/replacere.md51
-rw-r--r--docs/content/en/functions/safeHTML.md2
-rw-r--r--docs/content/en/functions/safeHTMLAttr.md42
-rw-r--r--docs/content/en/functions/scratch.md6
-rw-r--r--docs/content/en/functions/split.md17
-rw-r--r--docs/content/en/functions/strings.Count.md2
-rw-r--r--docs/content/en/functions/strings.HasSuffix.md2
-rw-r--r--docs/content/en/functions/symdiff.md5
-rw-r--r--docs/content/en/functions/upper.md2
-rw-r--r--docs/content/en/functions/urlize.md2
-rw-r--r--docs/content/en/functions/urlquery.md33
-rw-r--r--docs/content/en/functions/urls.Parse.md4
-rw-r--r--docs/content/en/functions/where.md4
40 files changed, 415 insertions, 250 deletions
diff --git a/docs/content/en/functions/GetPage.md b/docs/content/en/functions/GetPage.md
index aa5e9323f..0c74e628b 100644
--- a/docs/content/en/functions/GetPage.md
+++ b/docs/content/en/functions/GetPage.md
@@ -46,7 +46,7 @@ And since `Page` also provides a `.GetPage` method, the above is the same as:
## .GetPage and Multilingual Sites
-The previous examples have used the full content filename to lookup the post. Depending on how you have organized your content (whether you have the language code in the file name or not, e.g. `my-post.en.md`), you may want to do the lookup without extension. This will get you the current language's version of the page:
+The previous examples have used the full content filename to look up the post. Depending on how you have organized your content (whether you have the language code in the file name or not, e.g. `my-post.en.md`), you may want to do the lookup without extension. This will get you the current language's version of the page:
```go-html-template
{{ with .Site.GetPage "/blog/my-post" }}{{ .Title }}{{ end }}
diff --git a/docs/content/en/functions/RenderString.md b/docs/content/en/functions/RenderString.md
index e414b11ca..e4440971f 100644
--- a/docs/content/en/functions/RenderString.md
+++ b/docs/content/en/functions/RenderString.md
@@ -10,8 +10,6 @@ keywords: [markdown,goldmark,render]
signature: [".RenderString MARKUP"]
---
-{{< new-in "0.62.0" >}}
-
`.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).
The method takes an optional map argument with these options:
@@ -32,5 +30,4 @@ Some examples:
{{ "/italic org mode/" | $p.RenderString $optOrg }}
```
-
-**Note** that this method is more powerful than the similar [markdownify](/functions/markdownify/) function as it also supports [Render Hooks](/getting-started/configuration-markup/#markdown-render-hooks) and it has options to render other markup formats.
+{{< new-in "0.93.0" >}} **Note**: [markdownify](/functions/markdownify/) uses this function in order to support [Render Hooks](/getting-started/configuration-markup/#markdown-render-hooks).
diff --git a/docs/content/en/functions/abslangurl.md b/docs/content/en/functions/abslangurl.md
index d9818fcaf..11d540bde 100644
--- a/docs/content/en/functions/abslangurl.md
+++ b/docs/content/en/functions/abslangurl.md
@@ -1,27 +1,61 @@
---
title: absLangURL
-description: Adds the absolute URL with correct language prefix according to site configuration for multilingual.
-date: 2017-02-01
-publishdate: 2017-02-01
-lastmod: 2017-02-01
+description: Returns an absolute URL with a language prefix, if any.
categories: [functions]
menu:
docs:
- parent: "functions"
-keywords: [multilingual,i18n,urls]
+ parent: functions
+keywords: [urls, multilingual,i18n]
signature: ["absLangURL INPUT"]
-workson: []
-hugoversion:
-relatedfuncs: [relLangURL]
-deprecated: false
-aliases: []
---
-Both `absLangURL` and [`relLangURL`](/functions/rellangurl/) are similar to their [`absURL`](/functions/absurl/) and [`relURL`](/functions/relurl) relatives but will add the correct language prefix when the site is configured with more than one language.
+Use this function with both monolingual and multilingual configurations. The URL returned by this function depends on:
-So for a site `baseURL` set to `https://example.com/hugo/` and the current language is `en`:
+- Whether the input begins with a slash
+- The `baseURL` in site configuration
+- The language prefix, if any
+In examples that follow, the project is multilingual with content in both Español (`es`) and English (`en`). The default language is Español. The returned values are from the English site.
+
+### Input does not begin with a slash
+
+If the input does not begin with a slash, the resulting URL will be correct regardless of the `baseURL`.
+
+With `baseURL = https://example.org/`
+
+```go-html-template
+{{ absLangURL "" }} → https://example.org/en/
+{{ absLangURL "articles" }} → https://example.org/en/articles
+{{ absLangURL "style.css" }} → https://example.org/en/style.css
+```
+
+With `baseURL = https://example.org/docs/`
+
+```go-html-template
+{{ absLangURL "" }} → https://example.org/docs/en/
+{{ absLangURL "articles" }} → https://example.org/docs/en/articles
+{{ absLangURL "style.css" }} → https://example.org/docs/en/style.css
```
-{{ "blog/" | absLangURL }} → "https://example.com/hugo/en/blog/"
-{{ "blog/" | relLangURL }} → "/hugo/en/blog/"
+
+### 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`.
+
+With `baseURL = https://example.org/`
+
+```go-html-template
+{{ absLangURL "/" }} → https://example.org/en/
+{{ absLangURL "/articles" }} → https://example.org/en/articles
+{{ absLangURL "/style.css" }} → https://example.org/en/style.css
```
+
+With `baseURL = https://example.org/docs/`
+
+```go-html-template
+{{ absLangURL "/" }} → https://example.org/en/
+{{ absLangURL "/articles" }} → https://example.org/en/articles
+{{ absLangURL "/style.css" }} → https://example.org/en/style.css
+
+{{% note %}}
+The last three examples are not desirable in most situations. As a best practice, never include a leading slash when using this function.
+{{% /note %}}
diff --git a/docs/content/en/functions/absurl.md b/docs/content/en/functions/absurl.md
index 13524d2d6..efea2df75 100644
--- a/docs/content/en/functions/absurl.md
+++ b/docs/content/en/functions/absurl.md
@@ -1,50 +1,61 @@
---
title: absURL
-description: Creates an absolute URL based on the configured baseURL.
-date: 2017-02-01
-publishdate: 2017-02-01
-lastmod: 2017-02-01
+description: Returns an absolute URL.
categories: [functions]
menu:
docs:
- parent: "functions"
+ parent: functions
keywords: [urls]
signature: ["absURL INPUT"]
-workson: []
-hugoversion:
-relatedfuncs: [relURL]
-deprecated: false
-aliases: []
---
-Both `absURL` and `relURL` consider the configured value of `baseURL` in your site's [`config` file][configuration]. Given a `baseURL` set to `https://example.com/hugo/`:
+With multilingual configurations, use the [`absLangURL`] function instead. The URL returned by this function depends on:
+- Whether the input begins with a slash
+- The `baseURL` in site configuration
+
+### Input does not begin with a slash
+
+If the input does not begin with a slash, the resulting URL will be correct regardless of the `baseURL`.
+
+With `baseURL = https://example.org/`
+
+```go-html-template
+{{ absURL "" }} → https://example.org/
+{{ absURL "articles" }} → https://example.org/articles
+{{ absURL "style.css" }} → https://example.org/style.css
```
-{{ "mystyle.css" | absURL }} → "https://example.com/hugo/mystyle.css"
-{{ "mystyle.css" | relURL }} → "/hugo/mystyle.css"
-{{ "http://gohugo.io/" | relURL }} → "http://gohugo.io/"
-{{ "http://gohugo.io/" | absURL }} → "http://gohugo.io/"
+
+With `baseURL = https://example.org/docs/`
+
+```go-html-template
+{{ absURL "" }} → https://example.org/docs/
+{{ absURL "articles" }} → https://example.org/docs/articles
+{{ absURL "style.css" }} → https://example.org/docs/style.css
```
-The last two examples may look strange but can be very useful. For example, the following shows how to use `absURL` in [JSON-LD structured data (SEO)][jsonld], where some of your images for a piece of content may or may not be hosted locally:
+### Input begins with a slash
-{{< code file="layouts/partials/schemaorg-metadata.html" download="schemaorg-metadata.html" >}}
-<script type="application/ld+json">
-{
- "@context" : "http://schema.org",
- "@type" : "BlogPosting",
- "image" : {{ apply .Params.images "absURL" "." }}
-}
-</script>
-{{< /code >}}
+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`.
-The above uses the [apply function][] and also exposes how the Go template parser JSON-encodes objects inside `<script>` tags. See [the safeJS template function][safejs] for examples of how to tell Hugo not to escape strings inside of such tags.
+With `baseURL = https://example.org/`
+
+```go-html-template
+{{ absURL "/" }} → https://example.org/
+{{ absURL "/articles" }} → https://example.org/articles
+{{ absURL "/style.css" }} → https://example.org/style.css
+```
+
+With `baseURL = https://example.org/docs/`
+
+```go-html-template
+{{ absURL "/" }} → https://example.org/
+{{ absURL "/articles" }} → https://example.org/articles
+{{ absURL "/style.css" }} → https://example.org/style.css
+```
-{{% note "Ending Slash" %}}
-`absURL` and `relURL` are smart about missing slashes, but they will *not* add a closing slash to a URL if it is not present.
+{{% note %}}
+The last three examples are not desirable in most situations. As a best practice, never include a leading slash when using this function.
{{% /note %}}
-[apply function]: /functions/apply/
-[configuration]: /getting-started/configuration/
-[jsonld]: https://developers.google.com/search/docs/guides/intro-structured-data
-[safejs]: /functions/safejs
+[`absLangURL`]: /functions/abslangurl/
diff --git a/docs/content/en/functions/complement.md b/docs/content/en/functions/complement.md
index a41faf524..91640d803 100644
--- a/docs/content/en/functions/complement.md
+++ b/docs/content/en/functions/complement.md
@@ -1,29 +1,80 @@
---
-title: "complement"
-description: "`collections.Complement` (alias `complement`) gives the elements of a collection that are not in any of the others."
-date: 2018-11-07
+title: complement
+description: Returns the elements of the last collection that are not in any of the others.
categories: [functions]
menu:
docs:
- parent: "functions"
-keywords: [collections,intersect,union]
-signature: ["COLLECTION | complement COLLECTION [COLLECTION]..." ]
-hugoversion: "0.51"
+ parent: functions
+keywords: [collections]
+signature:
+- "complement COLLECTION [COLLECTION]..."
+- "collections.Complement COLLECTION [COLLECTION]..."
+relatedfuncs: [intersect,symdiff,union]
aliases: []
---
-Example:
+To find the elements within `$c3` that do not exist in `$c1` or `$c2`:
```go-html-template
-{{ $pages := site.RegularPages | first 50 }}
-{{ $news := where $pages "Type" "news" | first 5 }}
-{{ $blog := where $pages "Type" "blog" | first 5 }}
-{{ $other := $pages | complement $news $blog | first 10 }}
+{{ $c1 := slice 3 }}
+{{ $c2 := slice 4 5 }}
+{{ $c3 := slice 1 2 3 4 5 }}
+
+{{ complement $c1 $c2 $c3 }} → [1 2]
+```
+
+{{% note %}}
+Make your code simpler to understand by using a [chained pipeline]:
+
+[chained pipeline]: https://pkg.go.dev/text/template#hdr-Pipelines
+{{% /note %}}
+
+
+```go-html-template
+{{ $c3 | complement $c1 $c2 }} → [1 2]
```
-The above is an imaginary use case for the home page where you want to display different page listings in sections/boxes on different places on the page: 5 from `news`, 5 from the `blog` and then 10 of the pages not shown in the other listings, to _complement_ them.
+You can also use the `complement` function with page collections. Let's say your site has five content types:
+```text
+content/
+├── blog/
+├── books/
+├── faqs/
+├── films/
+└── songs/
+```
+To list everything except blog articles (`blog`) and frequently asked questions (`faqs`):
+```go-html-template
+{{ $blog := where site.RegularPages "Type" "blog" }}
+{{ $faqs := where site.RegularPages "Type" "faqs" }}
+{{ range site.RegularPages | complement $blog $faqs }}
+ <a href="{{ .RelPermalink }}">{{ .LinkTitle }}</a>
+{{ end }}
+```
+{{% note %}}
+Although the example above demonstrates the `complement` function, you could use the [`where`] function as well:
+
+[`where`]: /functions/where/
+{{% /note %}}
+
+```go-html-template
+{{ range where site.RegularPages "Type" "not in" (slice "blog" "faqs") }}
+ <a href="{{ .RelPermalink }}">{{ .LinkTitle }}</a>
+{{ end }}
+```
+
+In this example we use the `complement` function to remove [stop words] from a sentence:
+
+```go-html-template
+{{ $text := "The quick brown fox jumps over the lazy dog" }}
+{{ $stopWords := slice "a" "an" "in" "over" "the" "under" }}
+{{ $filtered := split $text " " | complement $stopWords }}
+
+{{ delimit $filtered " " }} → The quick brown fox jumps lazy dog
+```
+[stop words]: https://en.wikipedia.org/wiki/Stop_word
diff --git a/docs/content/en/functions/dateformat.md b/docs/content/en/functions/dateformat.md
index 362efabd3..7a16e80b2 100644
--- a/docs/content/en/functions/dateformat.md
+++ b/docs/content/en/functions/dateformat.md
@@ -22,7 +22,7 @@ deprecated: false
{{ time.Format "Monday, Jan 2, 2006" "2015-01-21" }} → "Wednesday, Jan 21, 2015"
```
-Note that since Hugo 0.87.0, `time.Format` will return a localized string for the current language. {{< new-in "0.87.0" >}}
+`time.Format` returns a localized string for the current language.
The `LAYOUT` string can be either:
@@ -34,9 +34,7 @@ See the [`time` function](/functions/time/) to convert a timestamp string to a G
## Date/time formatting layouts
-{{< new-in "0.87.0" >}}
-
-Go's date layout strings can be hard to reason about, especially with multiple languages. Since Hugo 0.87.0 you can alternatively use some predefined layout identifiers that will output localized dates or times:
+Go's date layout strings can be hard to reason about, especially with multiple languages. You can alternatively use some predefined layout identifiers that will output localized dates or times:
```go-html-template
{{ .Date | time.Format ":date_long" }}
diff --git a/docs/content/en/functions/dict.md b/docs/content/en/functions/dict.md
index 966b6af3e..5c8ad9d8a 100644
--- a/docs/content/en/functions/dict.md
+++ b/docs/content/en/functions/dict.md
@@ -3,7 +3,6 @@ title: dict
description: Creates a dictionary from a list of key and value pairs.
date: 2017-02-01
publishdate: 2017-02-01
-lastmod: 2017-02-26
categories: [functions]
menu:
docs:
@@ -25,21 +24,20 @@ Note that the `key` can be either a `string` or a `string slice`. The latter is
{{ $m := dict (slice "a" "b" "c") "value" }}
```
-
## Example: Using `dict` to pass multiple values to a `partial`
-The partial below creates a SVG and expects `fill`, `height` and `width` from the caller:
+The partial below creates an SVG and expects `fill`, `height` and `width` from the caller:
-**Partial definition**
+### Partial definition
{{< code file="layouts/partials/svgs/external-links.svg" download="external-links.svg" >}}
-<svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"
+<svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"
fill="{{ .fill }}" width="{{ .width }}" height="{{ .height }}" viewBox="0 0 32 32" aria-label="External Link">
<path d="M25.152 16.576v5.696q0 2.144-1.504 3.648t-3.648 1.504h-14.848q-2.144 0-3.648-1.504t-1.504-3.648v-14.848q0-2.112 1.504-3.616t3.648-1.536h12.576q0.224 0 0.384 0.16t0.16 0.416v1.152q0 0.256-0.16 0.416t-0.384 0.16h-12.576q-1.184 0-2.016 0.832t-0.864 2.016v14.848q0 1.184 0.864 2.016t2.016 0.864h14.848q1.184 0 2.016-0.864t0.832-2.016v-5.696q0-0.256 0.16-0.416t0.416-0.16h1.152q0.256 0 0.416 0.16t0.16 0.416zM32 1.152v9.12q0 0.48-0.352 0.8t-0.8 0.352-0.8-0.352l-3.136-3.136-11.648 11.648q-0.16 0.192-0.416 0.192t-0.384-0.192l-2.048-2.048q-0.192-0.16-0.192-0.384t0.192-0.416l11.648-11.648-3.136-3.136q-0.352-0.352-0.352-0.8t0.352-0.8 0.8-0.352h9.12q0.48 0 0.8 0.352t0.352 0.8z"></path>
</svg>
{{< /code >}}
-**Partial call**
+### Partial call
The `fill`, `height` and `width` values can be stored in one object with `dict` and passed to the partial:
@@ -47,6 +45,4 @@ The `fill`, `height` and `width` values can be stored in one object with `dict`
{{ partial "svgs/external-links.svg" (dict "fill" "#01589B" "width" 10 "height" 20 ) }}
{{< /code >}}
-
-
[partials]: /templates/partials/
diff --git a/docs/content/en/functions/errorf.md b/docs/content/en/functions/errorf.md
index 41ea2f192..04fd0edb0 100644
--- a/docs/content/en/functions/errorf.md
+++ b/docs/content/en/functions/errorf.md
@@ -30,7 +30,7 @@ Both functions return an empty string, so the messages are only printed to the c
{{ warnf "You should update the shortcodes in %q" .Path }}
```
-Note that `errorf`, `erroridf`, and `warnf` support all the formatting verbs of the [fmt](https://golang.org/pkg/fmt/) package.
+Note that `errorf`, `erroridf`, and `warnf` support all the formatting verbs of the [fmt](https://pkg.go.dev/fmt) package.
## Suppress errors
diff --git a/docs/content/en/functions/findRe.md b/docs/content/en/functions/findRe.md
index b6375be9c..2896e3cd1 100644
--- a/docs/content/en/functions/findRe.md
+++ b/docs/content/en/functions/findRe.md
@@ -1,44 +1,40 @@
---
title: findRE
-description: Returns a list of strings that match the regular expression.
-date: 2017-02-01
-publishdate: 2017-02-01
-lastmod: 2017-02-01
+description: Returns a slice of strings that match the regular expression.
categories: [functions]
menu:
docs:
- parent: "functions"
+ parent: functions
keywords: [regex]
-signature: ["findRE PATTERN INPUT [LIMIT]"]
-workson: []
-hugoversion:
-relatedfuncs: []
-deprecated: false
+signature:
+ - "findRE PATTERN INPUT [LIMIT]"
+ - "strings.FindRE PATTERN INPUT [LIMIT]"
+relatedfuncs: [replaceRE]
aliases: []
---
+By default, the `findRE` function finds all matches. You can limit the number of matches with an optional LIMIT paramater.
-By default all matches will be included. The number of matches can be limited with an optional third parameter.
+When specifying the regular expression, use a raw [string literal] (backticks) instead of an interpreted string literal (double quotes) to simplify the syntax. With an interpreted string literal you must escape backslashes.
-The example below returns a list of all second level headers (`<h2>`) in the content:
+The syntax of the regular expression is the same general syntax used by Perl, Python, and other languages. More precisely, it is the syntax accepted by [RE2] except for `\C`.
-```
-{{ findRE "<h2.*?>(.|\n)*?</h2>" .Content }}
+This example returns a slice of all second level headings (`h2` elements) within the rendered `.Content`:
+
+```go-html-template
+{{ findRE `(?s)<h2.*?>.*?</h2>` .Content }}
```
-You can limit the number of matches in the list with a third parameter. The following example shows how to limit the returned value to just one match (or none, if there are no matched substrings):
+The `s` flag causes `.` to match `\n` as well, allowing us to find an `h2` element that contains newlines.
-```
-{{ findRE "<h2.*?>(.|\n)*?</h2>" .Content 1 }}
- <!-- returns ["<h2 id="#foo">Foo</h2>"] -->
+To limit the number of matches to one:
+
+```go-html-template
+{{ findRE `(?s)<h2.*?>.*?</h2>`