Created
January 28, 2016 14:39
-
-
Save zemirco/d901356dde9a6cd71110 to your computer and use it in GitHub Desktop.
go nested html templates
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" }} | |
a | |
{{ end }} | |
{{ define "body" }} | |
<p> | |
some content in file a | |
</p> | |
{{ 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" }} | |
b | |
{{ end }} | |
{{ define "body" }} | |
<p> | |
some content in file b | |
</p> | |
{{ 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
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
<meta name="viewport" content="width=device-width, initial-scale=1"> | |
<title>{{ template "title" . }}</title> | |
</head> | |
<body> | |
<div class="container"> | |
{{template "body" .}} | |
</div> | |
{{ template "footer" . }} | |
</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
var ( | |
aTmpl = parseTemplate("a.html") | |
bTmpl = parseTemplate("b.html") | |
) | |
func main() { | |
// some http router | |
} | |
func GetIndex(w http.ResponseWriter, r *http.Request) *appError { | |
return listTmpl.Execute(w, r, nil) | |
} |
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
func parseTemplate(filename string) *appTemplate { | |
// HERE! somehow use the template specified by filename | |
tmpl := template.Must(template.ParseGlob("*.html")) | |
return &appTemplate{tmpl.Lookup("base.html")} | |
} | |
type appTemplate struct { | |
t *template.Template | |
} | |
func (tmpl *appTemplate) Execute(w http.ResponseWriter, r *http.Request, data interface{}) *appError { | |
d := struct { | |
Data interface{} | |
}{ | |
Data: data, | |
} | |
if err := tmpl.t.Execute(w, d); err != nil { | |
return appErrorf(err, "could not write template: %v") | |
} | |
return nil | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
should be able to set it up like this...
Also, with your go templates...need to make sure to always define the names even for partial templates. In
a.html
andb.html
you would need to set them up like..something like that anyways...good luck! 😄