I have created a function to check if a variable is defined:
fm["isset"] = func(a interface{}) bool {
if a == nil || a == "" || a == 0 {
fmt.Println("is not set")
return false
}
fmt.Println("is set")
return false
}
tmpl := template.Must(template.New("").Funcs(fm).ParseFiles("templates/header.html"))
err := tmpl.ExecuteTemplate(w, "header", templateData)
In the template I have:
email is set
This function works if the variable is contained by the templateData (which is a custom struct that contains a map and a string), but it gives me an error if the variable doesn't exist.
The error is:
executing "header" at <.Email>: can't evaluate field Email in type base.customData
In my case "base.go" is the handler and "customData" is defined by: type customData struct{..}.
I want to be able to reuse templates and to display some sections only if some variables are sent from the handler. Any idea how can I implement a variable isset check on the template side?
I also tried using: do stuff but this also gives me the same error.
Any idea?
Aucun commentaire:
Enregistrer un commentaire