allowances/template.go

35 lines
635 B
Go
Raw Normal View History

package main
import (
2013-03-05 22:54:29 -08:00
"fmt"
"html/template"
"path/filepath"
"sync"
)
var cachedTemplates = map[string]*template.Template{}
var cachedMutex sync.Mutex
2013-03-05 22:54:29 -08:00
func dollarize(value int) string {
return fmt.Sprintf("$%0.2f", float32(value)/100.0)
}
var funcs = template.FuncMap{
"dollarize": dollarize,
}
func T(name string) *template.Template {
cachedMutex.Lock()
defer cachedMutex.Unlock()
if t, ok := cachedTemplates[name]; ok {
return t
}
2013-03-05 22:54:29 -08:00
t := template.New("_base.html").Funcs(funcs)
t = template.Must(t.ParseFiles(
"templates/_base.html",
filepath.Join(*template_dir, name),
))
2013-02-21 00:19:38 -08:00
cachedTemplates[name] = t
return t
}