Noah
dd1e6c2918
* Initial codebase (lot of work!) * Uses vanilla Go net/http and implements by hand: session cookies backed by Redis; log in/out; CSRF protection; email verification flow; initial database models (User table)
49 lines
1.2 KiB
Go
49 lines
1.2 KiB
Go
package router
|
|
|
|
import (
|
|
"html/template"
|
|
"io"
|
|
|
|
"git.kirsle.net/apps/gosocial/pkg/config"
|
|
)
|
|
|
|
// LoadTemplate processes and returns a template. Filename is relative
|
|
// to the template directory, e.g. "index.html"
|
|
func LoadTemplate(filename string) *template.Template {
|
|
files := templates(config.TemplatePath + "/" + filename)
|
|
tmpl := template.Must(template.New("page").ParseFiles(files...))
|
|
return tmpl
|
|
}
|
|
|
|
// Default template funcs.
|
|
var defaultFuncs = template.FuncMap{}
|
|
|
|
// Base template layout.
|
|
var baseTemplates = []string{
|
|
config.TemplatePath + "/base.html",
|
|
}
|
|
|
|
// templates returns a template chain with the base templates preceding yours.
|
|
// Files given are expected to be full paths (config.TemplatePath + file)
|
|
func templates(files ...string) []string {
|
|
return append(baseTemplates, files...)
|
|
}
|
|
|
|
// RenderTemplate executes a template. Filename is relative to the templates
|
|
// root, e.g. "index.html"
|
|
func RenderTemplate(w io.Writer, filename string) error {
|
|
files := templates(config.TemplatePath + "/" + filename)
|
|
tmpl := template.Must(
|
|
template.New("index").ParseFiles(files...),
|
|
)
|
|
|
|
err := tmpl.ExecuteTemplate(w, "base", map[string]interface{}{
|
|
"Title": config.Title,
|
|
})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|