Last active
May 16, 2020 23:55
-
-
Save sanderhahn/cdf5fbe8ebb153c968f08cca8a8d2e9d to your computer and use it in GitHub Desktop.
Golang Template Trouble
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<head> | |
<title>{{ template "title" . }}</title> | |
</head> | |
<body> | |
{{ template "content" . }} | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{{ define "title" }}Page 1{{ end }} | |
{{ define "content" }} | |
<h1>Page 1</h1> | |
{{ end }} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{{ define "title" }}Page 2{{ end }} | |
{{ define "content" }} | |
<h1>Page 2</h1> | |
{{ end }} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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