html.go 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. // Copyright 2014 Manu Martinez-Almeida. All rights reserved.
  2. // Use of this source code is governed by a MIT style
  3. // license that can be found in the LICENSE file.
  4. package render
  5. import (
  6. "html/template"
  7. "net/http"
  8. )
  9. // Delims represents a set of Left and Right delimiters for HTML template rendering.
  10. type Delims struct {
  11. // Left delimiter, defaults to {{.
  12. Left string
  13. // Right delimiter, defaults to }}.
  14. Right string
  15. }
  16. // HTMLRender interface is to be implemented by HTMLProduction and HTMLDebug.
  17. type HTMLRender interface {
  18. // Instance returns an HTML instance.
  19. Instance(string, any) Render
  20. }
  21. // HTMLProduction contains template reference and its delims.
  22. type HTMLProduction struct {
  23. Template *template.Template
  24. Delims Delims
  25. }
  26. // HTMLDebug contains template delims and pattern and function with file list.
  27. type HTMLDebug struct {
  28. Files []string
  29. Glob string
  30. Delims Delims
  31. FuncMap template.FuncMap
  32. }
  33. // HTML contains template reference and its name with given interface object.
  34. type HTML struct {
  35. Template *template.Template
  36. Name string
  37. Data any
  38. }
  39. var htmlContentType = []string{"text/html; charset=utf-8"}
  40. // Instance (HTMLProduction) returns an HTML instance which it realizes Render interface.
  41. func (r HTMLProduction) Instance(name string, data any) Render {
  42. return HTML{
  43. Template: r.Template,
  44. Name: name,
  45. Data: data,
  46. }
  47. }
  48. // Instance (HTMLDebug) returns an HTML instance which it realizes Render interface.
  49. func (r HTMLDebug) Instance(name string, data any) Render {
  50. return HTML{
  51. Template: r.loadTemplate(),
  52. Name: name,
  53. Data: data,
  54. }
  55. }
  56. func (r HTMLDebug) loadTemplate() *template.Template {
  57. if r.FuncMap == nil {
  58. r.FuncMap = template.FuncMap{}
  59. }
  60. if len(r.Files) > 0 {
  61. return template.Must(template.New("").Delims(r.Delims.Left, r.Delims.Right).Funcs(r.FuncMap).ParseFiles(r.Files...))
  62. }
  63. if r.Glob != "" {
  64. return template.Must(template.New("").Delims(r.Delims.Left, r.Delims.Right).Funcs(r.FuncMap).ParseGlob(r.Glob))
  65. }
  66. panic("the HTML debug render was created without files or glob pattern")
  67. }
  68. // Render (HTML) executes template and writes its result with custom ContentType for response.
  69. func (r HTML) Render(w http.ResponseWriter) error {
  70. r.WriteContentType(w)
  71. if r.Name == "" {
  72. return r.Template.Execute(w, r.Data)
  73. }
  74. return r.Template.ExecuteTemplate(w, r.Name, r.Data)
  75. }
  76. // WriteContentType (HTML) writes HTML ContentType.
  77. func (r HTML) WriteContentType(w http.ResponseWriter) {
  78. writeContentType(w, htmlContentType)
  79. }