How can I control whitespace after an action in html/template?

Chas. Owens picture Chas. Owens · Jul 6, 2013 · Viewed 9k times · Source

I am having a problem controlling whitespace and still formatting html/template templates in a readable fashion. My templates look somthing 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 that produces (note: there is a line above 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>

What I want is:

<!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 the whitespace before and after the action are stripped, but I don't see anything like that in html/template. Does this really mean I have to make my templates unreadable like the following?

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}}

Answer

hkulekci picture hkulekci · Feb 4, 2016

You can use white space controller

{{range .foos -}} // eats trailing whitespace
    <tr><td>do something</td></tr>
{{- end}} // eats leading whitespace (\n from previous line)