type IndexViewModel struct {
// PopulateTemplates func
// Create map template name to template.Template
func PopulateTemplates() map[string]*template.Template {
const basePath = "templates"
result := make(map[string]*template.Template)
layout := template.Must(template.ParseFiles(basePath + "/_base.html"))
dir, err := os.Open(basePath + "/content")
panic("Failed to open template blocks directory: " + err.Error())
fis, err := dir.Readdir(-1)
panic("Failed to read contents of content directory: " + err.Error())
f, err := os.Open(basePath + "/content/" + fi.Name())
panic("Failed to open template '" + fi.Name() + "'")
content, err := ioutil.ReadAll(f)
panic("Failed to read content from file '" + fi.Name() + "'")
tmpl := template.Must(layout.Clone())
_, err = tmpl.Parse(string(content))
panic("Failed to parse contents of '" + fi.Name() + "' as template")
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
u1 := User{Username: "bonfy"}
u2 := User{Username: "rene"}
Post{User: u1, Body: "Beautiful day in Portland!"},
Post{User: u2, Body: "The Avengers movie was so cool!"},
v := IndexViewModel{Title: "Homepage", User: u1, Posts: posts}
templates := PopulateTemplates()
templates["index.html"].Execute(w, &v)
http.ListenAndServe(":8888", nil)