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-src/content/functions/test.yml | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) (limited to 'docs-src') diff --git a/docs-src/content/functions/test.yml b/docs-src/content/functions/test.yml index 9f638c88..a2763d12 100644 --- a/docs-src/content/functions/test.yml +++ b/docs-src/content/functions/test.yml @@ -76,3 +76,32 @@ funcs: template: :1:25: executing "" at : error calling required: The `config` datasource must have a value defined for `empty` $ 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" + - name: test.Ternary + alias: ternary + description: | + 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! + pipeline: true + arguments: + - name: truevalue + required: true + description: the value to return if `condition` is true + - name: falsevalue + required: true + description: the value to return if `condition` is false + - name: condition + required: true + description: the value to evaluate for truthiness + examples: + - | + $ gomplate -i '{{ ternary "FOO" "BAR" false }}' + BAR + $ gomplate -i '{{ ternary "FOO" "BAR" "yes" }}' + FOO -- cgit v1.2.3