mirror of
https://github.com/tiennm99/hugoDocs.git
synced 2026-10-14 20:17:17 +00:00
Miscellaneous corrections
This commit is contained in:
1 parent
508666575f
commit
d9e9811474
15 files changed
+90
-70
No files matched your search
@@ -18,8 +18,8 @@ With a decimal (base 10) input:
|
|||||||
{{ int 11 }} → 11 (int)
|
{{ int 11 }} → 11 (int)
|
||||||
{{ int "11" }} → 11 (int)
|
{{ int "11" }} → 11 (int)
|
||||||
|
|
||||||
{{ int 11/1 }} → 11 (int)
|
{{ int 11.1 }} → 11 (int)
|
||||||
{{ int 11/9 }} → 11 (int)
|
{{ int 11.9 }} → 11 (int)
|
||||||
```
|
```
|
||||||
|
|
||||||
With a binary (base 2) input:
|
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
|
```go-html-template
|
||||||
{{ $data := slice "one" "two" "three" "four" }}
|
{{ $data := slice "one" "two" "three" "four" }}
|
||||||
{{ range after 2 $data }}
|
<ul>
|
||||||
{{ . }}
|
{{ range after 2 $data }}
|
||||||
{{ end }}
|
<li>{{ . }}</li>
|
||||||
→ ["three", "four"]
|
{{ 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
|
## Example of `after` with `first`: 2nd–4th most recent articles
|
||||||
|
|||||||
@@ -15,7 +15,19 @@ action:
|
|||||||
aliases: [/functions/dict]
|
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.:
|
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:
|
The above produces this data structure:
|
||||||
|
|
||||||
|
|
||||||
```json
|
```json
|
||||||
{
|
{
|
||||||
"a": {
|
"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:
|
The partial below creates an SVG and expects `fill`, `height` and `width` from the caller:
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
---
|
---
|
||||||
title: collections.First
|
title: collections.First
|
||||||
description: Slices an array to the first N elements.
|
description: Returns the given collection, limited to the first N elements.
|
||||||
categories: []
|
categories: []
|
||||||
keywords: []
|
keywords: []
|
||||||
action:
|
action:
|
||||||
@@ -9,38 +9,28 @@ action:
|
|||||||
- functions/collections/After
|
- functions/collections/After
|
||||||
- functions/collections/Last
|
- functions/collections/Last
|
||||||
returnType: any
|
returnType: any
|
||||||
signatures: [collections.First LIMIT COLLECTION]
|
signatures: [collections.First N COLLECTION]
|
||||||
aliases: [/functions/first]
|
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.
|
```go-html-template
|
||||||
|
{{ range first 5 .Pages }}
|
||||||
`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 }}
|
|
||||||
{{ .Render "summary" }}
|
{{ .Render "summary" }}
|
||||||
{{ end }}
|
{{ 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
|
Use `first` and [`where`] together.
|
||||||
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:
|
|
||||||
|
|
||||||
{{< code file="first-and-where-together.html" >}}
|
```go-html-template
|
||||||
{{ range first 5 (where site.RegularPages "Type" "in" site.Params.mainSections).ByTitle }}
|
{{ range where .Pages "Section" "articles" | first 5 }}
|
||||||
{{ .Content }}
|
{{ .Render "summary" }}
|
||||||
{{ end }}
|
{{ end }}
|
||||||
{{< /code >}}
|
```
|
||||||
|
|
||||||
[limitkeyword]: https://www.techonthenet.com/sql/select_limit.php
|
|
||||||
[`where`]: /functions/collections/where
|
[`where`]: /functions/collections/where
|
||||||
[main sections]: /functions/collections/where#mainsections
|
|
||||||
@@ -13,9 +13,8 @@ action:
|
|||||||
signatures: [collections.Intersect SET1 SET2]
|
signatures: [collections.Intersect SET1 SET2]
|
||||||
aliases: [/functions/intersect]
|
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
|
```go-html-template
|
||||||
{{ $pages := where .Site.RegularPages "Type" "not in" (slice "page" "about") }}
|
{{ $pages := where .Site.RegularPages "Type" "not in" (slice "page" "about") }}
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
---
|
---
|
||||||
title: collections.Last
|
title: collections.Last
|
||||||
description: Slices an array to the last N elements.
|
description: Returns the given collection, limited to the last N elements.
|
||||||
categories: []
|
categories: []
|
||||||
keywords: []
|
keywords: []
|
||||||
action:
|
action:
|
||||||
@@ -9,7 +9,7 @@ action:
|
|||||||
- functions/collections/After
|
- functions/collections/After
|
||||||
- functions/collections/First
|
- functions/collections/First
|
||||||
returnType: any
|
returnType: any
|
||||||
signatures: [collections.Last INDEX COLLECTION]
|
signatures: [collections.Last N COLLECTION]
|
||||||
aliases: [/functions/last]
|
aliases: [/functions/last]
|
||||||
---
|
---
|
||||||
|
|
||||||
@@ -18,3 +18,17 @@ aliases: [/functions/last]
|
|||||||
{{ .Render "summary" }}
|
{{ .Render "summary" }}
|
||||||
{{ end }}
|
{{ 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 }}
|
{{ end }}
|
||||||
{{ $product }} → 24
|
{{ $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
|
## 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
|
[sorting and grouping methods]: /methods/pages
|
||||||
- linktitle
|
{{% /note %}}
|
||||||
- title
|
|
||||||
- front matter parameter
|
|
||||||
- date
|
|
||||||
- expiration date
|
|
||||||
- last modified date
|
|
||||||
- publish date
|
|
||||||
- length
|
|
||||||
|
|
||||||
In this contrived example, sort the site's regular pages by `.Type` in descending order:
|
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>
|
<h2><a href="{{ .RelPermalink }}">{{ .Title }}</a></h2>
|
||||||
{{ end }}
|
{{ end }}
|
||||||
```
|
```
|
||||||
|
|
||||||
[built-in methods for sorting page collections]: /templates/lists/#sort-content
|
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
---
|
---
|
||||||
title: collections.Union
|
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: []
|
categories: []
|
||||||
keywords: []
|
keywords: []
|
||||||
action:
|
action:
|
||||||
@@ -15,7 +15,7 @@ related:
|
|||||||
aliases: [/functions/union]
|
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
|
```go-html-template
|
||||||
{{ union (slice 1 2 3) (slice 3 4 5) }}
|
{{ union (slice 1 2 3) (slice 3 4 5) }}
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
---
|
---
|
||||||
title: collections.Uniq
|
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: []
|
categories: []
|
||||||
keywords: []
|
keywords: []
|
||||||
action:
|
action:
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
---
|
---
|
||||||
title: collections.Where
|
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: []
|
categories: []
|
||||||
keywords: []
|
keywords: []
|
||||||
action:
|
action:
|
||||||
@@ -17,11 +17,7 @@ aliases: [/functions/where]
|
|||||||
toc: true
|
toc: true
|
||||||
---
|
---
|
||||||
|
|
||||||
`where` filters an array to only the elements containing a matching
|
The `where` function is similar to the SQL [`where`] keyword.
|
||||||
value for a given field.
|
|
||||||
|
|
||||||
It works in a similar manner to the [`where` keyword in
|
|
||||||
SQL][wherekeyword].
|
|
||||||
|
|
||||||
```go-html-template
|
```go-html-template
|
||||||
{{ range where .Pages "Section" "foo" }}
|
{{ range where .Pages "Section" "foo" }}
|
||||||
@@ -38,7 +34,7 @@ series: golang
|
|||||||
|
|
||||||
```go-html-template
|
```go-html-template
|
||||||
{{ range where .Site.Pages "Params.series" "golang" }}
|
{{ range where .Site.Pages "Params.series" "golang" }}
|
||||||
{{ .Content }}
|
{{ .Content }}
|
||||||
{{ end }}
|
{{ end }}
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -46,7 +42,7 @@ It can also be used with the logical operators `!=`, `>=`, `in`, etc. Without an
|
|||||||
|
|
||||||
```go-html-template
|
```go-html-template
|
||||||
{{ range where .Pages "Section" "!=" "foo" }}
|
{{ range where .Pages "Section" "!=" "foo" }}
|
||||||
{{ .Content }}
|
{{ .Content }}
|
||||||
{{ end }}
|
{{ end }}
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -132,7 +128,7 @@ then ranges through only the first 5 posts in that list:
|
|||||||
|
|
||||||
```go-html-template
|
```go-html-template
|
||||||
{{ range first 5 (where site.RegularPages "Type" "in" site.Params.mainSections) }}
|
{{ range first 5 (where site.RegularPages "Type" "in" site.Params.mainSections) }}
|
||||||
{{ .Content }}
|
{{ .Content }}
|
||||||
{{ end }}
|
{{ end }}
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -157,7 +153,7 @@ Only the following operators are available for `nil`
|
|||||||
|
|
||||||
```go-html-template
|
```go-html-template
|
||||||
{{ range where .Pages "Params.specialpost" "!=" nil }}
|
{{ range where .Pages "Params.specialpost" "!=" nil }}
|
||||||
{{ .Content }}
|
{{ .Content }}
|
||||||
{{ end }}
|
{{ end }}
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -183,4 +179,4 @@ The user can override the default:
|
|||||||
{{< /code-toggle >}}
|
{{< /code-toggle >}}
|
||||||
|
|
||||||
[intersect]: /functions/collections/intersect
|
[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 }}
|
{{ .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 %}}
|
{{% note %}}
|
||||||
[Shortcodes] within the content are not rendered. To get the raw content with shortcodes rendered, use the [`RenderShortcodes`] method on a `Page` object.
|
[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
|
[`RenderShortcodes`]: /methods/page/rendershortcodes
|
||||||
{{% /note %}}
|
{{% /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
|
```go-html-template
|
||||||
{{ range $.Site.Data.jazz.bass }}
|
{{ range $.Site.Data.jazz.bass }}
|
||||||
{{ partial "artist.html" . }}
|
{{ partial "artist.html" . }}
|
||||||
{{ end }}
|
{{ end }}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|||||||
@@ -215,7 +215,7 @@ element's index.
|
|||||||
|
|
||||||
```go-html-template
|
```go-html-template
|
||||||
{{ range $elem_index, $elem_val := $array }}
|
{{ range $elem_index, $elem_val := $array }}
|
||||||
{{ $elem_index }} -- {{ $elem_val }}
|
{{ $elem_index }} -- {{ $elem_val }}
|
||||||
{{ end }}
|
{{ end }}
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -226,7 +226,7 @@ key.
|
|||||||
|
|
||||||
```go-html-template
|
```go-html-template
|
||||||
{{ range $elem_key, $elem_val := $map }}
|
{{ range $elem_key, $elem_val := $map }}
|
||||||
{{ $elem_key }} -- {{ $elem_val }}
|
{{ $elem_key }} -- {{ $elem_val }}
|
||||||
{{ end }}
|
{{ end }}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|||||||
@@ -78,7 +78,7 @@ The following example shows how to create `.Paginator` before its used:
|
|||||||
{{ $paginator := .Paginate (where .Pages "Type" "posts") }}
|
{{ $paginator := .Paginate (where .Pages "Type" "posts") }}
|
||||||
{{ template "_internal/pagination.html" . }}
|
{{ template "_internal/pagination.html" . }}
|
||||||
{{ range $paginator.Pages }}
|
{{ range $paginator.Pages }}
|
||||||
{{ .Title }}
|
{{ .Title }}
|
||||||
{{ end }}
|
{{ end }}
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -87,7 +87,7 @@ Without the `where` filter, the above example is even simpler:
|
|||||||
```go-html-template
|
```go-html-template
|
||||||
{{ template "_internal/pagination.html" . }}
|
{{ template "_internal/pagination.html" . }}
|
||||||
{{ range .Paginator.Pages }}
|
{{ range .Paginator.Pages }}
|
||||||
{{ .Title }}
|
{{ .Title }}
|
||||||
{{ end }}
|
{{ end }}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user