From 55ad067967c63d3943d18cee21fbf114d1ef7619 Mon Sep 17 00:00:00 2001 From: Stephen McQuay Date: Tue, 5 Mar 2013 22:54:29 -0800 Subject: [PATCH] dollarize the money --- template.go | 11 ++++++++++- templates/index.html | 2 +- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/template.go b/template.go index ccf70b4..52f0f88 100644 --- a/template.go +++ b/template.go @@ -1,6 +1,7 @@ package main import ( + "fmt" "html/template" "path/filepath" "sync" @@ -9,13 +10,21 @@ import ( var cachedTemplates = map[string]*template.Template{} var cachedMutex sync.Mutex +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 } - t := template.New("_base.html") + t := template.New("_base.html").Funcs(funcs) t = template.Must(t.ParseFiles( "templates/_base.html", filepath.Join(*template_dir, name), diff --git a/templates/index.html b/templates/index.html index da53cb3..44fa8c2 100644 --- a/templates/index.html +++ b/templates/index.html @@ -17,7 +17,7 @@ {{ .Name }} - {{ .Money }} + {{ dollarize .Money }} {{ end }}