Typical construct:
{{ $doThis := true }}
{{ if $doThis }}
{{ warnf "I did it" }}
{{ else }}
{{ warnf "I didn't do it" }}
{{ end }}
But that’s a simple example. With multiple and/or nested conditions, adding another condition can make the code difficult to read or maintain. What we really, really want is:
{{ $doThis := true }}
{{ if $doThis }}
{{ warnf "I did it" }}
{{ exit }} <-- pseudo code; this template function does not exist
{{ end }}
{{ warnf "I didn't do it" }}
There’s a proposal to add this functionality to Go’s template packages, but the effort has died on the vine.
But you can get the desired behavior by wrapping the code in a loop that executes once, using the break
statement:
{{ $doThis := true }}
{{ range seq 1 }}
{{ if $doThis }}
{{ warnf "I did it" }}
{{ break }}
{{ end }}
{{ warnf "I didn't do it" }}
{{ end }}
It’s a bit clumsy, but in some situations the resulting code is more manageable.