Skip to content

Instantly share code, notes, and snippets.

Created November 28, 2013 13:33
Show Gist options
  • Save anonymous/7691910 to your computer and use it in GitHub Desktop.
Save anonymous/7691910 to your computer and use it in GitHub Desktop.
<p>Hello</p>
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<div id="content">
{{template "content" .}}
</div>
</body>
</html>
package main
import (
"log"
"net/http"
"text/template"
)
var (
layout = template.Must(template.ParseFiles("layout.html"))
index = parsePartial(layout, "index.html")
)
// parsePartial parses a partial template file and adds it as a template named 'content'
// to a clone of the layout template.
func parseTemplate(layout *template.Template, name string) *template.Template {
t := template.Must(template.ParseFiles(name))
l := template.Must(layout.Clone())
return template.Must(l.AddParseTree("content", t.Tree))
}
// MainHandler is an example handler
func MainHandler(w http.ResponseWriter, r *http.Request) {
if err := index.Execute(w, nil); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
func main() {
http.HandleFunc("/", MainHandler)
log.Fatal(http.ListenAndServe(":8080", nil))
}
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<div id="content">
<p>Hello</p>
</div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment