This repository has been archived on 2022-08-26. You can view files and clone it, but cannot push or open issues or pull requests.
gosocial/pkg/router/template.go
Noah Petherbridge 2a8a1df6ab Initial commit
* 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)
2022-08-09 22:32:19 -07:00

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
}