mirror of
https://github.com/Theodor-Springmann-Stiftung/kgpz_web.git
synced 2025-10-28 16:45:32 +00:00
XML parsing overhaul
This commit is contained in:
@@ -92,13 +92,13 @@ func (c *TemplateContext) Globals() map[string]string {
|
||||
return c.globals
|
||||
}
|
||||
|
||||
func (c *TemplateContext) Template(fsys fs.FS) (*template.Template, error) {
|
||||
t, err := readTemplates(fsys, nil, c.globals)
|
||||
func (c *TemplateContext) Template(fsys fs.FS, funcmap *template.FuncMap) (*template.Template, error) {
|
||||
t, err := readTemplates(fsys, nil, c.globals, funcmap)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
t, err = readTemplates(fsys, t, c.locals)
|
||||
t, err = readTemplates(fsys, t, c.locals, funcmap)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -106,14 +106,20 @@ func (c *TemplateContext) Template(fsys fs.FS) (*template.Template, error) {
|
||||
return t, nil
|
||||
}
|
||||
|
||||
func readTemplates(fsys fs.FS, t *template.Template, paths map[string]string) (*template.Template, error) {
|
||||
func readTemplates(fsys fs.FS, t *template.Template, paths map[string]string, funcmap *template.FuncMap) (*template.Template, error) {
|
||||
for k, v := range paths {
|
||||
text, err := fs.ReadFile(fsys, v)
|
||||
if err != nil {
|
||||
return nil, NewError(FileAccessError, v)
|
||||
}
|
||||
|
||||
temp, err := template.New(k).Parse(string(text))
|
||||
temp := template.New(k)
|
||||
|
||||
if funcmap != nil {
|
||||
temp.Funcs(*funcmap)
|
||||
}
|
||||
|
||||
temp, err = temp.Parse(string(text))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -5,6 +5,9 @@ import (
|
||||
"io"
|
||||
"io/fs"
|
||||
"sync"
|
||||
|
||||
"github.com/Theodor-Springmann-Stiftung/kgpz_web/app"
|
||||
"github.com/Theodor-Springmann-Stiftung/kgpz_web/functions"
|
||||
)
|
||||
|
||||
type Engine struct {
|
||||
@@ -16,13 +19,28 @@ type Engine struct {
|
||||
FuncMap template.FuncMap
|
||||
}
|
||||
|
||||
func NewEngine(layouts, templates *fs.FS) *Engine {
|
||||
return &Engine{
|
||||
// INFO: We pass the app here to be able to access the config and other data for functions
|
||||
// which also means we must reload the engine if the app changes
|
||||
func NewEngine(layouts, templates *fs.FS, app *app.KGPZ) *Engine {
|
||||
e := Engine{
|
||||
mu: &sync.Mutex{},
|
||||
LayoutRegistry: NewLayoutRegistry(*layouts),
|
||||
TemplateRegistry: NewTemplateRegistry(*templates),
|
||||
FuncMap: template.FuncMap{},
|
||||
}
|
||||
|
||||
e.MapFuncs(app)
|
||||
return &e
|
||||
}
|
||||
|
||||
func (e *Engine) MapFuncs(app *app.KGPZ) error {
|
||||
e.mu.Lock()
|
||||
e.FuncMap = make(map[string]interface{})
|
||||
e.mu.Unlock()
|
||||
|
||||
e.AddFunc("GetDate", functions.GetDate)
|
||||
e.AddFunc("MonthName", functions.MonthName)
|
||||
e.AddFunc("MonthNameShort", functions.MonthNameShort)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (e *Engine) Load() error {
|
||||
@@ -43,18 +61,44 @@ func (e *Engine) Load() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (e *Engine) Reload() error {
|
||||
wg := sync.WaitGroup{}
|
||||
wg.Add(2)
|
||||
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
e.LayoutRegistry.Reset()
|
||||
}()
|
||||
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
e.TemplateRegistry.Reset()
|
||||
}()
|
||||
|
||||
wg.Wait()
|
||||
return nil
|
||||
}
|
||||
|
||||
// INFO: fn is a function that returns either one value or two values, the second one being an error
|
||||
func (e *Engine) AddFunc(name string, fn interface{}) {
|
||||
e.mu.Lock()
|
||||
defer e.mu.Unlock()
|
||||
e.FuncMap[name] = fn
|
||||
}
|
||||
|
||||
func (e *Engine) Render(out io.Writer, path string, data interface{}, layout ...string) error {
|
||||
// TODO: check if a reload is needed if files on disk have changed
|
||||
|
||||
e.mu.Lock()
|
||||
defer e.mu.Unlock()
|
||||
var l *template.Template
|
||||
if layout == nil || len(layout) == 0 {
|
||||
lay, err := e.LayoutRegistry.Default()
|
||||
lay, err := e.LayoutRegistry.Default(&e.FuncMap)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
l = lay
|
||||
} else {
|
||||
lay, err := e.LayoutRegistry.Layout(layout[0])
|
||||
lay, err := e.LayoutRegistry.Layout(layout[0], &e.FuncMap)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -66,7 +110,7 @@ func (e *Engine) Render(out io.Writer, path string, data interface{}, layout ...
|
||||
return err
|
||||
}
|
||||
|
||||
err = e.TemplateRegistry.Add(path, lay)
|
||||
err = e.TemplateRegistry.Add(path, lay, &e.FuncMap)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -33,6 +33,12 @@ func (r *LayoutRegistry) Register(fs fs.FS) *LayoutRegistry {
|
||||
return NewLayoutRegistry(merged_fs.MergeMultiple(fs, r.layoutsFS))
|
||||
}
|
||||
|
||||
func (r *LayoutRegistry) Reset() error {
|
||||
r.cache.Clear()
|
||||
r.once = sync.Once{}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *LayoutRegistry) Load() error {
|
||||
r.once.Do(func() {
|
||||
err := r.load()
|
||||
@@ -74,7 +80,7 @@ func (r *LayoutRegistry) load() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *LayoutRegistry) Layout(name string) (*template.Template, error) {
|
||||
func (r *LayoutRegistry) Layout(name string, funcmap *template.FuncMap) (*template.Template, error) {
|
||||
cached, ok := r.cache.Load(name)
|
||||
if ok {
|
||||
return cached.(*template.Template), nil
|
||||
@@ -87,7 +93,7 @@ func (r *LayoutRegistry) Layout(name string) (*template.Template, error) {
|
||||
return nil, NewError(NoTemplateError, name)
|
||||
}
|
||||
|
||||
t, err := context.Template(r.layoutsFS)
|
||||
t, err := context.Template(r.layoutsFS, funcmap)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -97,6 +103,6 @@ func (r *LayoutRegistry) Layout(name string) (*template.Template, error) {
|
||||
return t, nil
|
||||
}
|
||||
|
||||
func (r *LayoutRegistry) Default() (*template.Template, error) {
|
||||
return r.Layout(DEFAULT_LAYOUT_NAME)
|
||||
func (r *LayoutRegistry) Default(funcmap *template.FuncMap) (*template.Template, error) {
|
||||
return r.Layout(DEFAULT_LAYOUT_NAME, funcmap)
|
||||
}
|
||||
|
||||
@@ -33,6 +33,12 @@ func (r *TemplateRegistry) Register(path string, fs fs.FS) *TemplateRegistry {
|
||||
return NewTemplateRegistry(merged_fs.MergeMultiple(fs, r.routesFS))
|
||||
}
|
||||
|
||||
func (r *TemplateRegistry) Reset() error {
|
||||
r.cache.Clear()
|
||||
r.once = sync.Once{}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *TemplateRegistry) Load() error {
|
||||
r.once.Do(func() {
|
||||
err := r.load()
|
||||
@@ -79,7 +85,7 @@ func (r *TemplateRegistry) load() error {
|
||||
|
||||
// This function takes a template (typically a layout) and adds all the templates of
|
||||
// a given directory path to it. This is useful for adding a layout to a template.
|
||||
func (r *TemplateRegistry) Add(path string, t *template.Template) error {
|
||||
func (r *TemplateRegistry) Add(path string, t *template.Template, funcmap *template.FuncMap) error {
|
||||
temp, ok := r.cache.Load(path)
|
||||
if !ok {
|
||||
r.Load()
|
||||
@@ -88,14 +94,14 @@ func (r *TemplateRegistry) Add(path string, t *template.Template) error {
|
||||
return NewError(NoTemplateError, path)
|
||||
}
|
||||
|
||||
template, err := tc.Template(r.routesFS)
|
||||
template, err := tc.Template(r.routesFS, funcmap)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
r.cache.Store(path, template)
|
||||
|
||||
return r.Add(path, t)
|
||||
return r.Add(path, t, funcmap)
|
||||
}
|
||||
|
||||
casted := temp.(*template.Template)
|
||||
|
||||
Reference in New Issue
Block a user