keeping track of my children's money since 2013
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

61 lines
1.0KB

  1. package allowances
  2. import (
  3. "fmt"
  4. "html/template"
  5. "log"
  6. "strings"
  7. )
  8. func dollarize(value int) string {
  9. return fmt.Sprintf("$%0.2f", float32(value)/100.0)
  10. }
  11. type tmap map[string]*template.Template
  12. var tmpls tmap
  13. func getTemplates() (tmap, error) {
  14. var err error
  15. funcMap := template.FuncMap{
  16. "title": strings.Title,
  17. "dollarize": dollarize,
  18. }
  19. base, err := Asset("templates/base.html")
  20. if err != nil {
  21. return nil, err
  22. }
  23. tmpl, err := template.New("base").Funcs(funcMap).Parse(string(base))
  24. if err != nil {
  25. return nil, err
  26. }
  27. templates := make(map[string]*template.Template)
  28. templateFiles := []struct {
  29. name string
  30. path string
  31. }{
  32. {"home", "templates/index.html"},
  33. {"login", "templates/login.html"},
  34. }
  35. for _, tf := range templateFiles {
  36. a, err := Asset(tf.path)
  37. if err != nil {
  38. return nil, err
  39. }
  40. t, err := tmpl.Clone()
  41. if err != nil {
  42. return nil, err
  43. }
  44. t, err = t.Parse(string(a))
  45. if err != nil {
  46. log.Printf("XXX: %+v", err)
  47. return nil, err
  48. }
  49. templates[tf.name] = t
  50. }
  51. return templates, nil
  52. }