forked from gohugoio/hugo
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
tpl: adds
truth
and bool
template functions
The behavior of `truth` and `bool` is described in the corresponding test cases and examples. The decision-making around the behavior is a based on combination of the existing behavior of strconv.ParseBool in go and the MDN definition of "truthy" as JavaScript has the most interop with the Hugo ecosystem. Addresses gohugoio#9160 and (indirectly) gohugoio#5792
- Loading branch information
1 parent
f650e4d
commit 5a72de2
Showing
5 changed files
with
230 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
--- | ||
title: bool | ||
linktitle: bool | ||
description: Creates a `bool` from the argument passed into the function. | ||
date: 2023-01-28 | ||
publishdate: 2023-01-28 | ||
lastmod: 2023-01-28 | ||
categories: [functions] | ||
menu: | ||
docs: | ||
parent: "functions" | ||
keywords: [strings,boolean,bool] | ||
signature: ["bool INPUT"] | ||
workson: [] | ||
hugoversion: | ||
relatedfuncs: [truth] | ||
deprecated: false | ||
aliases: [] | ||
--- | ||
|
||
Useful for turning ints, strings, and nil into booleans. | ||
|
||
``` | ||
{{ bool "true" }} → true | ||
{{ bool "false" }} → false | ||
{{ bool "TRUE" }} → true | ||
{{ bool "FALSE" }} → false | ||
{{ truth "t" }} → true | ||
{{ truth "f" }} → false | ||
{{ truth "T" }} → true | ||
{{ truth "F" }} → false | ||
{{ bool "1" }} → true | ||
{{ bool "0" }} → false | ||
{{ bool 1 }} → true | ||
{{ bool 0 }} → false | ||
{{ bool true }} → true | ||
{{ bool false }} → false | ||
{{ bool nil }} → false | ||
``` | ||
|
||
This function will throw a type-casting error for most other types or strings. For less strict behavior, see [`truth`](/functions/truth). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
--- | ||
title: truth | ||
linktitle: truth | ||
description: Creates a `bool` from the truthyness of the argument passed into the function | ||
date: 2023-01-28 | ||
publishdate: 2023-01-28 | ||
lastmod: 2023-01-28 | ||
categories: [functions] | ||
menu: | ||
docs: | ||
parent: "functions" | ||
keywords: [strings,boolean,bool,truthy,falsey] | ||
signature: ["truth INPUT"] | ||
workson: [] | ||
hugoversion: | ||
relatedfuncs: [bool] | ||
deprecated: false | ||
aliases: [] | ||
--- | ||
|
||
Useful for turning different types into booleans based on their [truthy-ness](https://developer.mozilla.org/en-US/docs/Glossary/Truthy). | ||
|
||
It follows the same rules as [`bool`](/functions/bool), but with increased flexibility. | ||
|
||
``` | ||
{{ truth "true" }} → true | ||
{{ truth "false" }} → false | ||
{{ truth "TRUE" }} → true | ||
{{ truth "FALSE" }} → false | ||
{{ truth "t" }} → true | ||
{{ truth "f" }} → false | ||
{{ truth "T" }} → true | ||
{{ truth "F" }} → false | ||
{{ truth "1" }} → true | ||
{{ truth "0" }} → false | ||
{{ truth 1 }} → true | ||
{{ truth 0 }} → false | ||
{{ truth true }} → true | ||
{{ truth false }} → false | ||
{{ truth nil }} → false | ||
{{ truth "cheese" }} → true | ||
{{ truth 1.67 }} → true | ||
``` | ||
|
||
This function will not throw an error. For more strict behavior, see [`bool`](/functions/bool). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters