Global template data

When I execute the ExecuteTemplate I see all the examples using &whateversruct{Title: "title info", Body: "body info"} to send data to the template to replace the information. I was wondering if it is not possible to create a structure outside my handler function, because for each handler function I will not have the same Title, Body. It would be nice to send him a card that replaces the template information. Any thoughts or ideas?

Currently - fluently written

 type Info struct { Title string Body string } func View(w http.ResponseWriter) { temp.ExecuteTemplate(w, temp.Name(), &Info{Title: "title", Body: "body"}) } 

It just seems that creating a structure is not necessary. And the structure will not be the same for every function you create. Therefore, you will need to create a structure for each function (of which I know).

+4
source share
4 answers

To increase Kevin's answer: an anonymous structure will give a lot of the same behavior:

 func View(w http.ResponseWriter) { data := struct { Title string Body string } { "About page", "Body info", } temp.ExecuteTemplate(w, temp.Name(), &data) } 
+14
source

This structure is just an example. You can also pass the structure from the outside, or you could use the map as you intended. The structure is good because the type of structure can document which fields the template expects, but this is not required.

All of them should work:

 func View(w http.ResponseWriter, info Info) { temp.ExecuteTemplate(w, temp.Name(), &info) } 

 func View(w http.ResponseWriter, info *Info) { temp.ExecuteTemplate(w, temp.Name(), info) } 

 func View(w http.ResponseWriter, info map[string]interface{}) { temp.ExecuteTemplate(w, temp.Name(), info) } 
+10
source

An important difference between applying a template to a structure in relation to a map: using a map, you can create links in your template that do not exist on your map; the template will run without errors and the links will be empty. If you process a structure and refer to something that does not exist in your structure, template execution returns an error.

A link to elements that do not exist on your map may be useful. Consider menu.html and the getPage () function in the view.go view in this webapp example: https://bitbucket.org/jzs/sketchground/src . Using a map for the menu, the active menu item stands out easily.

A simple illustration of this difference:

 package main import ( "fmt" "html/template" "os" ) type Inventory struct { Material string Count uint } func main() { // prep the template tmpl, err := template.New("test").Parse("{{.Count}} items are made of {{.Material}} - {{.Foo}}\n") if err != nil { panic(err) } // map first sweaterMap := map[string]string{"Count": "17", "Material": "wool"} err = tmpl.Execute(os.Stdout, sweaterMap) if err != nil { fmt.Println("Error!") fmt.Println(err) } // struct second sweaters := Inventory{"wool", 17} err = tmpl.Execute(os.Stdout, sweaters) if err != nil { fmt.Println("Error!") fmt.Println(err) } } 
+6
source

You are absolutely right Kevin !!! I like it. Thanks!!!

 func View(w http.ResponseWriter) { info := make(map[string]string) info["Title"] = "About Page" info["Body"] = "Body Info" temp.ExecuteTemplate(w, temp.Name(), info) } 
+1
source

Source: https://habr.com/ru/post/1434940/


All Articles