html - Cant execute template well in golang -
i'm trying create templates , can't understand next thing: why doesn't such construction work? i've got test.go file:
package main import ( "net/http" "html/template" "fmt" ) func main() { http.handlefunc("/test.html", testhandler) http.listenandserve(":8080", nil) } func testhandler(w http.responsewriter, r *http.request) { //parsing html t, err := template.parsefiles("test.html") if err != nil { fmt.println(err) } name := "myname" city := "mycity" t.executetemplate(w, "t1", name) t.executetemplate(w, "t2", city) } and have test.html:
<html> <head> <title>tamplates</title> </head> <body> <h1>testing templates</h1> <p> empty template: {{ `"some text"` }} </p> <p> name {{ template "t1" . }} </p> <p> city {{ template "t2" . }} </p> </body> </html> it's easy example, somewhy doesnt work. tried adding strings {{ define "t1" }} {{ . }} {{ end }} , {{ define "t2" }} {{ . }} {{ end }}before h1 header, has resulted web page containing string myname mycity
update (07/02/2016)
i'm gonna try explain best can why code not been executed , proper solution using nested templates.
in test.html file importing 2 templates: t1 , t2,
<p> name {{ template "t1" . }} </p> <p> city {{ template "t2" . }} </p> those templates should defined , write in somewhere used (a place written in separate files). here first issue found, how defined them:
{{ define "t1" }} {{ . }} {{ end }} {{ define "t2" }} {{ . }} {{ end }} note {{.}} shorthand current object, render whatever data pass when calling executetemplate function. so, solve issue, should specify object want render in each template:
{{ define "t1" }} {{ .name }} {{ end }} {{ define "t2" }} {{ .city }} {{ end }} now, here second issue found. in testhandler function, rendering sub-templates, firstly t1 template, , secondly t2 template:
t.executetemplate(w, "t1", name) t.executetemplate(w, "t2", city) so @ no time, calling parent template.
below can find how using nested templates. hope solve doubt how use templates.
test.go file
package main import ( "fmt" "html/template" "net/http" ) func main() { http.handlefunc("/test.html", testhandler) http.listenandserve(":8080", nil) } func testhandler(w http.responsewriter, r *http.request) { //parsing html t, err := template.parsefiles("test2.html", "t1.tmpl", "t2.tmpl") if err != nil { fmt.println(err) } items := struct { name string city string }{ name: "myname", city: "mycity", } t.execute(w, items) } test.html file
<html> <head> <title>templates</title> </head> <body> <h1>testing templates</h1> <p> empty template: {{ `"some text"` }} </p> <p> name {{ template "t1" . }} </p> <p> city {{ template "t2" . }} </p> </body> </html> t1.tmpl file
{{ define "t1" }} {{ .name }} {{ end }} t2.tmpl file
{{ define "t2" }} {{ .city }} {{ end }} without using sub-templates:
i did changes in code made work. send 'name' , 'city' variable in struct collection execute method. check below:
package main import ( "fmt" "html/template" "net/http" ) func main() { http.handlefunc("/test.html", testhandler) http.listenandserve(":8080", nil) } func testhandler(w http.responsewriter, r *http.request) { //parsing html t, err := template.parsefiles("test.html") if err != nil { fmt.println(err) } items := struct { name string city string }{ name: "myname", city: "mycity", } t.execute(w, items) } and using {{.name}} , {{.city}} access exported fields.
<html> <head> <title>tamplates</title> </head> <body> <h1>testing templates</h1> <p> name {{.name}} </p> <p> city {{.city}} </p> </body> </html>
Comments
Post a Comment