goweb/template/html/service.go

117 lines
2.6 KiB
Go
Raw Normal View History

2018-12-06 15:18:05 +01:00
package html
import (
"fmt"
"html/template"
2020-01-24 13:09:16 +01:00
"io/ioutil"
2018-12-06 15:18:05 +01:00
"net/http"
"path/filepath"
"github.com/oxtoacart/bpool"
)
// TemplateService is a template/html based templating service
type TemplateService struct {
templates map[string]*template.Template
pool *bpool.BufferPool
helpers template.FuncMap
}
2020-01-24 13:09:16 +01:00
func (t *TemplateService) LoadLayout(name string, layout string, blocks []string) error {
tmpl := template.New(name)
tmpl.Funcs(t.helpers)
2018-12-06 15:18:05 +01:00
2020-01-24 13:09:16 +01:00
for _, b := range blocks {
if _, err := tmpl.Parse(b); err != nil {
return err
}
}
tmpl, err := tmpl.Parse(layout)
2018-12-06 15:18:05 +01:00
if err != nil {
return err
}
2020-01-24 13:09:16 +01:00
t.templates[name] = tmpl
return nil
}
// LoadTemplatesDir loads the templates used by the service
func (t *TemplateService) LoadTemplatesDir(templatesDir string) error {
layoutFiles, err := filepath.Glob(filepath.Join(templatesDir, "layouts", "*.tmpl"))
2018-12-06 15:18:05 +01:00
if err != nil {
return err
}
2020-01-24 13:09:16 +01:00
blockFiles, err := filepath.Glob(filepath.Join(templatesDir, "blocks", "*.tmpl"))
if err != nil {
return err
}
blockTemplates := make([]string, 0, len(blockFiles))
2018-12-06 15:18:05 +01:00
2020-01-24 13:09:16 +01:00
for _, f := range blockFiles {
templateData, err := ioutil.ReadFile(f)
if err != nil {
return err
}
2018-12-06 15:18:05 +01:00
2020-01-24 13:09:16 +01:00
blockTemplates = append(blockTemplates, string(templateData))
}
2018-12-06 15:18:05 +01:00
2020-01-24 13:09:16 +01:00
// Generate our templates map from our layouts/ and blocks/ directories
for _, f := range layoutFiles {
templateData, err := ioutil.ReadFile(f)
2018-12-06 15:18:05 +01:00
if err != nil {
return err
}
2020-01-24 13:09:16 +01:00
templateName := filepath.Base(f)
2018-12-06 15:18:05 +01:00
2020-01-24 13:09:16 +01:00
if err := t.LoadLayout(templateName, string(templateData), blockTemplates); err != nil {
return err
}
2018-12-06 15:18:05 +01:00
}
return nil
}
// RenderPage renders a template to the given http.ResponseWriter
func (t *TemplateService) RenderPage(w http.ResponseWriter, templateName string, data interface{}) error {
// Ensure the template exists in the map.
tmpl, ok := t.templates[templateName]
if !ok {
return fmt.Errorf("the template '%s' does not exist", templateName)
}
// 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
}
// NewTemplateService returns a new Service
func NewTemplateService(funcs ...OptionFunc) *TemplateService {
options := defaultOptions()
for _, f := range funcs {
f(options)
}
return &TemplateService{
templates: make(map[string]*template.Template),
pool: bpool.NewBufferPool(options.PoolSize),
helpers: options.Helpers,
}
}