Hi, I know it’s not much and the same could be obtained with a partial, but probably not as efficiently. Could we get a default function that doesn’t require nesting, but simply take on the first value neither null or empty ?
looks like you want or:
{{- or ($.Get "config") ($.Page.Params.config) "normal" -}}
Indeed, but for default.
just for the sake of the name ?
or are there any functional differences I don’t get?
huh, or only checks the truth value of multiple variable, and checks them all. default does that plus in a short-circuit fashion from right to left and then equals the first non-false value.
From the Go template documentation on or
Evaluation proceeds through the arguments left to right
and returns when the result is determined.
I’d read read that as “as soon as an argument is true, or returns that argument”. Looks like short-circuiting to me.
Yes, or does short circuit. and in fact I would not consider a default chain as short circuit.
Even if the deepest default gets a value it will still test that value with the next default statement up the tree.
The main difference is:
- or stops at truthy values → so false will continue the checks
- default tests for isset → a false will stop the checks
so I now understand the requirement of taking the first “defined/set” value and if none is set use the default …
Sorry I confused the docu on cond, which states :
Unlike ternary operators in other languages, the
compare.Conditionalfunction does not perform short-circuit evaluation. It evaluates both ARG1 and ARG2 regardless of the CONTROL value.
Due to the absence of short-circuit evaluation, these examples throw an error:{{ cond true "true" (div 1 0) }} {{ cond false (div 1 0) "false" }}
That said, the page for or does not claim they do it, so I’ll assume it is not “or else”. Someone corrects me if I’m wrong… and amend the docu.
they do: otherwise the first line would error out, too
{{ warnf "or true (div 1 0) : %v" (or true (div 1 0)) }}
{{ warnf "or false (div 1 0) : %v" (or false (div 1 0)) }}
WARN or true (div 1 0) : true
Total in 22 ms
ERROR error building site: render: failed to render pages: execute of template failed: template: home.html:2:62: executing "home.html" at <div 1 0>: error calling div: can't divide the value by 0
maybe implicit, cause it’s mentioned explicit that it doesn’t on the cond page, but I agree one should either have that statement for general usage or mentioned for each function…
This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.