Skip to content

lazy evaluation in ternary function? #1108

Answered by hairyhenderson
zswanson asked this question in Q&A
Discussion options

You must be logged in to vote

The short answer to this is "no, not with ternary" 😉

Ultimately the problem is that in Go templates, all arguments are evaluated. In Go you could do something like if cond1 || cond2, and cond2 would only be evaluated if cond1 first evaluated to false. But in the text/template language, all arguments to a function are evaluated first before calling the function.

So in the case of ternary, or even the built-in or function, all args must first be evaluated to booleans before the function itself is called.

The general workaround for this is to just use if/else blocks. In your case I think the following would work:

{{- $http_port := "" }}
{{- if eq $env_http_port "" }}
  {{- $http_port = (ds "…

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@zswanson
Comment options

Answer selected by zswanson
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants