compare.Conditional
语法
compare.Conditional CONTROL ARG1 ARG2
返回值
any
别名
cond
如果 CONTROL 为真,则该函数返回 ARG1,否则返回 ARG2。
{{ $qty := 42 }}
{{ cond (le $qty 3) "few" "many" }} → many
与其它语言中的三元条件运算符不同,compare.Conditional
函数不执行短路求值。它会计算 ARG1 和 ARG2,而与 CONTROL 值无关。
由于缺少短路求值,以下示例会抛出错误
{{ cond true "true" (div 1 0) }}
{{ cond false (div 1 0) "false" }}