2018-12-06 15:18:05 +01:00
|
|
|
package html
|
|
|
|
|
|
|
|
import (
|
2020-07-07 09:01:04 +02:00
|
|
|
"errors"
|
2018-12-06 15:18:05 +01:00
|
|
|
"html/template"
|
2020-04-15 18:33:30 +02:00
|
|
|
"io"
|
2018-12-06 15:18:05 +01:00
|
|
|
"net/http"
|
2020-07-07 09:01:04 +02:00
|
|
|
"sync"
|
2018-12-06 15:18:05 +01:00
|
|
|
|
|
|
|
"github.com/oxtoacart/bpool"
|
|
|
|
)
|
|
|
|
|
2020-07-07 09:01:04 +02:00
|
|
|
var (
|
|
|
|
ErrLayoutNotFound = errors.New("layout not found")
|
|
|
|
)
|
|
|
|
|
2018-12-06 15:18:05 +01:00
|
|
|
// TemplateService is a template/html based templating service
|
|
|
|
type TemplateService struct {
|
2020-07-07 09:01:04 +02:00
|
|
|
mutex sync.RWMutex
|
|
|
|
blocks map[string]string
|
|
|
|
layouts map[string]*template.Template
|
|
|
|
loader Loader
|
|
|
|
pool *bpool.BufferPool
|
|
|
|
helpers template.FuncMap
|
|
|
|
devMode bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *TemplateService) AddBlock(name string, blockContent string) {
|
|
|
|
t.mutex.Lock()
|
|
|
|
defer t.mutex.Unlock()
|
|
|
|
|
|
|
|
t.blocks[name] = blockContent
|
2018-12-06 15:18:05 +01:00
|
|
|
}
|
|
|
|
|
2020-07-07 09:01:04 +02:00
|
|
|
func (t *TemplateService) LoadLayout(name string, layoutContent string) error {
|
2020-01-24 13:09:16 +01:00
|
|
|
tmpl := template.New(name)
|
|
|
|
tmpl.Funcs(t.helpers)
|
2018-12-06 15:18:05 +01:00
|
|
|
|
2020-07-07 09:01:04 +02:00
|
|
|
t.mutex.RLock()
|
|
|
|
for _, b := range t.blocks {
|
2020-01-24 13:09:16 +01:00
|
|
|
if _, err := tmpl.Parse(b); err != nil {
|
2020-07-07 09:01:04 +02:00
|
|
|
t.mutex.RUnlock()
|
2020-01-24 13:09:16 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2020-07-07 09:01:04 +02:00
|
|
|
t.mutex.RUnlock()
|
2020-01-24 13:09:16 +01:00
|
|
|
|
2020-07-07 09:01:04 +02:00
|
|
|
tmpl, err := tmpl.Parse(layoutContent)
|
2018-12-06 15:18:05 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-07-07 09:01:04 +02:00
|
|
|
t.mutex.Lock()
|
|
|
|
t.layouts[name] = tmpl
|
|
|
|
t.mutex.Unlock()
|
2020-01-24 13:09:16 +01:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-07-07 09:01:04 +02:00
|
|
|
// Load parses templates via the configured template loader.
|
|
|
|
func (t *TemplateService) Load() error {
|
|
|
|
return t.loader.Load(t)
|
2018-12-06 15:18:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// RenderPage renders a template to the given http.ResponseWriter
|
|
|
|
func (t *TemplateService) RenderPage(w http.ResponseWriter, templateName string, data interface{}) error {
|
2020-07-07 09:01:04 +02:00
|
|
|
if t.devMode {
|
|
|
|
if err := t.loader.Load(t); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2018-12-06 15:18:05 +01:00
|
|
|
|
|
|
|
// Ensure the template exists in the map.
|
2020-07-07 09:01:04 +02:00
|
|
|
tmpl, ok := t.layouts[templateName]
|
2018-12-06 15:18:05 +01:00
|
|
|
if !ok {
|
2020-07-07 09:01:04 +02:00
|
|
|
return ErrLayoutNotFound
|
2018-12-06 15:18:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create a buffer to temporarily write to and check if any errors were encountered.
|
|
|
|
buf := t.pool.Get()
|
|
|
|
defer t.pool.Put(buf)
|
|
|
|
|
|
|
|
if err := tmpl.ExecuteTemplate(buf, templateName, data); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set the header and write the buffer to the http.ResponseWriter
|
|
|
|
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
|
|
|
_, err := buf.WriteTo(w)
|
|
|
|
return err
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-07-07 09:01:04 +02:00
|
|
|
// Render renders a layout to the given io.Writer.
|
|
|
|
func (t *TemplateService) Render(w io.Writer, layoutName string, data interface{}) error {
|
|
|
|
if t.devMode {
|
|
|
|
if err := t.loader.Load(t); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2020-04-15 18:33:30 +02:00
|
|
|
|
|
|
|
// Ensure the template exists in the map.
|
2020-07-07 09:01:04 +02:00
|
|
|
tmpl, ok := t.layouts[layoutName]
|
2020-04-15 18:33:30 +02:00
|
|
|
if !ok {
|
2020-07-07 09:01:04 +02:00
|
|
|
return ErrLayoutNotFound
|
2020-04-15 18:33:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create a buffer to temporarily write to and check if any errors were encountered.
|
|
|
|
buf := t.pool.Get()
|
|
|
|
defer t.pool.Put(buf)
|
|
|
|
|
2020-07-07 09:01:04 +02:00
|
|
|
if err := tmpl.ExecuteTemplate(buf, layoutName, data); err != nil {
|
2020-04-15 18:33:30 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err := buf.WriteTo(w)
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-12-06 15:18:05 +01:00
|
|
|
// NewTemplateService returns a new Service
|
2020-07-07 09:01:04 +02:00
|
|
|
func NewTemplateService(loader Loader, funcs ...OptionFunc) *TemplateService {
|
2018-12-06 15:18:05 +01:00
|
|
|
options := defaultOptions()
|
|
|
|
for _, f := range funcs {
|
|
|
|
f(options)
|
|
|
|
}
|
|
|
|
return &TemplateService{
|
2020-07-07 09:01:04 +02:00
|
|
|
blocks: make(map[string]string),
|
|
|
|
layouts: make(map[string]*template.Template),
|
|
|
|
pool: bpool.NewBufferPool(options.PoolSize),
|
|
|
|
helpers: options.Helpers,
|
|
|
|
loader: loader,
|
|
|
|
devMode: options.DevMode,
|
2018-12-06 15:18:05 +01:00
|
|
|
}
|
|
|
|
}
|