From 45b527a6237e758faa022b9e5f9b02a806ea9b2e Mon Sep 17 00:00:00 2001 From: Dave Henderson Date: Sat, 17 Nov 2018 12:22:15 -0500 Subject: New test.Ternary function Signed-off-by: Dave Henderson --- docs/content/functions/test.md | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) (limited to 'docs') diff --git a/docs/content/functions/test.md b/docs/content/functions/test.md index c6d24008..dfb0b0ae 100644 --- a/docs/content/functions/test.md +++ b/docs/content/functions/test.md @@ -122,3 +122,43 @@ template: :1:25: executing "" at : error call $ gomplate -d config=config.yaml -i '{{ (ds "config").bogus | required "The `config` datasource must have a value defined for `bogus`" }}' template: :1:7: executing "" at <"config">: map has no entry for key "bogus" ``` + +## `test.Ternary` + +**Alias:** `ternary` + +Returns one of two values depending on whether the third is true. Note that the third value does not have to be a boolean - it is converted first by the [`conv.ToBool`](../conv/#conv-tobool) function (values like `true`, `1`, `"true"`, `"Yes"`, etc... are considered true). + +This is effectively a short-form of the following template: + +``` +{{ if conv.ToBool $condition }}{{ $truevalue }}{{ else }}{{ $falsevalue }}{{ end }} +``` + +Keep in mind that using an explicit `if`/`else` block is often easier to understand than ternary expressions! + +### Usage +```go +test.Ternary truevalue falsevalue condition +``` + +```go +condition | test.Ternary truevalue falsevalue +``` + +### Arguments + +| name | description | +|------|-------------| +| `truevalue` | _(required)_ the value to return if `condition` is true | +| `falsevalue` | _(required)_ the value to return if `condition` is false | +| `condition` | _(required)_ the value to evaluate for truthiness | + +### Examples + +```console +$ gomplate -i '{{ ternary "FOO" "BAR" false }}' +BAR +$ gomplate -i '{{ ternary "FOO" "BAR" "yes" }}' +FOO +``` -- cgit v1.2.3