package main import ( "io/ioutil" "os" "path/filepath" "strings" "text/template" ) type templateData struct { SiteTitle string StylesheetUrl string Nav []navItem Content []string } func loadTemplate(cfg config) (templ *template.Template) { tmpl_raw, err := ioutil.ReadFile(cfg.TemplateFile) if err != nil { panic(err) } templ, err = template.New("template").Parse(string(tmpl_raw)) if err != nil { panic(err) } return } func applyTemplate(dir string, data templateData, tmpl *template.Template) error { // ensure directory exists os.MkdirAll(strings.ToLower(dir), 0755) file := filepath.Join(strings.ToLower(dir), "index.html") // create file, fail if it already exists f, err := os.Create(file) if err != nil { return err } defer f.Close() // apply template, write to file err = tmpl.Execute(f, data) if err != nil { return err } return nil }