goweb/template/html/service.go

136 lines
2.9 KiB
Go
Raw Permalink Normal View History

2018-12-06 15:18:05 +01:00
package html
import (
"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"
"sync"
2018-12-06 15:18:05 +01:00
"github.com/oxtoacart/bpool"
)
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 {
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
}
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
t.mutex.RLock()
for _, b := range t.blocks {
2020-01-24 13:09:16 +01:00
if _, err := tmpl.Parse(b); err != nil {
t.mutex.RUnlock()
2020-01-24 13:09:16 +01:00
return err
}
}
t.mutex.RUnlock()
2020-01-24 13:09:16 +01:00
tmpl, err := tmpl.Parse(layoutContent)
2018-12-06 15:18:05 +01:00
if err != nil {
return err
}
t.mutex.Lock()
t.layouts[name] = tmpl
t.mutex.Unlock()
2020-01-24 13:09:16 +01:00
return nil
}
// 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 {
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.
tmpl, ok := t.layouts[templateName]
2018-12-06 15:18:05 +01:00
if !ok {
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
}
// 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.
tmpl, ok := t.layouts[layoutName]
2020-04-15 18:33:30 +02:00
if !ok {
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)
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
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{
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
}
}