I would like to know if there is any way I could use the output from calling a reusable template inside an if
conditional, e.g. something along the lines of this:
{{ define "test.tmpl" }}SomeExpectedValue{{ end }}
Header
{{/* here \/ */}}
{{- if eq (template "test.tmpl") "SomeExpectedValue" }}
The expected body: {{ template "test.tmpl" }}.
{{- else }}
Something else.
{{- end }}
Footer
Since a reusable template can be used easily throughout the body of a template via the template
action, I was hoping I could also use it inside an if
condition. So far, looking at the go template docs, I couldn't figure out a way to achieve this.
The output of a template cannot be used as a value within a template. What you are looking for can be done using additional template functions that you pass to template.Funcs
before evaluating the template. So, instead of writing a test.tmpl
, write a Go function that returns a value, and call that function from your template.