Skip to content

Instantly share code, notes, and snippets.

@sanderhahn
Last active May 16, 2020 23:55
Show Gist options
  • Save sanderhahn/cdf5fbe8ebb153c968f08cca8a8d2e9d to your computer and use it in GitHub Desktop.
Save sanderhahn/cdf5fbe8ebb153c968f08cca8a8d2e9d to your computer and use it in GitHub Desktop.
Golang Template Trouble
<html>
<head>
<title>{{ template "title" . }}</title>
</head>
<body>
{{ template "content" . }}
</body>
</html>
{{ define "title" }}Page 1{{ end }}
{{ define "content" }}
<h1>Page 1</h1>
{{ end }}
{{ define "title" }}Page 2{{ end }}
{{ define "content" }}
<h1>Page 2</h1>
{{ end }}
package main
import (
"html/template"
"log"
"os"
)
func main() {
templates, err := template.ParseGlob("./views/**")
if err != nil {
log.Fatal(err)
}
// template uses page 2 content instead of 1
// https://github.com/golang/go/issues/22392
err = templates.ExecuteTemplate(os.Stdout, "page1.html", nil)
if err != nil {
log.Fatal(err)
}
err = templates.ExecuteTemplate(os.Stdout, "layout.html", nil)
if err != nil {
log.Fatal(err)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment