61 lines
1.0 KiB
Go
61 lines
1.0 KiB
Go
package allowances
|
|
|
|
import (
|
|
"fmt"
|
|
"html/template"
|
|
"log"
|
|
"strings"
|
|
)
|
|
|
|
func dollarize(value int) string {
|
|
return fmt.Sprintf("$%0.2f", float32(value)/100.0)
|
|
}
|
|
|
|
type tmap map[string]*template.Template
|
|
|
|
var tmpls tmap
|
|
|
|
func getTemplates() (tmap, error) {
|
|
var err error
|
|
funcMap := template.FuncMap{
|
|
"title": strings.Title,
|
|
"dollarize": dollarize,
|
|
}
|
|
base, err := Asset("templates/base.html")
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
tmpl, err := template.New("base").Funcs(funcMap).Parse(string(base))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
templates := make(map[string]*template.Template)
|
|
templateFiles := []struct {
|
|
name string
|
|
path string
|
|
}{
|
|
{"home", "templates/index.html"},
|
|
{"login", "templates/login.html"},
|
|
}
|
|
|
|
for _, tf := range templateFiles {
|
|
a, err := Asset(tf.path)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
t, err := tmpl.Clone()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
t, err = t.Parse(string(a))
|
|
if err != nil {
|
|
log.Printf("XXX: %+v", err)
|
|
return nil, err
|
|
}
|
|
templates[tf.name] = t
|
|
}
|
|
|
|
return templates, nil
|
|
}
|