template: add layout loading API
This commit is contained in:
parent
bbcb571772
commit
18f2ffaaa9
1
go.mod
1
go.mod
@ -3,6 +3,7 @@ module gitlab.com/wpetit/goweb
|
|||||||
go 1.12
|
go 1.12
|
||||||
|
|
||||||
require (
|
require (
|
||||||
|
github.com/davecgh/go-spew v1.1.0
|
||||||
github.com/go-chi/chi v4.0.2+incompatible
|
github.com/go-chi/chi v4.0.2+incompatible
|
||||||
github.com/go-playground/locales v0.12.1 // indirect
|
github.com/go-playground/locales v0.12.1 // indirect
|
||||||
github.com/go-playground/universal-translator v0.16.0 // indirect
|
github.com/go-playground/universal-translator v0.16.0 // indirect
|
||||||
|
@ -6,7 +6,7 @@ import "gitlab.com/wpetit/goweb/service"
|
|||||||
// the HTML template service implementation
|
// the HTML template service implementation
|
||||||
func ServiceProvider(templateDir string, funcs ...OptionFunc) service.Provider {
|
func ServiceProvider(templateDir string, funcs ...OptionFunc) service.Provider {
|
||||||
templateService := NewTemplateService(funcs...)
|
templateService := NewTemplateService(funcs...)
|
||||||
err := templateService.LoadTemplates(templateDir)
|
err := templateService.LoadTemplatesDir(templateDir)
|
||||||
return func(container *service.Container) (interface{}, error) {
|
return func(container *service.Container) (interface{}, error) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
@ -3,6 +3,7 @@ package html
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"html/template"
|
"html/template"
|
||||||
|
"io/ioutil"
|
||||||
"net/http"
|
"net/http"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
|
||||||
@ -16,35 +17,62 @@ type TemplateService struct {
|
|||||||
helpers template.FuncMap
|
helpers template.FuncMap
|
||||||
}
|
}
|
||||||
|
|
||||||
// LoadTemplates loads the templates used by the service
|
func (t *TemplateService) LoadLayout(name string, layout string, blocks []string) error {
|
||||||
func (t *TemplateService) LoadTemplates(templatesDir string) error {
|
tmpl := template.New(name)
|
||||||
|
tmpl.Funcs(t.helpers)
|
||||||
|
|
||||||
layouts, err := filepath.Glob(filepath.Join(templatesDir, "layouts", "*.tmpl"))
|
for _, b := range blocks {
|
||||||
|
if _, err := tmpl.Parse(b); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
tmpl, err := tmpl.Parse(layout)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
blocks, err := filepath.Glob(filepath.Join(templatesDir, "blocks", "*.tmpl"))
|
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"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
blockFiles, err := filepath.Glob(filepath.Join(templatesDir, "blocks", "*.tmpl"))
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
blockTemplates := make([]string, 0, len(blockFiles))
|
||||||
|
|
||||||
|
for _, f := range blockFiles {
|
||||||
|
templateData, err := ioutil.ReadFile(f)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
blockTemplates = append(blockTemplates, string(templateData))
|
||||||
|
}
|
||||||
|
|
||||||
// Generate our templates map from our layouts/ and blocks/ directories
|
// Generate our templates map from our layouts/ and blocks/ directories
|
||||||
for _, layout := range layouts {
|
for _, f := range layoutFiles {
|
||||||
|
templateData, err := ioutil.ReadFile(f)
|
||||||
var err error
|
|
||||||
files := append(blocks, layout)
|
|
||||||
|
|
||||||
tmpl := template.New("")
|
|
||||||
tmpl.Funcs(t.helpers)
|
|
||||||
|
|
||||||
tmpl, err = tmpl.ParseFiles(files...)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
t.templates[filepath.Base(layout)] = tmpl
|
templateName := filepath.Base(f)
|
||||||
|
|
||||||
|
if err := t.LoadLayout(templateName, string(templateData), blockTemplates); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
|
Loading…
Reference in New Issue
Block a user