hugo

Fork of github.com/gohugoio/hugo with reverse pagination support

git clone git://git.shimmy1996.com/hugo.git

apply.md (4282B)

    1 ---
    2 title: apply
    3 description: Given a map, array, or slice, `apply` returns a new slice with a function applied over it.
    4 date: 2017-02-01
    5 publishdate: 2017-02-01
    6 lastmod: 2017-02-01
    7 categories: [functions]
    8 menu:
    9   docs:
   10     parent: "functions"
   11 keywords: [advanced]
   12 signature: ["apply COLLECTION FUNCTION [PARAM...]"]
   13 workson: []
   14 hugoversion:
   15 relatedfuncs: []
   16 deprecated: false
   17 draft: false
   18 aliases: []
   19 ---
   20 
   21 {{< todo >}}
   22 POTENTIAL 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 
   27 1. The first parameter is the sequence to operate on.
   28 2. The second parameter is the name of the function as a string, which must be the name of a valid [Hugo function][functions].
   29 3. 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 
   31 Here is an example of a content file with `names:` as a front matter field:
   32 
   33 ```
   34 +++
   35 names: [ "Derek Perkins", "Joe Bergevin", "Tanner Linsley" ]
   36 +++
   37 ```
   38 
   39 You can then use `apply` as follows:
   40 
   41 ```
   42 {{ apply .Params.names "urlize" "." }}
   43 ```
   44 
   45 Which will result in the following:
   46 
   47 ```
   48 "derek-perkins", "joe-bergevin", "tanner-linsley"
   49 ```
   50 
   51 This is *roughly* equivalent to using the following with [range][]:
   52 
   53 ```
   54 {{ range .Params.names }}{{ . | urlize }}{{ end }}
   55 ```
   56 
   57 However, it is not possible to provide the output of a range to the [`delimit` function][delimit], so you need to `apply` it.
   58 
   59 If 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.html" (index . 0) }}
   68   {{ else }}
   69     {{ $last := sub $len 1 }}
   70     {{ range first $last . }}
   71       {{ partial "post-tag-link.html" . }},
   72     {{ end }}
   73     {{ partial "post-tag-link.html" (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 
   83 This 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 
   85 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:
   86 
   87 ```
   88 {{ with .Params.tags }}
   89     <div class="tags-list">
   90       Tags:
   91       {{ $sort := sort . }}
   92       {{ $links := apply $sort "partial" "post-tag-link.html" "." }}
   93       {{ $clean := apply $links "chomp" "." }}
   94       {{ delimit $clean ", " }}
   95     </div>
   96 {{ end }}
   97 ```
   98 
   99 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:
  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.html" ".") "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."