1--- 2title: apply 3description: Given a map, array, or slice, `apply` returns a new slice with a function applied over it. 4date: 2017-02-01 5publishdate: 2017-02-01 6lastmod: 2017-02-01 7categories: [functions] 8menu: 9 docs: 10 parent: "functions" 11keywords: [advanced] 12signature: ["apply COLLECTION FUNCTION [PARAM...]"] 13workson: [] 14hugoversion: 15relatedfuncs: [] 16deprecated: false 17draft: false 18aliases: [] 19--- 20 21{{< todo >}} 22POTENTIAL NEW CONTENT: see apply/sequence discussion: https://discourse.gohugo.io/t/apply-printf-on-a-sequence/5722; 23{{< /todo >}} 24 25`apply` expects at least three parameters, depending on the function being applied. 26 271. The first parameter is the sequence to operate on. 282. The second parameter is the name of the function as a string, which must be the name of a valid [Hugo function][functions]. 293. After that, the parameters to the applied function are provided, with the string `"."` standing in for each element of the sequence the function is to be applied against. 30 31Here is an example of a content file with `names:` as a front matter field: 32 33``` 34+++ 35names: [ "Derek Perkins", "Joe Bergevin", "Tanner Linsley" ] 36+++ 37``` 38 39You can then use `apply` as follows: 40 41``` 42{{ apply .Params.names "urlize" "." }} 43``` 44 45Which will result in the following: 46 47``` 48"derek-perkins", "joe-bergevin", "tanner-linsley" 49``` 50 51This is *roughly* equivalent to using the following with [range][]: 52 53``` 54{{ range .Params.names }}{{ . | urlize }}{{ end }} 55``` 56 57However, it is not possible to provide the output of a range to the [`delimit` function][delimit], so you need to `apply` it. 58 59If you have `post-tag-list.html` and `post-tag-link.html` as [partials][], you *could* use the following snippets, respectively: 60 61{{< code file="layouts/partials/post-tag-list.html" copy="false" >}} 62{{ with .Params.tags }} 63<div class="tags-list"> 64 Tags: 65 {{ $len := len . }} 66 {{ if eq $len 1 }} 67 {{ partial "post-tag-link" (index . 0) }} 68 {{ else }} 69 {{ $last := sub $len 1 }} 70 {{ range first $last . }} 71 {{ partial "post-tag-link" . }}, 72 {{ end }} 73 {{ partial "post-tag-link" (index . $last) }} 74 {{ end }} 75</div> 76{{ end }} 77{{< /code >}} 78 79{{< code file="layouts/partials/post-tag-link.html" copy="false" >}} 80<a class="post-tag post-tag-{{ . | urlize }}" href="/tags/{{ . | urlize }}">{{ . }}</a> 81{{< /code >}} 82 83This works, but the complexity of `post-tag-list.html` is fairly high. The Hugo template needs to perform special behavior for the case where there’s only one tag, and it has to treat the last tag as special. Additionally, the tag list will be rendered something like `Tags: tag1 , tag2 , tag3` because of the way that the HTML is generated and then interpreted by a browser. 84 85This 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: 86 87``` 88{{ with .Params.tags }} 89 <div class="tags-list"> 90 Tags: 91 {{ $sort := sort . }} 92 {{ $links := apply $sort "partial" "post-tag-link" "." }} 93 {{ $clean := apply $links "chomp" "." }} 94 {{ delimit $clean ", " }} 95 </div> 96{{ end }} 97``` 98 99Now 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: 100 101{{< code file="layouts/partials/post-tag-list.html" download="post-tag-list.html" >}} 102 {{ with .Params.tags }} 103 <div class="tags-list"> 104 Tags: 105 {{ delimit (apply (apply (sort .) "partial" "post-tag-link" ".") "chomp" ".") ", " }} 106 </div> 107 {{ end }} 108{{< /code >}} 109 110{{% note %}} 111`apply` does not work when receiving the sequence as an argument through a pipeline. 112{{% /note %}} 113 114[chomp]: /functions/chomp/ "See documentation for the chomp function" 115[delimit]: /functions/delimit/ "See documentation for the delimit function" 116[functions]: /functions/ "See the full list of Hugo functions to see what can be passed as an argument to the apply function." 117[partials]: /templates/partials/ 118[range]: /functions/range/ "Learn the importance of the range function, a fundamental keyword in both Hugo templates and the Go programming language." 119