Merge commit 'e509cac533600cf4fa8382c9cdab78ddd82db688'

This commit is contained in:
Bjørn Erik Pedersen
2023-10-20 09:43:56 +02:00
298 changed files with 4568 additions and 1991 deletions

View File

@@ -0,0 +1,47 @@
---
title: compare.Conditional
linkTitle: cond
description: Returns one of two arguments depending on the value of the control argument.
categories: [functions]
keywords: []
menu:
docs:
parent: functions
function:
aliases: [cond]
returnType: any
signatures: [compare.Conditional CONTROL ARG1 ARG2]
relatedFunctions:
- compare.Conditional
- compare.Default
aliases: [/functions/cond]
---
The CONTROL argument is a boolean value that indicates whether the function should return ARG1 or ARG2. If CONTROL is `true`, the function returns ARG1. Otherwise, the function returns ARG2.
```go-html-template
{{ $qty := 42 }}
{{ cond (le $qty 3) "few" "many" }} → "many"
```
The CONTROL argument must be either `true` or `false`. To cast a non-boolean value to boolean, pass it through the `not` operator twice.
```go-html-template
{{ cond (42 | not | not) "truthy" "falsy" }} → "truthy"
{{ cond ("" | not | not) "truthy" "falsy" }} → "falsy"
```
{{% note %}}
Unlike [ternary operators] in other languages, the `cond` function does not perform [short-circuit evaluation]. The function evaluates both ARG1 and ARG2, regardless of the CONTROL value.
[short-circuit evaluation]: https://en.wikipedia.org/wiki/Short-circuit_evaluation
[ternary operators]: https://en.wikipedia.org/wiki/Ternary_conditional_operator
{{% /note %}}
Due to the absence of short-circuit evaluation, these examples throw an error:
```go-html-template
{{ cond true "true" (div 1 0) }}
{{ cond false (div 1 0) "false" }}
```

View File

@@ -0,0 +1,88 @@
---
title: compare.Default
linkTitle: default
description: Allows setting a default value that can be returned if a first value is not set.
categories: [functions]
keywords: []
menu:
docs:
parent: functions
function:
aliases: [default]
returnType: any
signatures: [compare.Default DEFAULT INPUT]
relatedFunctions:
- compare.Conditional
- compare.Default
aliases: [/functions/default]
---
`default` checks whether a given value is set and returns a default value if it is not. *Set* in this context means different things depending on the data type:
* non-zero for numeric types and times
* non-zero length for strings, arrays, slices, and maps
* any boolean or struct value
* non-nil for any other types
`default` function examples reference the following content page:
{{< code file="content/posts/default-function-example.md" >}}
---
title: Sane Defaults
seo_title:
date: 2017-02-18
font:
oldparam: The default function helps make your templating DRYer.
newparam:
---
{{< /code >}}
`default` can be written in more than one way:
```go-html-template
{{ .Params.font | default "Roboto" }}
{{ default "Roboto" .Params.font }}
```
Both of the above `default` function calls return `Roboto`.
A `default` value, however, does not need to be hard coded like the previous example. The `default` value can be a variable or pulled directly from the front matter using dot notation:
```go-html-template
{{ $old := .Params.oldparam }}
<p>{{ .Params.newparam | default $old }}</p>
```
Which would return:
```html
<p>The default function helps make your templating DRYer.</p>
```
And then using dot notation
```go-html-template
<title>{{ .Params.seo_title | default .Title }}</title>
```
Which would return
```html
<title>Sane Defaults</title>
```
The following have equivalent return values but are far less terse. This demonstrates the utility of `default`:
Using `if`:
```go-html-template
<title>{{ if .Params.seo_title }}{{ .Params.seo_title }}{{ else }}{{ .Title }}{{ end }}</title>
=> Sane Defaults
```
Using `with`:
```go-html-template
<title>{{ with .Params.seo_title }}{{ . }}{{ else }}{{ .Title }}{{ end }}</title>
=> Sane Defaults
```

View File

@@ -0,0 +1,32 @@
---
title: compare.Eq
linkTitle: eq
description: Returns the boolean truth of arg1 == arg2 || arg1 == arg3.
categories: [functions]
keywords: []
menu:
docs:
parent: functions
function:
aliases: [eq]
returnType: bool
signatures: ['compare.Eq ARG1 ARG2 [ARG...]']
relatedFunctions:
- compare.Eq
- compare.Ge
- compare.Gt
- compare.Le
- compare.Lt
- compare.Ne
aliases: [/functions/eq]
---
```go-html-template
{{ eq 1 1 }} → true
{{ eq 1 2 }} → false
{{ eq 1 1 1 }} → true
{{ eq 1 1 2 }} → true
{{ eq 1 2 1 }} → true
{{ eq 1 2 2 }} → false
```

View File

@@ -0,0 +1,37 @@
---
title: compare.Ge
linkTitle: ge
description: Returns the boolean truth of arg1 >= arg2 && arg1 >= arg3.
categories: [functions]
keywords: []
menu:
docs:
parent: functions
function:
aliases: [ge]
returnType: bool
signatures: ['compare.Ge ARG1 ARG2 [ARG...]']
relatedFunctions:
- compare.Eq
- compare.Ge
- compare.Gt
- compare.Le
- compare.Lt
- compare.Ne
aliases: [/functions/ge]
---
```go-html-template
{{ ge 1 1 }} → true
{{ ge 1 2 }} → false
{{ ge 2 1 }} → true
{{ ge 1 1 1 }} → true
{{ ge 1 1 2 }} → false
{{ ge 1 2 1 }} → false
{{ ge 1 2 2 }} → false
{{ ge 2 1 1 }} → true
{{ ge 2 1 2 }} → true
{{ ge 2 2 1 }} → true
```

View File

@@ -0,0 +1,37 @@
---
title: compare.Gt
linkTitle: gt
description: Returns the boolean truth of arg1 > arg2 && arg1 > arg3.
categories: [functions]
keywords: []
menu:
docs:
parent: functions
function:
aliases: [gt]
returnType: bool
signatures: ['compare.Gt ARG1 ARG2 [ARG...]']
relatedFunctions:
- compare.Eq
- compare.Ge
- compare.Gt
- compare.Le
- compare.Lt
- compare.Ne
aliases: [/functions/gt]
---
```go-html-template
{{ gt 1 1 }} → false
{{ gt 1 2 }} → false
{{ gt 2 1 }} → true
{{ gt 1 1 1 }} → false
{{ gt 1 1 2 }} → false
{{ gt 1 2 1 }} → false
{{ gt 1 2 2 }} → false
{{ gt 2 1 1 }} → true
{{ gt 2 1 2 }} → false
{{ gt 2 2 1 }} → false
```

View File

@@ -0,0 +1,37 @@
---
title: compare.Le
linkTitle: le
description: Returns the boolean truth of arg1 <= arg2 && arg1 <= arg3.
categories: [functions]
keywords: []
menu:
docs:
parent: functions
function:
aliases: [le]
returnType: bool
signatures: ['compare.Le ARG1 ARG2 [ARG...]']
relatedFunctions:
- compare.Eq
- compare.Ge
- compare.Gt
- compare.Le
- compare.Lt
- compare.Ne
aliases: [/functions/le]
---
```go-html-template
{{ le 1 1 }} → true
{{ le 1 2 }} → true
{{ le 2 1 }} → false
{{ le 1 1 1 }} → true
{{ le 1 1 2 }} → true
{{ le 1 2 1 }} → true
{{ le 1 2 2 }} → true
{{ le 2 1 1 }} → false
{{ le 2 1 2 }} → false
{{ le 2 2 1 }} → false
```

View File

@@ -0,0 +1,37 @@
---
title: compare.Lt
linkTitle: lt
description: Returns the boolean truth of arg1 < arg2 && arg1 < arg3.
categories: [functions]
keywords: []
menu:
docs:
parent: functions
function:
aliases: [lt]
returnType: bool
signatures: ['compare.Lt ARG1 ARG2 [ARG...]']
relatedFunctions:
- compare.Eq
- compare.Ge
- compare.Gt
- compare.Le
- compare.Lt
- compare.Ne
aliases: [/functions/lt]
---
```go-html-template
{{ lt 1 1 }} → false
{{ lt 1 2 }} → true
{{ lt 2 1 }} → false
{{ lt 1 1 1 }} → false
{{ lt 1 1 2 }} → false
{{ lt 1 2 1 }} → false
{{ lt 1 2 2 }} → true
{{ lt 2 1 1 }} → false
{{ lt 2 1 2 }} → false
{{ lt 2 2 1 }} → false
```

View File

@@ -0,0 +1,32 @@
---
title: compare.Ne
linkTitle: ne
description: Returns the boolean truth of arg1 != arg2 && arg1 != arg3.
categories: [functions]
keywords: []
menu:
docs:
parent: functions
function:
aliases: [ne]
returnType: bool
signatures: ['compare.Ne ARG1 ARG2 [ARG...]']
relatedFunctions:
- compare.Eq
- compare.Ge
- compare.Gt
- compare.Le
- compare.Lt
- compare.Ne
aliases: [/functions/ne]
---
```go-html-template
{{ ne 1 1 }} → false
{{ ne 1 2 }} → true
{{ ne 1 1 1 }} → false
{{ ne 1 1 2 }} → false
{{ ne 1 2 1 }} → false
{{ ne 1 2 2 }} → true
```