diff options
author | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2019-11-11 11:46:22 +0100 |
---|---|---|
committer | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2019-11-11 11:46:22 +0100 |
commit | 79355043e8f805cfdd911efed3613f0c5b32e853 (patch) | |
tree | dbd24aae289b60d05cb346b17b4ff8499baf534a /docs/content/en/functions | |
parent | d1d1f240a25945b37eebe8a9a3f439f290832b33 (diff) | |
parent | efc0b1bb6c6564f54d596467dbc6a18cb206954e (diff) |
Merge commit 'efc0b1bb6c6564f54d596467dbc6a18cb206954e'
Diffstat (limited to 'docs/content/en/functions')
-rw-r--r-- | docs/content/en/functions/index-function.md | 22 | ||||
-rw-r--r-- | docs/content/en/functions/print.md | 3 |
2 files changed, 19 insertions, 6 deletions
diff --git a/docs/content/en/functions/index-function.md b/docs/content/en/functions/index-function.md index e5f039caf..94b9b4191 100644 --- a/docs/content/en/functions/index-function.md +++ b/docs/content/en/functions/index-function.md @@ -11,7 +11,7 @@ menu: docs: parent: "functions" keywords: [] -signature: ["index COLLECTION INDEX", "index COLLECTION KEY"] +signature: ["index COLLECTION INDEXES", "index COLLECTION KEYS"] workson: [] hugoversion: relatedfuncs: [] @@ -20,13 +20,25 @@ aliases: [/functions/index/] needsexample: true --- -From the Godocs: +The `index` functions returns the result of indexing its first argument by the following arguments. Each indexed item must be a map or a slice, e.g.: -> Returns the result of indexing its first argument by the following arguments. Thus "index x 1 2 3" is, in Go syntax, x[1][2][3]. Each indexed item must be a map, slice, or array. +```go-text-template +{{ $slice := slice "a" "b" "c" }} +{{ index $slice 1 }} => b +{{ $map := dict "a" 100 "b" 200 }} +{{ index $map "b" }} => 200 +``` + +The function takes multiple indices as arguments, and this can be used to get nested values, e.g.: + +```go-text-template +{{ $map := dict "a" 100 "b" 200 "c" (slice 10 20 30) }} +{{ index $map "c" 1 }} => 20 +{{ $map := dict "a" 100 "b" 200 "c" (dict "d" 10 "e" 20) }} +{{ index $map "c" "e" }} => 20 +``` -In Go templates, you can't access array, slice, or map elements directly the same way you would in Go. For example, `$.Site.Data.authors[.Params.authorkey]` isn't supported syntax. -Instead, you have to use `index`, a function that handles the lookup for you. ## Example: Load Data from a Path Based on Front Matter Params diff --git a/docs/content/en/functions/print.md b/docs/content/en/functions/print.md index fffbb79dc..d04a6ee17 100644 --- a/docs/content/en/functions/print.md +++ b/docs/content/en/functions/print.md @@ -1,7 +1,7 @@ --- title: print linktitle: print -description: Prints the default representation of the given argument using the standard `fmt.Print` function. +description: Prints the default representation of the given arguments using the standard `fmt.Print` function. godocref: https://golang.org/pkg/fmt/ date: 2017-02-01 publishdate: 2017-02-01 @@ -22,5 +22,6 @@ See [the go doc](https://golang.org/pkg/fmt/) for additional information. ``` {{ print "foo" }} → "foo" +{{ print "foo" "bar" }} → "foobar" {{ print (slice 1 2 3) }} → [1 2 3] ``` |