summaryrefslogtreecommitdiff
path: root/src/util/template.go
diff options
context:
space:
mode:
Diffstat (limited to 'src/util/template.go')
-rw-r--r--src/util/template.go56
1 files changed, 56 insertions, 0 deletions
diff --git a/src/util/template.go b/src/util/template.go
new file mode 100644
index 0000000..f42875a
--- /dev/null
+++ b/src/util/template.go
@@ -0,0 +1,56 @@
+package util
+
+import (
+ "path"
+ "text/template"
+ "time"
+)
+
+func NewTemplate(filenames ...string) *template.Template {
+ return template.Must(template.New(path.Base(filenames[0])).
+ Funcs(template.FuncMap{
+ // Form input helpers
+ "value": func(v interface{}) string {
+ if v == nil {
+ return ""
+ }
+ return "value=\"" + template.HTMLEscapeString(v.(string)) + "\""
+ },
+ "checked": func(v1 interface{}, v2 interface{}) string {
+ if v1 == nil || v2 == nil {
+ return ""
+ }
+ if v1 == v2 {
+ return "checked"
+ }
+ return ""
+ },
+ "selected": func(v1 interface{}, v2 interface{}) string {
+ if v1 == nil || v2 == nil {
+ return ""
+ }
+ if v1 == v2 {
+ return "selected"
+ }
+ return ""
+ },
+ // Form result helpers
+ "q": func(v interface{}) string {
+ if v == nil || v.(string) == "" {
+ return "<output class=none>none</output>"
+ }
+ return "<output><q>" + template.HTMLEscapeString(v.(string)) + "</q></output>"
+ },
+ "have": func(v interface{}) bool {
+ return v != nil && v.(string) == "on"
+ },
+ "date": func(v string) string {
+ t, err := time.Parse("2006-01-02", v)
+ if err != nil {
+ panic(err)
+ }
+ return t.Format("January 2, 2006")
+ },
+ }).
+ ParseFiles(filenames...))
+}