package main import ( "fmt" "html/template" "path/filepath" "regexp" "net/http" ) var ( templates = map[string]*template.Template{} ) // Tempalte helper functions var funcMap = template.FuncMap { "add": func (x, y int) int { return x + y }, "minus": func (x, y int) int { return x - y }, } func initTemplates() { files, _ := filepath.Glob("templates/pages/*.html") re := regexp.MustCompile("templates/pages/(.*).html") fmt.Println("Loading Templates:") for _, t := range files { name := re.FindStringSubmatch(t) fmt.Println(" ", name[1]) templates[name[1]] = template.Must(template.New(name[1]).Funcs(funcMap).ParseFiles("templates/layout.html", t)) } } func ShowTemplate(template string, w http.ResponseWriter, data map[string]interface{}) { err := templates[template].ExecuteTemplate(w, "layout.html", data) if err != nil { fmt.Println("Exec err: ", err) } // TODO: show error 500 page }