Mam problem z kontrolowaniem białych znaków i formatowaniem szablonów html/template
w czytelny sposób. Moje szablony wyglądać somthing tak:Jak mogę kontrolować białe znaki po akcji w html/template?
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}}
kod
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",
},
},
})
}
Ale to produkuje (uwaga: nie jest to linia powyżej the doctype):
<!DOCTYPE html>
<html>
<head>
<title>stock</title>
</head>
<body>
2 items are made of angora
3 items are made of wool
</body>
</html>
Co chcę jest:
<!DOCTYPE html>
<html>
<head>
<title>stock</title>
</head>
<body>
2 items are made of angora
3 items are made of wool
</body>
</html>
W innych językach szablonów mogę powiedzieć rzeczy, jak
[[- value -]]
i spacji przed i po działaniu są usuwane, ale nie widzę niczego tak jak w html/template
. Czy to naprawdę oznacza, że muszę uczynić moje szablony nieczytelnymi, tak jak poniższe?
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}}
Od idź '1.6' https://golang.org/doc/go1.6#template – webwurst
tak, dziękuję @webwurst przed 1.6, można sprawdzić https : //golang.org/pkg/text/template/#hdr-Text_and_spaces – hkulekci
Działa to tylko dla 'text/template', a nie' html/template'. –