summaryrefslogtreecommitdiffstats
path: root/docs/content/en/functions/apply.md
diff options
context:
space:
mode:
Diffstat (limited to 'docs/content/en/functions/apply.md')
-rw-r--r--docs/content/en/functions/apply.md83
1 files changed, 35 insertions, 48 deletions
diff --git a/docs/content/en/functions/apply.md b/docs/content/en/functions/apply.md
index 983e2fdb9..0bc071e7b 100644
--- a/docs/content/en/functions/apply.md
+++ b/docs/content/en/functions/apply.md
@@ -1,27 +1,15 @@
---
title: apply
description: Given a map, array, or slice, `apply` returns a new slice with a function applied over it.
-date: 2017-02-01
-publishdate: 2017-02-01
-lastmod: 2017-02-01
categories: [functions]
menu:
docs:
- parent: "functions"
+ parent: functions
keywords: [advanced]
signature: ["apply COLLECTION FUNCTION [PARAM...]"]
-workson: []
-hugoversion:
relatedfuncs: []
-deprecated: false
-draft: false
-aliases: []
---
-{{< todo >}}
-POTENTIAL NEW CONTENT: see apply/sequence discussion: https://discourse.gohugo.io/t/apply-printf-on-a-sequence/5722;
-{{< /todo >}}
-
`apply` expects at least three parameters, depending on the function being applied.
1. The first parameter is the sequence to operate on.
@@ -30,15 +18,14 @@ POTENTIAL NEW CONTENT: see apply/sequence discussion: https://discourse.gohugo.i
Here is an example of a content file with `names:` as a front matter field:
-```
-+++
+{{< code-toggle file="content/example.md" fm=true copy=false >}}
+title: Example
names: [ "Derek Perkins", "Joe Bergevin", "Tanner Linsley" ]
-+++
-```
+{{< /code-toggle >}}
You can then use `apply` as follows:
-```
+```go-html-template
{{ apply .Params.names "urlize" "." }}
```
@@ -50,7 +37,7 @@ Which will result in the following:
This is *roughly* equivalent to using the following with [range]:
-```
+```go-html-template
{{ range .Params.names }}{{ . | urlize }}{{ end }}
```
@@ -58,25 +45,25 @@ However, it is not possible to provide the output of a range to the [`delimit` f
If you have `post-tag-list.html` and `post-tag-link.html` as [partials], you *could* use the following snippets, respectively:
-{{< code file="layouts/partials/post-tag-list.html" copy="false" >}}
+{{< code file="layouts/partials/post-tag-list.html" copy=false >}}
{{ with .Params.tags }}
-<div class="tags-list">
- Tags:
- {{ $len := len . }}
- {{ if eq $len 1 }}
- {{ partial "post-tag-link.html" (index . 0) }}
- {{ else }}
- {{ $last := sub $len 1 }}
- {{ range first $last . }}
- {{ partial "post-tag-link.html" . }},
+ <div class="tags-list">
+ Tags:
+ {{ $len := len . }}
+ {{ if eq $len 1 }}
+ {{ partial "post-tag-link.html" (index . 0) }}
+ {{ else }}
+ {{ $last := sub $len 1 }}
+ {{ range first $last . }}
+ {{ partial "post-tag-link.html" . }},
+ {{ end }}
+ {{ partial "post-tag-link.html" (index . $last) }}
{{ end }}
- {{ partial "post-tag-link.html" (index . $last) }}
- {{ end }}
-</div>
+ </div>
{{ end }}
{{< /code >}}
-{{< code file="layouts/partials/post-tag-link.html" copy="false" >}}
+{{< code file="layouts/partials/post-tag-link.html" copy=false >}}
<a class="post-tag post-tag-{{ . | urlize }}" href="/tags/{{ . | urlize }}">{{ . }}</a>
{{< /code >}}
@@ -84,27 +71,27 @@ This works, but the complexity of `post-tag-list.html` is fairly high. The Hugo
This first version of `layouts/partials/post-tag-list.html` separates all of the operations for ease of reading. The combined and DRYer version is shown next:
-```
+```go-html-template
{{ with .Params.tags }}
- <div class="tags-list">
- Tags:
- {{ $sort := sort . }}
- {{ $links := apply $sort "partial" "post-tag-link.html" "." }}
- {{ $clean := apply $links "chomp" "." }}
- {{ delimit $clean ", " }}
- </div>
+ <div class="tags-list">
+ Tags:
+ {{ $sort := sort . }}
+ {{ $links := apply $sort "partial" "post-tag-link.html" "." }}
+ {{ $clean := apply $links "chomp" "." }}
+ {{ delimit $clean ", " }}
+ </div>
{{ end }}
```
Now in the completed version, you can sort the tags, convert the tags to links with `layouts/partials/post-tag-link.html`, [chomp] off stray newlines, and join the tags together in a delimited list for presentation. Here is an even DRYer version of the preceding example:
-{{< code file="layouts/partials/post-tag-list.html" download="post-tag-list.html" >}}
- {{ with .Params.tags }}
- <div class="tags-list">
- Tags:
- {{ delimit (apply (apply (sort .) "partial" "post-tag-link.html" ".") "chomp" ".") ", " }}
- </div>
- {{ end }}
+{{< code file="layouts/partials/post-tag-list.html" >}}
+{{ with .Params.tags }}
+ <div class="tags-list">
+ Tags:
+ {{ delimit (apply (apply (sort .) "partial" "post-tag-link.html" ".") "chomp" ".") ", " }}
+ </div>
+{{ end }}
{{< /code >}}
{{% note %}}