Adam
Adam

Reputation: 3138

Golang template order

Is there a way to make the template order irrelevant.

Here's my code:

var overallTemplates = []string{
    "templates/analytics.html",
    "templates/header.html",
    "templates/footer.html"}

func HomeHandler(w http.ResponseWriter, r *http.Request) {
    render(w,
        append([]string{"templates/home.html"}, overallTemplates...),
        nil)
}

func render(w http.ResponseWriter, files []string, data interface{}) {
    tmpl := template.Must(template.ParseFiles(files...))
    err := tmpl.Execute(w, data)

    if err != nil {
        fmt.Printf("Couldn't load template: %v\n", err)
    }
}

It works but if I change the order of overallTemplates to:

var overallTemplates = []string{
    "templates/header.html",
    "templates/footer.html",
    "templates/analytics.html"}

I get a blank page because analytics.html content is something like {{define "analytics"}}...{{end}} and it's called by footer.html like {{define "footer"}}{{template "analytics"}} ...{{end}}

Upvotes: 0

Views: 1182

Answers (1)

icza
icza

Reputation: 418377

template.ParseFiles() documents that:

The returned template's name will have the (base) name and (parsed) contents of the first file.

So in your first example your template designates "templates/analytics.html" because that's the first template you pass, and when you change order, the template will designate "templates/header.html".

And if you execute the template with Template.Execute(), these are the (default) templates to be executed.

Instead you should use Template.ExecuteTemplate() and explicitly specify you want to execute the "templates/analytics.html", whose name will be analytics, so pass that:

err := tmpl.ExecuteTemplate(w, "analytics", data)

That way it will not matter in what order you pass the templates to template.ParseFiles().

And a friendly note: do not parse your templates in the handler: it's slow. Parse them once, on app startup, store them e.g. in package variables, and just execute them in the handler. For details, see It takes too much time when using "template" package to generate a dynamic web page to client in Golang.

Also see related quesetion: Go template name

Upvotes: 6

Related Questions