Skip to content

Commit

Permalink
Miscellaneous corrections
Browse files Browse the repository at this point in the history
  • Loading branch information
jmooring authored Nov 6, 2023
1 parent 5086665 commit d9e9811
Show file tree
Hide file tree
Showing 15 changed files with 91 additions and 71 deletions.
4 changes: 2 additions & 2 deletions content/en/functions/cast/ToInt.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
18 changes: 14 additions & 4 deletions content/en/functions/collections/After.md
Original file line number Diff line number Diff line change
Expand Up @@ -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&ndash;4th most recent articles
Expand Down
17 changes: 14 additions & 3 deletions content/en/functions/collections/Dictionary.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.:

Expand All @@ -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": {
Expand All @@ -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:

Expand Down
38 changes: 14 additions & 24 deletions content/en/functions/collections/First.md
Original file line number Diff line number Diff line change
@@ -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:
Expand All @@ -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
3 changes: 1 addition & 2 deletions content/en/functions/collections/Intersect.md
Original file line number Diff line number Diff line change
Expand Up @@ -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") }}
Expand Down
18 changes: 16 additions & 2 deletions content/en/functions/collections/Last.md
Original file line number Diff line number Diff line change
@@ -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:
Expand All @@ -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]
---

Expand All @@ -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 }}
```
8 changes: 8 additions & 0 deletions content/en/functions/collections/Seq.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
18 changes: 5 additions & 13 deletions content/en/functions/collections/Sort.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:

- weight
- linktitle
- title
- front matter parameter
- date
- expiration date
- last modified date
- publish date
- length
{{% note %}}
Although you can use the `sort` function to sort a page collection, Hugo provides [sorting and grouping methods] as well.

[sorting and grouping methods]: /methods/pages
{{% /note %}}

In this contrived example, sort the site's regular pages by `.Type` in descending order:

Expand All @@ -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
4 changes: 2 additions & 2 deletions content/en/functions/collections/Union.md
Original file line number Diff line number Diff line change
@@ -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:
Expand All @@ -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) }}
Expand Down
2 changes: 1 addition & 1 deletion content/en/functions/collections/Uniq.md
Original file line number Diff line number Diff line change
@@ -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:
Expand Down
18 changes: 7 additions & 11 deletions content/en/functions/collections/Where.md
Original file line number Diff line number Diff line change
@@ -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:
Expand All @@ -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" }}
Expand All @@ -38,15 +34,15 @@ series: golang

```go-html-template
{{ range where .Site.Pages "Params.series" "golang" }}
{{ .Content }}
{{ .Content }}
{{ end }}
```

It can also be used with the logical operators `!=`, `>=`, `in`, etc. Without an operator, `where` compares a given field with a matching value equivalent to `=`.

```go-html-template
{{ range where .Pages "Section" "!=" "foo" }}
{{ .Content }}
{{ .Content }}
{{ end }}
```

Expand Down Expand Up @@ -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 }}
```

Expand All @@ -157,7 +153,7 @@ Only the following operators are available for `nil`

```go-html-template
{{ range where .Pages "Params.specialpost" "!=" nil }}
{{ .Content }}
{{ .Content }}
{{ end }}
```

Expand All @@ -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
4 changes: 2 additions & 2 deletions content/en/methods/page/RawContent.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
2 changes: 1 addition & 1 deletion content/en/templates/data-templates.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}
```

Expand Down
4 changes: 2 additions & 2 deletions content/en/templates/introduction.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}
```

Expand All @@ -226,7 +226,7 @@ key.

```go-html-template
{{ range $elem_key, $elem_val := $map }}
{{ $elem_key }} -- {{ $elem_val }}
{{ $elem_key }} -- {{ $elem_val }}
{{ end }}
```

Expand Down
4 changes: 2 additions & 2 deletions content/en/templates/pagination.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}
```

Expand All @@ -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 }}
```

Expand Down

0 comments on commit d9e9811

Please sign in to comment.