From fb76580f81615def6e979f629b5bee648e5fbf6c Mon Sep 17 00:00:00 2001 From: Joe Mooring Date: Sat, 11 May 2024 06:15:59 -0700 Subject: [PATCH] Update function signatures Co-authored-by: irkode --- .../en/functions/collections/Dictionary.md | 35 ++++++------------- content/en/functions/collections/KeyVals.md | 2 +- content/en/functions/collections/Querify.md | 27 ++++++++------ content/en/functions/collections/Slice.md | 10 ++++-- 4 files changed, 35 insertions(+), 39 deletions(-) diff --git a/content/en/functions/collections/Dictionary.md b/content/en/functions/collections/Dictionary.md index f46b02e75..2ac875d28 100644 --- a/content/en/functions/collections/Dictionary.md +++ b/content/en/functions/collections/Dictionary.md @@ -1,6 +1,6 @@ --- title: collections.Dictionary -description: Creates a map from a list of key and value pairs. +description: Returns a map composed of the given key-value pairs. categories: [] keywords: [] action: @@ -8,10 +8,12 @@ action: related: - functions/collections/Slice returnType: mapany - signatures: ['collections.Dictionary KEY VALUE [VALUE...]'] + signatures: ['collections.Dictionary [VALUE...]'] aliases: [/functions/dict] --- +Specify the key-value pairs as individual arguments: + ```go-html-template {{ $m := dict "a" 1 "b" 2 }} ``` @@ -25,6 +27,12 @@ The above produces this data structure: } ``` +To create an empty map: + +```go-html-template +{{ $m := dict }} +``` + Note that the `key` can be either a `string` or a `string slice`. The latter is useful to create a deeply nested structure, e.g.: @@ -43,26 +51,3 @@ The above produces this data structure: } } ``` - -## Pass values to a partial template - -The partial below creates an SVG and expects `fill`, `height` and `width` from the caller: - -### Partial definition - -{{< code file=layouts/partials/svgs/external-links.svg >}} - - - -{{< /code >}} - -### Partial call - -The `fill`, `height` and `width` values can be stored in one object with `dict` and passed to the partial: - -{{< code file=layouts/_default/list.html >}} -{{ partial "svgs/external-links.svg" (dict "fill" "#01589B" "width" 10 "height" 20 ) }} -{{< /code >}} - -[partials]: /templates/partials/ diff --git a/content/en/functions/collections/KeyVals.md b/content/en/functions/collections/KeyVals.md index 9f1bd1745..1dfd2bc38 100644 --- a/content/en/functions/collections/KeyVals.md +++ b/content/en/functions/collections/KeyVals.md @@ -8,7 +8,7 @@ action: related: - methods/pages/Related returnType: types.KeyValues - signatures: [collections.KeyVals KEY VALUES...] + signatures: [collections.KeyVals KEY VALUE...] aliases: [/functions/keyvals] --- diff --git a/content/en/functions/collections/Querify.md b/content/en/functions/collections/Querify.md index ea0434fc5..07e27bd7e 100644 --- a/content/en/functions/collections/Querify.md +++ b/content/en/functions/collections/Querify.md @@ -1,6 +1,6 @@ --- title: collections.Querify -description: Takes a set or slice of key-value pairs and returns a query string to be appended to URLs. +description: Returns a URL query string composed of the given key-value pairs. categories: [] keywords: [] action: @@ -9,24 +9,29 @@ action: - functions/go-template/urlquery.md returnType: string signatures: - - collections.Querify VALUE [VALUE...] - - collections.Querify COLLECTION + - collections.Querify [VALUE...] aliases: [/functions/querify] --- -`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. +Specify the key-value pairs as individual arguments, or as a slice. The following are equivalent: -The following examples create a link to a search results page on Google. ```go-html-template -Search - -{{ $qs := slice "q" "test" "page" 3 }} -Search +{{ collections.Querify "a" 1 "b" 2 }} +{{ collections.Querify (slice "a" 1 "b" 2) }} ``` -Both of these examples render the following HTML: +To append a query string to a URL: + +```go-html-template +{{ $qs := collections.Querify "a" 1 "b" 2 }} +{{ $href := printf "https://example.org?%s" $qs }} + +Link +``` + +Hugo renders this to: ```html -Search +Link ``` diff --git a/content/en/functions/collections/Slice.md b/content/en/functions/collections/Slice.md index 56c068d4b..385f9b658 100644 --- a/content/en/functions/collections/Slice.md +++ b/content/en/functions/collections/Slice.md @@ -1,6 +1,6 @@ --- title: collections.Slice -description: Creates a slice of all passed arguments. +description: Returns a slice composed of the given values. categories: [] keywords: [] action: @@ -8,7 +8,7 @@ action: related: - functions/collections/Dictionary returnType: any - signatures: [collections.Slice ITEM...] + signatures: ['collections.Slice [VALUE...]'] aliases: [/functions/slice] --- @@ -16,3 +16,9 @@ aliases: [/functions/slice] {{ $s := slice "a" "b" "c" }} {{ $s }} → [a b c] ``` + +To create an empty slice: + +```go-html-template +{{ $s := slice }} +```