mirror of
https://github.com/gohugoio/hugo.git
synced 2024-11-28 22:01:43 -05:00
Merge commit 'aaaf1c8df5d6aa061609d20510f7fa6c42e5cc1a'
This commit is contained in:
commit
0baa4f9837
4 changed files with 55 additions and 133 deletions
|
@ -1,7 +1,7 @@
|
||||||
---
|
---
|
||||||
title: collections.Apply
|
title: collections.Apply
|
||||||
linkTitle: apply
|
linkTitle: apply
|
||||||
description: Given an array or slice, `apply` returns a new slice with a function applied over it.
|
description: Returns a new collection with each element transformed by the given function.
|
||||||
categories: [functions]
|
categories: [functions]
|
||||||
keywords: []
|
keywords: []
|
||||||
menu:
|
menu:
|
||||||
|
@ -9,8 +9,8 @@ menu:
|
||||||
parent: functions
|
parent: functions
|
||||||
function:
|
function:
|
||||||
aliases: [apply]
|
aliases: [apply]
|
||||||
returnType: any
|
returnType: '[]any'
|
||||||
signatures: ['collections.Apply COLLECTION FUNCTION [PARAM...]']
|
signatures: [collections.Apply COLLECTION FUNCTION PARAM...]
|
||||||
relatedFunctions:
|
relatedFunctions:
|
||||||
- collections.Apply
|
- collections.Apply
|
||||||
- collections.Delimit
|
- collections.Delimit
|
||||||
|
@ -21,95 +21,17 @@ relatedFunctions:
|
||||||
aliases: [/functions/apply]
|
aliases: [/functions/apply]
|
||||||
---
|
---
|
||||||
|
|
||||||
`apply` expects at least three arguments, depending on the function being applied.
|
The `apply` function takes three or more arguments, depending on the function being applied to the collection elements.
|
||||||
|
|
||||||
1. The first argument is the sequence to operate on.
|
The first argument is the collection itself, the second argument is the function name, and the remaining arguments are passed to the function, with the string `"."` representing the collection element.
|
||||||
2. The second argument is the name of the function as a string, which must be the name of a valid [template function].
|
|
||||||
3. After that, the arguments to the applied function are provided, with the string `"."` standing in for each element of the sequence the function is to be applied against.
|
|
||||||
|
|
||||||
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
|
```go-html-template
|
||||||
{{ apply .Params.names "urlize" "." }}
|
{{ $s := slice "hello" "world" }}
|
||||||
|
|
||||||
|
{{ $s = apply $s "strings.FirstUpper" "." }}
|
||||||
|
{{ $s }} → [Hello World]
|
||||||
|
|
||||||
|
{{ $s = apply $s "strings.Replace" "." "l" "_" }}
|
||||||
|
{{ $s }} → [He__o Wor_d]
|
||||||
```
|
```
|
||||||
|
|
||||||
Which will result in the following:
|
|
||||||
|
|
||||||
```
|
|
||||||
"derek-perkins", "joe-bergevin", "tanner-linsley"
|
|
||||||
```
|
|
||||||
|
|
||||||
This is *roughly* equivalent to using the following with [`range`]:
|
|
||||||
|
|
||||||
```go-html-template
|
|
||||||
{{ range .Params.names }}{{ . | urlize }}{{ end }}
|
|
||||||
```
|
|
||||||
|
|
||||||
However, it is not possible to provide the output of a range to the [`delimit`]function, so you need to `apply` it.
|
|
||||||
|
|
||||||
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 >}}
|
|
||||||
{{ 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" . }},
|
|
||||||
{{ end }}
|
|
||||||
{{ partial "post-tag-link.html" (index . $last) }}
|
|
||||||
{{ end }}
|
|
||||||
</div>
|
|
||||||
{{ end }}
|
|
||||||
{{< /code >}}
|
|
||||||
|
|
||||||
{{< code file="layouts/partials/post-tag-link.html" copy=false >}}
|
|
||||||
<a class="post-tag post-tag-{{ . | urlize }}" href="/tags/{{ . | urlize }}">{{ . }}</a>
|
|
||||||
{{< /code >}}
|
|
||||||
|
|
||||||
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.
|
|
||||||
|
|
||||||
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>
|
|
||||||
{{ end }}
|
|
||||||
```
|
|
||||||
|
|
||||||
Now in the completed version, you can sort the tags, convert the tags to links with `layouts/partials/post-tag-link.html`, [`chomp`] 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" >}}
|
|
||||||
{{ with .Params.tags }}
|
|
||||||
<div class="tags-list">
|
|
||||||
Tags:
|
|
||||||
{{ delimit (apply (apply (sort .) "partial" "post-tag-link.html" ".") "chomp" ".") ", " }}
|
|
||||||
</div>
|
|
||||||
{{ end }}
|
|
||||||
{{< /code >}}
|
|
||||||
|
|
||||||
{{% note %}}
|
|
||||||
`apply` does not work when receiving the sequence as an argument through a pipeline.
|
|
||||||
{{% /note %}}
|
|
||||||
|
|
||||||
[`chomp`]: /functions/strings/chomp/
|
|
||||||
[`delimit`]: /functions/collections/delimit/
|
|
||||||
[template function]: /functions/
|
|
||||||
[`range`]: /functions/go-template/range/
|
|
||||||
|
|
|
@ -17,9 +17,9 @@ See [images.Filter](#filter) for how to apply these filters to an image.
|
||||||
|
|
||||||
{{< new-in "0.119.0" >}}
|
{{< new-in "0.119.0" >}}
|
||||||
|
|
||||||
{{% funcsig %}}
|
{{< funcsig >}}
|
||||||
images.Process SRC SPEC
|
images.Process SRC SPEC
|
||||||
{{% /funcsig %}}
|
{{< /funcsig >}}
|
||||||
|
|
||||||
A general purpose image processing function.
|
A general purpose image processing function.
|
||||||
|
|
||||||
|
@ -36,9 +36,9 @@ This filter has all the same options as the [Process](/content-management/image-
|
||||||
|
|
||||||
## Overlay
|
## Overlay
|
||||||
|
|
||||||
{{% funcsig %}}
|
{{< funcsig >}}
|
||||||
images.Overlay SRC X Y
|
images.Overlay SRC X Y
|
||||||
{{% /funcsig %}}
|
{{< /funcsig >}}
|
||||||
|
|
||||||
Overlay creates a filter that overlays the source image at position x y, e.g:
|
Overlay creates a filter that overlays the source image at position x y, e.g:
|
||||||
|
|
||||||
|
@ -60,9 +60,9 @@ The above will overlay `$logo` in the upper left corner of `$img` (at position `
|
||||||
|
|
||||||
{{< new-in "0.119.0" >}}
|
{{< new-in "0.119.0" >}}
|
||||||
|
|
||||||
{{% funcsig %}}
|
{{< funcsig >}}
|
||||||
images.Opacity SRC OPACITY
|
images.Opacity SRC OPACITY
|
||||||
{{% /funcsig %}}
|
{{< /funcsig >}}
|
||||||
|
|
||||||
Opacity creates a filter that changes the opacity of an image.
|
Opacity creates a filter that changes the opacity of an image.
|
||||||
The OPACITY parameter must be in range (0, 1).
|
The OPACITY parameter must be in range (0, 1).
|
||||||
|
@ -84,9 +84,9 @@ This filter is most useful for target formats that support transparency, e.g. PN
|
||||||
|
|
||||||
Using the `Text` filter, you can add text to an image.
|
Using the `Text` filter, you can add text to an image.
|
||||||
|
|
||||||
{{% funcsig %}}
|
{{< funcsig >}}
|
||||||
images.Text TEXT MAP)
|
images.Text TEXT MAP)
|
||||||
{{% /funcsig %}}
|
{{< /funcsig >}}
|
||||||
|
|
||||||
The following example will add the text `Hugo rocks!` to the image with the specified color, size and position.
|
The following example will add the text `Hugo rocks!` to the image with the specified color, size and position.
|
||||||
|
|
||||||
|
@ -141,27 +141,27 @@ To add a 2px gray border to an image:
|
||||||
|
|
||||||
## Brightness
|
## Brightness
|
||||||
|
|
||||||
{{% funcsig %}}
|
{{< funcsig >}}
|
||||||
images.Brightness PERCENTAGE
|
images.Brightness PERCENTAGE
|
||||||
{{% /funcsig %}}
|
{{< /funcsig >}}
|
||||||
|
|
||||||
Brightness creates a filter that changes the brightness of an image.
|
Brightness creates a filter that changes the brightness of an image.
|
||||||
The percentage parameter must be in range (-100, 100).
|
The percentage parameter must be in range (-100, 100).
|
||||||
|
|
||||||
### ColorBalance
|
### ColorBalance
|
||||||
|
|
||||||
{{% funcsig %}}
|
{{< funcsig >}}
|
||||||
images.ColorBalance PERCENTAGERED PERCENTAGEGREEN PERCENTAGEBLUE
|
images.ColorBalance PERCENTAGERED PERCENTAGEGREEN PERCENTAGEBLUE
|
||||||
{{% /funcsig %}}
|
{{< /funcsig >}}
|
||||||
|
|
||||||
ColorBalance creates a filter that changes the color balance of an image.
|
ColorBalance creates a filter that changes the color balance of an image.
|
||||||
The percentage parameters for each color channel (red, green, blue) must be in range (-100, 500).
|
The percentage parameters for each color channel (red, green, blue) must be in range (-100, 500).
|
||||||
|
|
||||||
## Colorize
|
## Colorize
|
||||||
|
|
||||||
{{% funcsig %}}
|
{{< funcsig >}}
|
||||||
images.Colorize HUE SATURATION PERCENTAGE
|
images.Colorize HUE SATURATION PERCENTAGE
|
||||||
{{% /funcsig %}}
|
{{< /funcsig >}}
|
||||||
|
|
||||||
Colorize creates a filter that produces a colorized version of an image.
|
Colorize creates a filter that produces a colorized version of an image.
|
||||||
The hue parameter is the angle on the color wheel, typically in range (0, 360).
|
The hue parameter is the angle on the color wheel, typically in range (0, 360).
|
||||||
|
@ -170,18 +170,18 @@ The percentage parameter specifies the strength of the effect, it must be in ran
|
||||||
|
|
||||||
## Contrast
|
## Contrast
|
||||||
|
|
||||||
{{% funcsig %}}
|
{{< funcsig >}}
|
||||||
images.Contrast PERCENTAGE
|
images.Contrast PERCENTAGE
|
||||||
{{% /funcsig %}}
|
{{< /funcsig >}}
|
||||||
|
|
||||||
Contrast creates a filter that changes the contrast of an image.
|
Contrast creates a filter that changes the contrast of an image.
|
||||||
The percentage parameter must be in range (-100, 100).
|
The percentage parameter must be in range (-100, 100).
|
||||||
|
|
||||||
## Gamma
|
## Gamma
|
||||||
|
|
||||||
{{% funcsig %}}
|
{{< funcsig >}}
|
||||||
images.Gamma GAMMA
|
images.Gamma GAMMA
|
||||||
{{% /funcsig %}}
|
{{< /funcsig >}}
|
||||||
|
|
||||||
Gamma creates a filter that performs a gamma correction on an image.
|
Gamma creates a filter that performs a gamma correction on an image.
|
||||||
The gamma parameter must be positive. Gamma = 1 gives the original image.
|
The gamma parameter must be positive. Gamma = 1 gives the original image.
|
||||||
|
@ -189,75 +189,75 @@ Gamma less than 1 darkens the image and gamma greater than 1 lightens it.
|
||||||
|
|
||||||
## GaussianBlur
|
## GaussianBlur
|
||||||
|
|
||||||
{{% funcsig %}}
|
{{< funcsig >}}
|
||||||
images.GaussianBlur SIGMA
|
images.GaussianBlur SIGMA
|
||||||
{{% /funcsig %}}
|
{{< /funcsig >}}
|
||||||
|
|
||||||
GaussianBlur creates a filter that applies a gaussian blur to an image.
|
GaussianBlur creates a filter that applies a gaussian blur to an image.
|
||||||
|
|
||||||
## Grayscale
|
## Grayscale
|
||||||
|
|
||||||
{{% funcsig %}}
|
{{< funcsig >}}
|
||||||
images.Grayscale
|
images.Grayscale
|
||||||
{{% /funcsig %}}
|
{{< /funcsig >}}
|
||||||
|
|
||||||
Grayscale creates a filter that produces a grayscale version of an image.
|
Grayscale creates a filter that produces a grayscale version of an image.
|
||||||
|
|
||||||
## Hue
|
## Hue
|
||||||
|
|
||||||
{{% funcsig %}}
|
{{< funcsig >}}
|
||||||
images.Hue SHIFT
|
images.Hue SHIFT
|
||||||
{{% /funcsig %}}
|
{{< /funcsig >}}
|
||||||
|
|
||||||
Hue creates a filter that rotates the hue of an image.
|
Hue creates a filter that rotates the hue of an image.
|
||||||
The hue angle shift is typically in range -180 to 180.
|
The hue angle shift is typically in range -180 to 180.
|
||||||
|
|
||||||
## Invert
|
## Invert
|
||||||
|
|
||||||
{{% funcsig %}}
|
{{< funcsig >}}
|
||||||
images.Invert
|
images.Invert
|
||||||
{{% /funcsig %}}
|
{{< /funcsig >}}
|
||||||
|
|
||||||
Invert creates a filter that negates the colors of an image.
|
Invert creates a filter that negates the colors of an image.
|
||||||
|
|
||||||
## Pixelate
|
## Pixelate
|
||||||
|
|
||||||
{{% funcsig %}}
|
{{< funcsig >}}
|
||||||
images.Pixelate SIZE
|
images.Pixelate SIZE
|
||||||
{{% /funcsig %}}
|
{{< /funcsig >}}
|
||||||
|
|
||||||
Pixelate creates a filter that applies a pixelation effect to an image.
|
Pixelate creates a filter that applies a pixelation effect to an image.
|
||||||
|
|
||||||
## Saturation
|
## Saturation
|
||||||
|
|
||||||
{{% funcsig %}}
|
{{< funcsig >}}
|
||||||
images.Saturation PERCENTAGE
|
images.Saturation PERCENTAGE
|
||||||
{{% /funcsig %}}
|
{{< /funcsig >}}
|
||||||
|
|
||||||
Saturation creates a filter that changes the saturation of an image.
|
Saturation creates a filter that changes the saturation of an image.
|
||||||
|
|
||||||
## Sepia
|
## Sepia
|
||||||
|
|
||||||
{{% funcsig %}}
|
{{< funcsig >}}
|
||||||
images.Sepia PERCENTAGE
|
images.Sepia PERCENTAGE
|
||||||
{{% /funcsig %}}
|
{{< /funcsig >}}
|
||||||
|
|
||||||
Sepia creates a filter that produces a sepia-toned version of an image.
|
Sepia creates a filter that produces a sepia-toned version of an image.
|
||||||
|
|
||||||
## Sigmoid
|
## Sigmoid
|
||||||
|
|
||||||
{{% funcsig %}}
|
{{< funcsig >}}
|
||||||
images.Sigmoid MIDPOINT FACTOR
|
images.Sigmoid MIDPOINT FACTOR
|
||||||
{{% /funcsig %}}
|
{{< /funcsig >}}
|
||||||
|
|
||||||
Sigmoid creates a filter that changes the contrast of an image using a sigmoidal function and returns the adjusted image.
|
Sigmoid creates a filter that changes the contrast of an image using a sigmoidal function and returns the adjusted image.
|
||||||
It's a non-linear contrast change useful for photo adjustments as it preserves highlight and shadow detail.
|
It's a non-linear contrast change useful for photo adjustments as it preserves highlight and shadow detail.
|
||||||
|
|
||||||
## UnsharpMask
|
## UnsharpMask
|
||||||
|
|
||||||
{{% funcsig %}}
|
{{< funcsig >}}
|
||||||
images.UnsharpMask SIGMA AMOUNT THRESHOLD
|
images.UnsharpMask SIGMA AMOUNT THRESHOLD
|
||||||
{{% /funcsig %}}
|
{{< /funcsig >}}
|
||||||
|
|
||||||
UnsharpMask creates a filter that sharpens an image.
|
UnsharpMask creates a filter that sharpens an image.
|
||||||
The sigma parameter is used in a gaussian function and affects the radius of effect.
|
The sigma parameter is used in a gaussian function and affects the radius of effect.
|
||||||
|
@ -269,9 +269,9 @@ The threshold parameter controls the minimum brightness change that will be shar
|
||||||
|
|
||||||
### Filter
|
### Filter
|
||||||
|
|
||||||
{{% funcsig %}}
|
{{< funcsig >}}
|
||||||
IMAGE | images.Filter FILTERS...
|
IMAGE | images.Filter FILTERS...
|
||||||
{{% /funcsig %}}
|
{{< /funcsig >}}
|
||||||
|
|
||||||
Can be used to apply a set of filters to an image:
|
Can be used to apply a set of filters to an image:
|
||||||
|
|
||||||
|
@ -287,9 +287,9 @@ Parses the image and returns the height, width, and color model.
|
||||||
|
|
||||||
The `imageConfig` function takes a single parameter, a file path (_string_) relative to the _project's root directory_, with or without a leading slash.
|
The `imageConfig` function takes a single parameter, a file path (_string_) relative to the _project's root directory_, with or without a leading slash.
|
||||||
|
|
||||||
{{% funcsig %}}
|
{{< funcsig >}}
|
||||||
images.ImageConfig PATH
|
images.ImageConfig PATH
|
||||||
{{% /funcsig %}}
|
{{< /funcsig >}}
|
||||||
|
|
||||||
```go-html-template
|
```go-html-template
|
||||||
{{ with (imageConfig "favicon.ico") }}
|
{{ with (imageConfig "favicon.ico") }}
|
||||||
|
|
|
@ -5,7 +5,7 @@
|
||||||
{{- if $.Params.function.returnType }}
|
{{- if $.Params.function.returnType }}
|
||||||
{{- $signature = printf "%s ⟼ %s" . $.Params.function.returnType }}
|
{{- $signature = printf "%s ⟼ %s" . $.Params.function.returnType }}
|
||||||
{{- end }}
|
{{- end }}
|
||||||
<pre class="f5 mb4 ph3 pv2 bg-light-gray" style="border-left:4px solid #0594CB;">
|
<pre class="f5 mb4 ph3 pv2 bg-light-gray overflow-x-auto" style="border-left:4px solid #0594CB;">
|
||||||
{{- $signature -}}
|
{{- $signature -}}
|
||||||
</pre>
|
</pre>
|
||||||
{{- end }}
|
{{- end }}
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
<h4 class="minor mb1 pt2 primary-color-dark">Syntax</h4>
|
<h4 class="minor mb1 pt2 primary-color-dark">Syntax</h4>
|
||||||
<pre class="f5 mb4 ph3 pv2 bg-light-gray" style="border-left:4px solid #0594CB;">
|
<pre class="f5 mb4 ph3 pv2 bg-light-gray overflow-x-auto" style="border-left:4px solid #0594CB;">
|
||||||
{{- .Inner -}}
|
{{- .Inner -}}
|
||||||
</pre>
|
</pre>
|
Loading…
Reference in a new issue