Templates 02 - Conditionals
Conditional Templates
Another very common feature we need when working with templates is the use of conditions.
For all the conditional examples in this article, we will use this code snippet:
type User struct {
Name string
LastName string
Country string
Admin bool
YearsOfExperience int
}
func main() {
tmplFile := "conditional.tmpl"
users := []User{
{"John", "Doe", "USA", true, 25},
{"Gavin", "Steele", "USA", false, 3},
{"Ashton", "Walsh", "CA", false, 5},
}
tmpl, err := template.New(tmplFile).ParseFiles(tmplFile)
if err != nil {
panic(err)
}
err = tmpl.Execute(os.Stdout, users)
if err != nil {
panic(err)
}
}
For example, if we need to print Admin User or Regular User depending on the value of the Admin field in the User struct, we can create a template like this:
The template name will be conditional.tmpl, and this is the content of the template:
{{ range . -}}
----------
Name: {{ .Name }} LastName: {{ .LastName }} Country: {{ .Country }}
User Tye: {{ if .Admin }}Admin User{{ else }}Regular User{{ end }}
{{ end }}
----------
Name: John LastName: Doe Country: USA
User Type: Admin User
----------
Name: Gavin LastName: Steele Country: USA
User Type: Regular User
----------
Name: Ashton LastName: Walsh Country: CA
User Type: Regular User
The Admin field of the struct is a boolean, so we can check if it is true with the marker {{ if .Admin }}.
We should insert the {{ end }} marker after the {{ if }} marker. In this example, we are using the {{ else }} marker to print that it is a regular user, but we could have just an if without else:
{{ range . }}
----------
Name: {{ .Name }} LastName: {{ .LastName }} Country: {{ .Country }}
{{ if .Admin }}User Type: Admin User{{ end }}
{{ end }}
----------
Name: John LastName: Doe Country: USA
User Type: Admin User
----------
Name: Gavin LastName: Steele Country: USA
----------
Name: Ashton LastName: Walsh Country: CA
We can insert the minus sign - to remove whitespace and line breaks that we don’t want. This way, we can insert line breaks manually with “\n”.
{{ range . -}}
----------
Name: {{ .Name }} LastName: {{ .LastName }} Country: {{ .Country }}{{ "\n" }}
{{- if .Admin }}User Type: Admin User{{ "\n" }}{{ end -}}
{{ end -}}
----------
----------
Name: John LastName: Doe Country: USA
User Type: Admin User
----------
Name: Gavin LastName: Steele Country: USA
----------
Name: Ashton LastName: Walsh Country: CA
----------
Multiple conditions
When we need to compare multiple conditions, we can use the and function.
{{ range . -}}
----------
Name: {{ .Name }} LastName: {{ .LastName }} Country: {{ .Country }}{{ "\n" }}
{{- if (and (.Admin) (eq .Name "John")) }}John Admin{{ "\n" }}{{ end -}}
{{ end -}}
----------
----------
Name: John LastName: Doe Country: USA
John Admin
----------
Name: Gavin LastName: Steele Country: USA
----------
Name: Ashton LastName: Walsh Country: CA
----------
In the example above, we used if together with the and function, which will evaluate whether both provided arguments are null.
First, we check if .Admin is true, and then we use the eq function to compare if the .Name field is equal to the string John.
The functions we have for making comparisons are:
- eq equal
- ne not equal
- lt lower than
- le lower than or equal
- gt greater than
- ge greater than or equal
eq
Equal
{{ range . -}}
----------
Name: {{ .Name }} LastName: {{ .LastName }} Country: {{ .Country }}{{ "\n" }}
{{- if (and (.Admin) (eq .Name "John")) }}John Admin{{ "\n" }}{{ end -}}
{{ end -}}
----------
----------
Name: John LastName: Doe Country: USA
John Admin
----------
Name: Gavin LastName: Steele Country: USA
----------
Name: Ashton LastName: Walsh Country: CA
----------
ne
Not equal
{{ range . -}}
----------
Name: {{ .Name }} LastName: {{ .LastName }} Country: {{ .Country }}{{ "\n" }}
{{- if ne .Name "John" }}Not John{{ "\n" }}{{ end -}}
{{ end -}}
----------
----------
Name: John LastName: Doe Country: USA
----------
Name: Gavin LastName: Steele Country: USA
Not John
----------
Name: Ashton LastName: Walsh Country: CA
Not John
----------
lt
Lower than
{{ range . -}}
----------
Name: {{ .Name }} LastName: {{ .LastName }} Country: {{ .Country }}{{ "\n" }}
{{- if lt .YearsOfExperience 5 }}Less than 5 years of experience{{ "\n" }}{{ end -}}
{{ end -}}
----------
----------
Name: John LastName: Doe Country: USA
----------
Name: Gavin LastName: Steele Country: USA
Less than 5 years of experience
----------
Name: Ashton LastName: Walsh Country: CA
----------
le
Lower than or equal
{{ range . -}}
----------
Name: {{ .Name }} LastName: {{ .LastName }} Country: {{ .Country }}{{ "\n" }}
{{- if le .YearsOfExperience 5 }}Less than or equal to 5 years of experience{{ "\n" }}{{ end -}}
{{ end -}}
----------
----------
Name: John LastName: Doe Country: USA
----------
Name: Gavin LastName: Steele Country: USA
Less than or equal to 5 years of experience
----------
Name: Ashton LastName: Walsh Country: CA
Less than or equal to 5 years of experience
----------
gt
Greater than
{{ range . -}}
----------
Name: {{ .Name }} LastName: {{ .LastName }} Country: {{ .Country }}{{ "\n" }}
{{- if gt .YearsOfExperience 5 }}Greater than 5 years of experience{{ "\n" }}{{ end -}}
{{ end -}}
----------
----------
Name: John LastName: Doe Country: USA
Greater than 5 years of experience
----------
Name: Gavin LastName: Steele Country: USA
----------
Name: Ashton LastName: Walsh Country: CA
----------
ge
Greater than or equal
{{ range . -}}
----------
Name: {{ .Name }} LastName: {{ .LastName }} Country: {{ .Country }}{{ "\n" }}
{{- if ge .YearsOfExperience 5 }}Greater than or equal to 5 years of experience{{ "\n" }}{{ end -}}
{{ end -}}
----------
----------
Name: John LastName: Doe Country: USA
Greater than or equal to 5 years of experience
----------
Name: Gavin LastName: Steele Country: USA
----------
Name: Ashton LastName: Walsh Country: CA
Greater than or equal to 5 years of experience
----------