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.
12
// Delims represents a set of Left and Right delimiters for HTML template rendering.
14
// Left delimiter, defaults to {{.
16
// Right delimiter, defaults to }}.
20
// HTMLRender interface is to be implemented by HTMLProduction and HTMLDebug.
21
type HTMLRender interface {
22
// Instance returns an HTML instance.
23
Instance(string, any) Render
26
// HTMLProduction contains template reference and its delims.
27
type HTMLProduction struct {
28
Template *template.Template
32
// HTMLDebug contains template delims and pattern and function with file list.
33
type HTMLDebug struct {
37
FuncMap template.FuncMap
40
// HTML contains template reference and its name with given interface object.
42
Template *template.Template
47
var htmlContentType = []string{"text/html; charset=utf-8"}
49
// Instance (HTMLProduction) returns an HTML instance which it realizes Render interface.
50
func (r HTMLProduction) Instance(name string, data any) Render {
58
// Instance (HTMLDebug) returns an HTML instance which it realizes Render interface.
59
func (r HTMLDebug) Instance(name string, data any) Render {
61
Template: r.loadTemplate(),
66
func (r HTMLDebug) loadTemplate() *template.Template {
68
r.FuncMap = template.FuncMap{}
71
return template.Must(template.New("").Delims(r.Delims.Left, r.Delims.Right).Funcs(r.FuncMap).ParseFiles(r.Files...))
74
return template.Must(template.New("").Delims(r.Delims.Left, r.Delims.Right).Funcs(r.FuncMap).ParseGlob(r.Glob))
76
panic("the HTML debug render was created without files or glob pattern")
79
// Render (HTML) executes template and writes its result with custom ContentType for response.
80
func (r HTML) Render(w http.ResponseWriter) error {
84
return r.Template.Execute(w, r.Data)
86
return r.Template.ExecuteTemplate(w, r.Name, r.Data)
89
// WriteContentType (HTML) writes HTML ContentType.
90
func (r HTML) WriteContentType(w http.ResponseWriter) {
91
writeContentType(w, htmlContentType)