Given the below template:
{{ range $item := . }}
{{ if $item.IsA}}
Ok
{{ else }}
Fine
{{ end }}
{{ end }}
Done!
When I render it using:
t := template.New("test").Parse(_types)
text, err := t.Execute(&buffer, strct)
The output is something like:
!empty line
!empty line
Ok
!empty line
!empty line
Done!
This means that if I want to format the text correctly, I have to re-write it as
{{ range $item := .}}{{ if $item.IsA }}OK{{ else }}{{ end }}{{ end }}
Done!
Then I get something like:
Ok
Done!
Which is the desired output.
Writing the template the second way is very unreadable and messy. Is there any way that we can write the template with proper indentation but somehow configure rendering in such a way that the template placeholders would not be converted to new lines, or their indentation would be ignored (so that the desired output would be generated)?
Edit: using {- ""}
even makes the whole thing worse! Why? Please consider the following:
{{- range $item := . }}
{{- if $item.IsA }}
{{- "How many spaces??" -}}OK
...
So let me put it in another way, is there any built-in post-processor available in golang for templates?