options.go 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. package render
  2. import (
  3. "errors"
  4. "fmt"
  5. "html/template"
  6. "net/http"
  7. "github.com/go-kit/kit/log"
  8. )
  9. type Option func(*Renderer) error
  10. // AddTemplates adds filenames for the next call to parseTempaltes
  11. func AddTemplates(files ...string) Option {
  12. return func(r *Renderer) error {
  13. if len(files) == 0 {
  14. return fmt.Errorf("render: no templates passed")
  15. }
  16. r.templateFiles = files
  17. return nil
  18. }
  19. }
  20. func BaseTemplates(bases ...string) Option {
  21. return func(r *Renderer) error {
  22. r.baseTemplates = bases
  23. return nil
  24. }
  25. }
  26. // ErrorTemplate sets the filename of template that should be used for errors.
  27. func ErrorTemplate(name string) Option {
  28. return func(r *Renderer) error {
  29. r.errorTemplate = name
  30. return nil
  31. }
  32. }
  33. func FuncMap(m template.FuncMap) Option {
  34. return func(r *Renderer) error {
  35. r.funcMap = m
  36. return nil
  37. }
  38. }
  39. type FuncInjector func(*http.Request) interface{}
  40. func InjectTemplateFunc(name string, fn FuncInjector) Option {
  41. return func(r *Renderer) error {
  42. if _, has := r.tplFuncInjectors[name]; has {
  43. return fmt.Errorf("injection %s name already taken", name)
  44. }
  45. r.tplFuncInjectors[name] = fn
  46. return nil
  47. }
  48. }
  49. func SetLogger(l log.Logger) Option {
  50. return func(r *Renderer) error {
  51. if l == nil {
  52. return errors.New("render: nil logger passed")
  53. }
  54. r.log = l
  55. return nil
  56. }
  57. }