Conditional

2 min read

Choose between values inside an expression: defaults for empty values, the first non-empty of several, and ternaries, so a template does not need an if block for a one-line choice.

Reference

FunctionDescription
boolDefaultReturns the field’s boolean value if it is a bool, otherwise returns def.
boolTernaryIs like ternary but requires a strict bool argument.
coalesceReturns the first non-empty value from a variadic list.
defaultReturns val if non-empty, otherwise returns def.
emptyReturns true when the value is empty (nil, "", 0, false, empty slice, empty map, or "<no value>").
eqTernaryReturns trueVal when val equals target (string comparison), falseVal otherwise.
notEmptyReturns true when the value is non-empty.
ternaryReturns trueVal when condition is truthy, falseVal otherwise.

Functions

boolDefault

boolDefault returns the field’s boolean value if it is a bool, otherwise returns def. Useful when a boolean field may be absent (nil).

{{ boolDefault .spec.suspend false }}  → false

boolTernary

boolTernary is like ternary but requires a strict bool argument. Use this when the field is declared as a boolean in the CRD schema — it avoids the truthiness ambiguity of ternary for values like 0, "", or [].

{{ boolTernary .spec.debug "debug" "info" }}  → "debug"

coalesce

coalesce returns the first non-empty value from a variadic list. Useful when a field can come from multiple sources with a final fallback.

{{ coalesce .spec.sidecarImage .spec.defaultSidecarImage "envoyproxy/envoy:v1.30" }}  → "envoyproxy/envoy:v1.30"

default

default returns val if non-empty, otherwise returns def. “Empty” means nil, "", 0, false, empty slice, or empty map.

{{ default "info" .spec.logLevel }}  → "info"
{{ .spec.logLevel | default "info" }}  → "info"

empty

empty returns true when the value is empty (nil, "", 0, false, empty slice, empty map, or "<no value>").

{{ empty .spec.logLevel }}  → true

eqTernary

eqTernary returns trueVal when val equals target (string comparison), falseVal otherwise. Shorthand for the boolTernary (eq val target) trueVal falseVal pattern — useful when branching on a string field value such as a status string, a mode flag, or a cross-CRD found result.

{{ eqTernary .spec.mode "production" "strict" "permissive" }}  → "strict"

notEmpty

notEmpty returns true when the value is non-empty. The inverse of empty.

{{ notEmpty .spec.mode }}  → true

ternary

ternary returns trueVal when condition is truthy, falseVal otherwise. Condition truthiness follows Go rules: non-empty string, non-zero number, non-empty slice/map, true bool.

{{ ternary .spec.debug "debug" "info" }}  → "debug"