mirror of
https://github.com/gohugoio/hugo.git
synced 2026-08-24 15:28:54 +00:00
Miscellaneous corrections
This commit is contained in:
@@ -18,8 +18,8 @@ With a decimal (base 10) input:
|
||||
{{ int 11 }} → 11 (int)
|
||||
{{ int "11" }} → 11 (int)
|
||||
|
||||
{{ int 11/1 }} → 11 (int)
|
||||
{{ int 11/9 }} → 11 (int)
|
||||
{{ int 11.1 }} → 11 (int)
|
||||
{{ int 11.9 }} → 11 (int)
|
||||
```
|
||||
|
||||
With a binary (base 2) input:
|
||||
|
||||
@@ -17,10 +17,20 @@ The following shows `after` being used in conjunction with the [`slice`]function
|
||||
|
||||
```go-html-template
|
||||
{{ $data := slice "one" "two" "three" "four" }}
|
||||
{{ range after 2 $data }}
|
||||
{{ . }}
|
||||
{{ end }}
|
||||
→ ["three", "four"]
|
||||
<ul>
|
||||
{{ range after 2 $data }}
|
||||
<li>{{ . }}</li>
|
||||
{{ end }}
|
||||
</ul>
|
||||
```
|
||||
|
||||
The template above is rendered to:
|
||||
|
||||
```html
|
||||
<ul>
|
||||
<li>three</li>
|
||||
<li>four</li>
|
||||
</ul>
|
||||
```
|
||||
|
||||
## Example of `after` with `first`: 2nd–4th most recent articles
|
||||
|
||||
@@ -15,7 +15,19 @@ action:
|
||||
aliases: [/functions/dict]
|
||||
---
|
||||
|
||||
`dict` is especially useful for passing more than one value to a partial template.
|
||||
```go-html-template
|
||||
{{ $m := dict "a" 1 "b" 2 }}
|
||||
```
|
||||
|
||||
The above produces this data structure:
|
||||
|
||||
```json
|
||||
{
|
||||
"a": 1,
|
||||
"b": 2
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
Note that the `key` can be either a `string` or a `string slice`. The latter is useful to create a deeply nested structure, e.g.:
|
||||
|
||||
@@ -25,7 +37,6 @@ Note that the `key` can be either a `string` or a `string slice`. The latter is
|
||||
|
||||
The above produces this data structure:
|
||||
|
||||
|
||||
```json
|
||||
{
|
||||
"a": {
|
||||
@@ -36,7 +47,7 @@ The above produces this data structure:
|
||||
}
|
||||
```
|
||||
|
||||
## Example: using `dict` to pass multiple values to a `partial`
|
||||
## Pass values to a partial template
|
||||
|
||||
The partial below creates an SVG and expects `fill`, `height` and `width` from the caller:
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
---
|
||||
title: collections.First
|
||||
description: Slices an array to the first N elements.
|
||||
description: Returns the given collection, limited to the first N elements.
|
||||
categories: []
|
||||
keywords: []
|
||||
action:
|
||||
@@ -9,38 +9,28 @@ action:
|
||||
- functions/collections/After
|
||||
- functions/collections/Last
|
||||
returnType: any
|
||||
signatures: [collections.First LIMIT COLLECTION]
|
||||
signatures: [collections.First N COLLECTION]
|
||||
aliases: [/functions/first]
|
||||
---
|
||||
|
||||
`first` works in a similar manner to the [`limit` keyword in SQL][limitkeyword]. It reduces the array to only the `first N` elements. It takes the array and number of elements as input.
|
||||
|
||||
`first` takes two arguments:
|
||||
|
||||
1. `number of elements`
|
||||
2. `array` *or* `slice of maps or structs`
|
||||
|
||||
{{< code file="layout/_default/section.html" >}}
|
||||
{{ range first 10 .Pages }}
|
||||
```go-html-template
|
||||
{{ range first 5 .Pages }}
|
||||
{{ .Render "summary" }}
|
||||
{{ end }}
|
||||
{{< /code >}}
|
||||
```
|
||||
|
||||
*Note: Exclusive to `first`, LIMIT can be '0' to return an empty array.*
|
||||
Set `N` to zero to return an empty collection.
|
||||
|
||||
## `first` and `where` Together
|
||||
```go-html-template
|
||||
{{ $emptyPageCollection := first 0 .Pages}}
|
||||
```
|
||||
|
||||
Using `first` and [`where`] together can be very
|
||||
powerful. Below snippet gets a list of posts only from [main
|
||||
sections], sorts it by the `title` parameter, and then
|
||||
ranges through only the first 5 posts in that list:
|
||||
Use `first` and [`where`] together.
|
||||
|
||||
{{< code file="first-and-where-together.html" >}}
|
||||
{{ range first 5 (where site.RegularPages "Type" "in" site.Params.mainSections).ByTitle }}
|
||||
{{ .Content }}
|
||||
```go-html-template
|
||||
{{ range where .Pages "Section" "articles" | first 5 }}
|
||||
{{ .Render "summary" }}
|
||||
{{ end }}
|
||||
{{< /code >}}
|
||||
```
|
||||
|
||||
[limitkeyword]: https://www.techonthenet.com/sql/select_limit.php
|
||||
[`where`]: /functions/collections/where
|
||||
[main sections]: /functions/collections/where#mainsections
|
||||
|
||||
@@ -13,9 +13,8 @@ action:
|
||||
signatures: [collections.Intersect SET1 SET2]
|
||||
aliases: [/functions/intersect]
|
||||
---
|
||||
A useful example is to use it as `AND` filters when combined with where:
|
||||
|
||||
## AND filter in where query
|
||||
A useful example is to use it as `AND` filters when combined with where:
|
||||
|
||||
```go-html-template
|
||||
{{ $pages := where .Site.RegularPages "Type" "not in" (slice "page" "about") }}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
---
|
||||
title: collections.Last
|
||||
description: Slices an array to the last N elements.
|
||||
description: Returns the given collection, limited to the last N elements.
|
||||
categories: []
|
||||
keywords: []
|
||||
action:
|
||||
@@ -9,7 +9,7 @@ action:
|
||||
- functions/collections/After
|
||||
- functions/collections/First
|
||||
returnType: any
|
||||
signatures: [collections.Last INDEX COLLECTION]
|
||||
signatures: [collections.Last N COLLECTION]
|
||||
aliases: [/functions/last]
|
||||
---
|
||||
|
||||
@@ -18,3 +18,17 @@ aliases: [/functions/last]
|
||||
{{ .Render "summary" }}
|
||||
{{ end }}
|
||||
```
|
||||
|
||||
Set `N` to zero to return an empty collection.
|
||||
|
||||
```go-html-template
|
||||
{{ $emptyPageCollection := last 0 .Pages}}
|
||||
```
|
||||
|
||||
Use `last` and [`where`] together.
|
||||
|
||||
```go-html-template
|
||||
{{ range where .Pages "Section" "articles" | last 5 }}
|
||||
{{ .Render "summary" }}
|
||||
{{ end }}
|
||||
```
|
||||
|
||||
@@ -36,3 +36,11 @@ Iterate over a sequence of integers:
|
||||
{{ end }}
|
||||
{{ $product }} → 24
|
||||
```
|
||||
|
||||
The example above is contrived. To calculate the product of 2 or more numbers, use the [`math.Product`] function:
|
||||
|
||||
```go-html-template
|
||||
{{ math.Product (seq 4) }} → 24
|
||||
```
|
||||
|
||||
[`math.Product`]: /functions/math/product
|
||||
|
||||
@@ -107,17 +107,11 @@ Victor Marius Jean
|
||||
|
||||
## Sort a page collection
|
||||
|
||||
Although you can use the `sort` function to sort a page collection, Hugo provides [built-in methods for sorting page collections] by:
|
||||
{{% note %}}
|
||||
Although you can use the `sort` function to sort a page collection, Hugo provides [sorting and grouping methods] as well.
|
||||
|
||||
- weight
|
||||
- linktitle
|
||||
- title
|
||||
- front matter parameter
|
||||
- date
|
||||
- expiration date
|
||||
- last modified date
|
||||
- publish date
|
||||
- length
|
||||
[sorting and grouping methods]: /methods/pages
|
||||
{{% /note %}}
|
||||
|
||||
In this contrived example, sort the site's regular pages by `.Type` in descending order:
|
||||
|
||||
@@ -126,5 +120,3 @@ In this contrived example, sort the site's regular pages by `.Type` in descendin
|
||||
<h2><a href="{{ .RelPermalink }}">{{ .Title }}</a></h2>
|
||||
{{ end }}
|
||||
```
|
||||
|
||||
[built-in methods for sorting page collections]: /templates/lists/#sort-content
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
---
|
||||
title: collections.Union
|
||||
description: Given two arrays or slices, returns a new array that contains the elements or objects that belong to either or both arrays/slices.
|
||||
description: Given two arrays or slices, returns a new array that contains the elements that belong to either or both arrays/slices.
|
||||
categories: []
|
||||
keywords: []
|
||||
action:
|
||||
@@ -15,7 +15,7 @@ related:
|
||||
aliases: [/functions/union]
|
||||
---
|
||||
|
||||
Given two arrays (or slices) A and B, this function will return a new array that contains the elements or objects that belong to either A or to B or to both. The elements supported are strings, integers, and floats (only float64).
|
||||
Given two arrays (or slices) A and B, this function will return a new array that contains the elements or objects that belong to either A or to B or to both.
|
||||
|
||||
```go-html-template
|
||||
{{ union (slice 1 2 3) (slice 3 4 5) }}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
---
|
||||
title: collections.Uniq
|
||||
description: Takes in a slice or array and returns a slice with duplicate elements removed.
|
||||
description: Returns the given collection, removing duplicate elements.
|
||||
categories: []
|
||||
keywords: []
|
||||
action:
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
---
|
||||
title: collections.Where
|
||||
description: Filters an array to only the elements containing a matching value for a given field.
|
||||
description: Returns the given collection, removing elements that do not match the given field.
|
||||
categories: []
|
||||
keywords: []
|
||||
action:
|
||||
@@ -17,11 +17,7 @@ aliases: [/functions/where]
|
||||
toc: true
|
||||
---
|
||||
|
||||
`where` filters an array to only the elements containing a matching
|
||||
value for a given field.
|
||||
|
||||
It works in a similar manner to the [`where` keyword in
|
||||
SQL][wherekeyword].
|
||||
The `where` function is similar to the SQL [`where`] keyword.
|
||||
|
||||
```go-html-template
|
||||
{{ range where .Pages "Section" "foo" }}
|
||||
@@ -38,7 +34,7 @@ series: golang
|
||||
|
||||
```go-html-template
|
||||
{{ range where .Site.Pages "Params.series" "golang" }}
|
||||
{{ .Content }}
|
||||
{{ .Content }}
|
||||
{{ end }}
|
||||
```
|
||||
|
||||
@@ -46,7 +42,7 @@ It can also be used with the logical operators `!=`, `>=`, `in`, etc. Without an
|
||||
|
||||
```go-html-template
|
||||
{{ range where .Pages "Section" "!=" "foo" }}
|
||||
{{ .Content }}
|
||||
{{ .Content }}
|
||||
{{ end }}
|
||||
```
|
||||
|
||||
@@ -132,7 +128,7 @@ then ranges through only the first 5 posts in that list:
|
||||
|
||||
```go-html-template
|
||||
{{ range first 5 (where site.RegularPages "Type" "in" site.Params.mainSections) }}
|
||||
{{ .Content }}
|
||||
{{ .Content }}
|
||||
{{ end }}
|
||||
```
|
||||
|
||||
@@ -157,7 +153,7 @@ Only the following operators are available for `nil`
|
||||
|
||||
```go-html-template
|
||||
{{ range where .Pages "Params.specialpost" "!=" nil }}
|
||||
{{ .Content }}
|
||||
{{ .Content }}
|
||||
{{ end }}
|
||||
```
|
||||
|
||||
@@ -183,4 +179,4 @@ The user can override the default:
|
||||
{{< /code-toggle >}}
|
||||
|
||||
[intersect]: /functions/collections/intersect
|
||||
[wherekeyword]: https://www.techonthenet.com/sql/where.php
|
||||
[`where`]: https://www.techonthenet.com/sql/where.php
|
||||
|
||||
@@ -19,7 +19,7 @@ The `RawContent` method on a `Page` object returns the raw content. The raw cont
|
||||
{{ .RawContent }}
|
||||
```
|
||||
|
||||
This is useful when rendering a page in a plain text [content format].
|
||||
This is useful when rendering a page in a plain text [output format].
|
||||
|
||||
{{% note %}}
|
||||
[Shortcodes] within the content are not rendered. To get the raw content with shortcodes rendered, use the [`RenderShortcodes`] method on a `Page` object.
|
||||
@@ -28,4 +28,4 @@ This is useful when rendering a page in a plain text [content format].
|
||||
[`RenderShortcodes`]: /methods/page/rendershortcodes
|
||||
{{% /note %}}
|
||||
|
||||
[content format]: /templates/output-formats
|
||||
[output format]: /templates/output-formats
|
||||
|
||||
@@ -95,7 +95,7 @@ You can now render the list of recordings for all the bass players in a template
|
||||
|
||||
```go-html-template
|
||||
{{ range $.Site.Data.jazz.bass }}
|
||||
{{ partial "artist.html" . }}
|
||||
{{ partial "artist.html" . }}
|
||||
{{ end }}
|
||||
```
|
||||
|
||||
|
||||
@@ -215,7 +215,7 @@ element's index.
|
||||
|
||||
```go-html-template
|
||||
{{ range $elem_index, $elem_val := $array }}
|
||||
{{ $elem_index }} -- {{ $elem_val }}
|
||||
{{ $elem_index }} -- {{ $elem_val }}
|
||||
{{ end }}
|
||||
```
|
||||
|
||||
@@ -226,7 +226,7 @@ key.
|
||||
|
||||
```go-html-template
|
||||
{{ range $elem_key, $elem_val := $map }}
|
||||
{{ $elem_key }} -- {{ $elem_val }}
|
||||
{{ $elem_key }} -- {{ $elem_val }}
|
||||
{{ end }}
|
||||
```
|
||||
|
||||
|
||||
@@ -78,7 +78,7 @@ The following example shows how to create `.Paginator` before its used:
|
||||
{{ $paginator := .Paginate (where .Pages "Type" "posts") }}
|
||||
{{ template "_internal/pagination.html" . }}
|
||||
{{ range $paginator.Pages }}
|
||||
{{ .Title }}
|
||||
{{ .Title }}
|
||||
{{ end }}
|
||||
```
|
||||
|
||||
@@ -87,7 +87,7 @@ Without the `where` filter, the above example is even simpler:
|
||||
```go-html-template
|
||||
{{ template "_internal/pagination.html" . }}
|
||||
{{ range .Paginator.Pages }}
|
||||
{{ .Title }}
|
||||
{{ .Title }}
|
||||
{{ end }}
|
||||
```
|
||||
|
||||
|
||||
Reference in New Issue
Block a user