I have a problem with managing spaces and formatting html/template in readable mode. My templates look something like this:
layout.tmpl
{{define "layout"}} <!DOCTYPE html> <html> <head> <title>{{.title}}</title> </head> <body> {{ template "body" . }} </body> </html> {{end}}
body.tmpl
{{define "body"}} {{ range .items }} {{.count}} items are made of {{.material}} {{end}} {{end}}
code
package main import ( "os" "text/template" ) type View struct { layout string body string } type Smap map[string]string func (self View) Render(data map[string]interface{}) { layout := self.layout + ".tmpl" body := self.body + ".tmpl" tmpl := template.Must(template.New("layout").ParseFiles(layout, body)) tmpl.ExecuteTemplate(os.Stdout, "layout", data) } func main() { view := View{ "layout", "body" } view.Render(map[string]interface{}{ "title": "stock", "items": []Smap{ Smap{ "count": "2", "material": "angora", }, Smap{ "count": "3", "material": "wool", }, }, }) }
But this produces (note: there is a line above doctype):
<!DOCTYPE html> <html> <head> <title>stock</title> </head> <body> 2 items are made of angora 3 items are made of wool </body> </html>
I want to:
<!DOCTYPE html> <html> <head> <title>stock</title> </head> <body> 2 items are made of angora 3 items are made of wool </body> </html>
In other template languages, I can say things like
[[- value -]]
and spaces before and after the action are removed, but I don't see anything like it in html/template . Does this really mean that I have to make my templates unreadable, as shown below?
layout.tmpl
{{define "layout"}}<!DOCTYPE html> <html> <head> <title>.title</title> </head> <body> {{ template "body" . }} </body> </html> {{end}}
body.tmpl
{{define "body"}}{{ range .items }}{{.count}} items are made of {{.material}} {{end}}{{end}}