template.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. package template
  2. import (
  3. "io"
  4. "net/http"
  5. "sync"
  6. )
  7. // IEngine interface, to be implemented for any templating engine added to the repository
  8. type IEngine interface {
  9. IEngineCore
  10. Load() error
  11. Render(out io.Writer, template string, binding interface{}, layout ...string) error
  12. }
  13. // IEngineCore interface
  14. type IEngineCore interface {
  15. AddFunc(name string, fn interface{}) IEngineCore
  16. AddFuncMap(m map[string]interface{}) IEngineCore
  17. Debug(enabled bool) IEngineCore
  18. Delims(left, right string) IEngineCore
  19. FuncMap() map[string]interface{}
  20. Layout(key string) IEngineCore
  21. Reload(enabled bool) IEngineCore
  22. }
  23. // Engine engine struct
  24. type Engine struct {
  25. IEngineCore
  26. // delimiters
  27. Left string
  28. Right string
  29. // views folder
  30. Directory string
  31. // http.FileSystem supports embedded files
  32. FileSystem http.FileSystem
  33. // views extension
  34. Extension string
  35. // layout variable name that incapsulates the template
  36. LayoutName string
  37. // determines if the engine parsed all templates
  38. Loaded bool
  39. // reload on each render
  40. ShouldReload bool
  41. // debug prints the parsed templates
  42. Verbose bool
  43. // lock for funcmap and templates
  44. Mutex sync.RWMutex
  45. // template funcmap
  46. Funcmap map[string]interface{}
  47. }
  48. // AddFunc adds the function to the template's function map.
  49. // It is legal to overwrite elements of the default actions
  50. func (e *Engine) AddFunc(name string, fn interface{}) *Engine {
  51. e.Mutex.Lock()
  52. e.Funcmap[name] = fn
  53. e.Mutex.Unlock()
  54. return e
  55. }
  56. // AddFuncMap adds the functions from a map to the template's function map.
  57. // It is legal to overwrite elements of the default actions
  58. func (e *Engine) AddFuncMap(m map[string]interface{}) *Engine {
  59. e.Mutex.Lock()
  60. for name, fn := range m {
  61. e.Funcmap[name] = fn
  62. }
  63. e.Mutex.Unlock()
  64. return e
  65. }
  66. // Debug will print the parsed templates when Load is triggered.
  67. func (e *Engine) Debug(enabled bool) *Engine {
  68. e.Verbose = enabled
  69. return e
  70. }
  71. // Delims sets the action delimiters to the specified strings, to be used in
  72. // templates. An empty delimiter stands for the
  73. // corresponding default: "{{" and "}}".
  74. func (e *Engine) Delims(left, right string) *Engine {
  75. e.Left, e.Right = left, right
  76. return e
  77. }
  78. // FuncMap returns the template's function map.
  79. func (e *Engine) FuncMap() map[string]interface{} {
  80. return e.Funcmap
  81. }
  82. // Layout defines the variable name that will incapsulate the template
  83. func (e *Engine) Layout(key string) *Engine {
  84. e.LayoutName = key
  85. return e
  86. }
  87. // Reload if set to true the templates are reloading on each render,
  88. // use it when you're in development and you don't want to restart
  89. // the application when you edit a template file.
  90. func (e *Engine) Reload(enabled bool) *Engine {
  91. e.ShouldReload = enabled
  92. return e
  93. }