mirror of
https://github.com/Theodor-Springmann-Stiftung/musenalm.git
synced 2025-10-29 09:15:33 +00:00
Introduced templating and views
This commit is contained in:
14
templating/consts.go
Normal file
14
templating/consts.go
Normal file
@@ -0,0 +1,14 @@
|
||||
package templating
|
||||
|
||||
var TEMPLATE_FORMATS = []string{".html", ".tmpl", ".gotmpl", ".gotemplate", ".gohtml", ".gohtmltemplate"}
|
||||
|
||||
const TEMPLATE_GLOBAL_CONTEXT_NAME = "globals"
|
||||
const TEMPLATE_ROOT_NAME = "root"
|
||||
const TEMPLATE_LOCAL_CONTEXT_NAME = "locals"
|
||||
const TEMPLATE_GLOBAL_PREFIX = "_"
|
||||
const TEMPLATE_COMPONENT_DIRECTORY = "components"
|
||||
const TEMPLATE_HEAD = "head"
|
||||
const TEMPLATE_BODY = "body"
|
||||
const TEMPLATE_HEADERS = "headers"
|
||||
const ROOT_LAYOUT_NAME = "root"
|
||||
const DEFAULT_LAYOUT_NAME = "default"
|
||||
148
templating/context.go
Normal file
148
templating/context.go
Normal file
@@ -0,0 +1,148 @@
|
||||
package templating
|
||||
|
||||
import (
|
||||
"html/template"
|
||||
"io/fs"
|
||||
"path/filepath"
|
||||
"slices"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type TemplateContext struct {
|
||||
// WARNING: Path is a URL path, NOT a filesystem path
|
||||
Path string
|
||||
// WARNING: The keys of these maps are template names, NOT filesystem paths
|
||||
// The values are FS paths absolute from the root directory of the templates FS
|
||||
locals map[string]string
|
||||
globals map[string]string
|
||||
}
|
||||
|
||||
func NewTemplateContext(path string) TemplateContext {
|
||||
return TemplateContext{
|
||||
Path: path,
|
||||
locals: make(map[string]string),
|
||||
globals: make(map[string]string),
|
||||
}
|
||||
}
|
||||
|
||||
func (c *TemplateContext) Parse(fsys fs.FS) error {
|
||||
fspath := PathToFSPath(c.Path)
|
||||
entries, err := fs.ReadDir(fsys, fspath)
|
||||
|
||||
if err != nil {
|
||||
return NewError(InvalidPathError, c.Path)
|
||||
}
|
||||
|
||||
for _, e := range entries {
|
||||
if e.IsDir() {
|
||||
// INFO: components in the components directory can be overwritten
|
||||
// by components in the base directory down below
|
||||
// TODO: Maybe allow for subdirectories in the components directory?
|
||||
if e.Name() == TEMPLATE_COMPONENT_DIRECTORY {
|
||||
entries, err := fs.ReadDir(fsys, filepath.Join(fspath, e.Name()))
|
||||
if err != nil {
|
||||
return NewError(FileAccessError, filepath.Join(fspath, e.Name()))
|
||||
}
|
||||
|
||||
for _, e := range entries {
|
||||
ext := filepath.Ext(e.Name())
|
||||
|
||||
if !slices.Contains(TEMPLATE_FORMATS, ext) {
|
||||
continue
|
||||
}
|
||||
|
||||
name := strings.TrimSuffix(e.Name(), ext)
|
||||
if strings.HasPrefix(e.Name(), TEMPLATE_GLOBAL_PREFIX) {
|
||||
c.globals[name] = filepath.Join(fspath, TEMPLATE_COMPONENT_DIRECTORY, e.Name())
|
||||
} else {
|
||||
c.locals[name] = filepath.Join(fspath, TEMPLATE_COMPONENT_DIRECTORY, e.Name())
|
||||
}
|
||||
}
|
||||
continue
|
||||
}
|
||||
}
|
||||
|
||||
ext := filepath.Ext(e.Name())
|
||||
|
||||
if !slices.Contains(TEMPLATE_FORMATS, ext) {
|
||||
continue
|
||||
}
|
||||
|
||||
name := strings.TrimSuffix(e.Name(), ext)
|
||||
if strings.HasPrefix(e.Name(), TEMPLATE_GLOBAL_PREFIX) {
|
||||
c.globals[name] = filepath.Join(fspath, e.Name())
|
||||
} else {
|
||||
c.locals[name] = filepath.Join(fspath, e.Name())
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *TemplateContext) SetGlobals(globals map[string]string) error {
|
||||
// INFO: this allows for overwriting of existing global keys.
|
||||
// Make sure to call this appopriately before or after Parse(), depending on your use case
|
||||
for k, v := range globals {
|
||||
c.globals[k] = v
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *TemplateContext) Globals() map[string]string {
|
||||
return c.globals
|
||||
}
|
||||
|
||||
func (c *TemplateContext) Template(fsys fs.FS, funcmap *template.FuncMap) (*template.Template, error) {
|
||||
// TODO: locals need to be in order: root, head, body
|
||||
t, err := readTemplates(fsys, nil, c.locals, funcmap)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
t, err = readTemplates(fsys, t, c.globals, funcmap)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return t, nil
|
||||
}
|
||||
|
||||
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 := template.New(k)
|
||||
|
||||
if funcmap != nil {
|
||||
temp.Funcs(*funcmap)
|
||||
}
|
||||
|
||||
temp, err = temp.Parse(string(text))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if t == nil {
|
||||
t = temp
|
||||
continue
|
||||
}
|
||||
|
||||
for _, template := range temp.Templates() {
|
||||
_, err = t.AddParseTree(template.Name(), template.Tree)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
_, err = t.AddParseTree(temp.Name(), temp.Tree)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return t, nil
|
||||
}
|
||||
148
templating/engine.go
Normal file
148
templating/engine.go
Normal file
@@ -0,0 +1,148 @@
|
||||
package templating
|
||||
|
||||
import (
|
||||
"html/template"
|
||||
"io"
|
||||
"io/fs"
|
||||
"sync"
|
||||
)
|
||||
|
||||
const (
|
||||
ASSETS_URL_PREFIX = "/assets"
|
||||
)
|
||||
|
||||
type Engine struct {
|
||||
// NOTE: LayoutRegistry and TemplateRegistry have their own syncronization & cache and do not require a mutex here
|
||||
LayoutRegistry *LayoutRegistry
|
||||
TemplateRegistry *TemplateRegistry
|
||||
|
||||
mu *sync.Mutex
|
||||
FuncMap template.FuncMap
|
||||
GlobalData map[string]interface{}
|
||||
}
|
||||
|
||||
// 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) *Engine {
|
||||
e := Engine{
|
||||
mu: &sync.Mutex{},
|
||||
LayoutRegistry: NewLayoutRegistry(*layouts),
|
||||
TemplateRegistry: NewTemplateRegistry(*templates),
|
||||
FuncMap: make(template.FuncMap),
|
||||
}
|
||||
e.funcs()
|
||||
return &e
|
||||
}
|
||||
|
||||
func (e *Engine) funcs() error {
|
||||
e.mu.Lock()
|
||||
e.mu.Unlock()
|
||||
return nil
|
||||
}
|
||||
|
||||
func (e *Engine) Globals(data map[string]interface{}) {
|
||||
e.mu.Lock()
|
||||
defer e.mu.Unlock()
|
||||
if e.GlobalData == nil {
|
||||
e.GlobalData = data
|
||||
} else {
|
||||
for k, v := range data {
|
||||
(e.GlobalData)[k] = v
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (e *Engine) Load() error {
|
||||
wg := sync.WaitGroup{}
|
||||
wg.Add(2)
|
||||
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
e.LayoutRegistry.Load()
|
||||
}()
|
||||
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
e.TemplateRegistry.Load()
|
||||
}()
|
||||
|
||||
wg.Wait()
|
||||
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) AddFuncs(funcs map[string]interface{}) {
|
||||
e.mu.Lock()
|
||||
defer e.mu.Unlock()
|
||||
for k, v := range funcs {
|
||||
e.FuncMap[k] = v
|
||||
}
|
||||
}
|
||||
|
||||
func (e *Engine) Render(out io.Writer, path string, ld map[string]interface{}, layout ...string) error {
|
||||
// TODO: check if a reload is needed if files on disk have changed
|
||||
gd := e.GlobalData
|
||||
if e.GlobalData != nil {
|
||||
for k, v := range ld {
|
||||
gd[k] = v
|
||||
}
|
||||
}
|
||||
|
||||
e.mu.Lock()
|
||||
defer e.mu.Unlock()
|
||||
var l *template.Template
|
||||
if layout == nil || len(layout) == 0 {
|
||||
lay, err := e.LayoutRegistry.Default(&e.FuncMap)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
l = lay
|
||||
} else {
|
||||
lay, err := e.LayoutRegistry.Layout(layout[0], &e.FuncMap)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
l = lay
|
||||
}
|
||||
|
||||
lay, err := l.Clone()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = e.TemplateRegistry.Add(path, lay, &e.FuncMap)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = lay.Execute(out, gd)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
21
templating/errors.go
Normal file
21
templating/errors.go
Normal file
@@ -0,0 +1,21 @@
|
||||
package templating
|
||||
|
||||
import "errors"
|
||||
|
||||
var InvalidPathError = errors.New("Invalid path. Must be a directory.")
|
||||
var NoTemplateError = errors.New("No template found for this name")
|
||||
var InvalidTemplateError = errors.New("invalid template")
|
||||
var FileAccessError = errors.New("could not stat file or directory")
|
||||
|
||||
type FSError[T error] struct {
|
||||
File string
|
||||
Err T
|
||||
}
|
||||
|
||||
func NewError[T error](t T, file string) FSError[T] {
|
||||
return FSError[T]{File: file, Err: t}
|
||||
}
|
||||
|
||||
func (e FSError[T]) Error() string {
|
||||
return e.Err.Error() + ": " + e.File
|
||||
}
|
||||
32
templating/helpers.go
Normal file
32
templating/helpers.go
Normal file
@@ -0,0 +1,32 @@
|
||||
package templating
|
||||
|
||||
import "strings"
|
||||
|
||||
func PathToFSPath(p string) string {
|
||||
if p == "/" {
|
||||
return "."
|
||||
}
|
||||
|
||||
p = strings.TrimPrefix(p, "/")
|
||||
p = strings.TrimSuffix(p, "/")
|
||||
|
||||
return p
|
||||
}
|
||||
|
||||
func FSPathToPath(p string) string {
|
||||
if p == "." {
|
||||
return "/"
|
||||
}
|
||||
|
||||
p = strings.TrimPrefix(p, ".")
|
||||
|
||||
if !strings.HasPrefix(p, "/") {
|
||||
p = "/" + p
|
||||
}
|
||||
|
||||
if !strings.HasSuffix(p, "/") {
|
||||
p = p + "/"
|
||||
}
|
||||
|
||||
return p
|
||||
}
|
||||
108
templating/layout_registry.go
Normal file
108
templating/layout_registry.go
Normal file
@@ -0,0 +1,108 @@
|
||||
package templating
|
||||
|
||||
import (
|
||||
"html/template"
|
||||
"io/fs"
|
||||
"sync"
|
||||
|
||||
"github.com/Theodor-Springmann-Stiftung/musenalm/helpers"
|
||||
"github.com/yalue/merged_fs"
|
||||
)
|
||||
|
||||
// TODO: Implement Handler interface, maybe in template? But then template would need to know about the layout registry
|
||||
// Function signature: func (t *templateHandler) ServeHTTP(w http.ResponseWriter, r *http.Request)
|
||||
// A static Handler could incoporate both the layout registry and the template registry and serve templates that dont need any data
|
||||
// INFO: this ist thread-safe and safe to call in a handler or middleware
|
||||
type LayoutRegistry struct {
|
||||
layoutsFS fs.FS
|
||||
once sync.Once
|
||||
// INFO: Layout & cache keys are template directory names
|
||||
layouts map[string]TemplateContext
|
||||
// WARNING: maybe this is too early for caching?
|
||||
cache sync.Map
|
||||
}
|
||||
|
||||
func NewLayoutRegistry(routes fs.FS) *LayoutRegistry {
|
||||
return &LayoutRegistry{
|
||||
layoutsFS: routes,
|
||||
}
|
||||
}
|
||||
|
||||
// NOTE: Upon registering a new layout dir, we return a new LayoutRegistry
|
||||
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()
|
||||
helpers.Assert(err, "Error loading layouts. Exiting.")
|
||||
})
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *LayoutRegistry) load() error {
|
||||
layouts := make(map[string]TemplateContext)
|
||||
rootcontext := NewTemplateContext(".")
|
||||
err := rootcontext.Parse(r.layoutsFS)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
globals := rootcontext.Globals()
|
||||
|
||||
entries, err := fs.ReadDir(r.layoutsFS, ".")
|
||||
if err != nil {
|
||||
return NewError(FileAccessError, ".")
|
||||
}
|
||||
|
||||
for _, e := range entries {
|
||||
if !e.IsDir() || e.Name() == TEMPLATE_COMPONENT_DIRECTORY {
|
||||
continue
|
||||
}
|
||||
|
||||
url := FSPathToPath(e.Name())
|
||||
context := NewTemplateContext(url)
|
||||
context.SetGlobals(globals)
|
||||
context.Parse(r.layoutsFS)
|
||||
|
||||
layouts[e.Name()] = context
|
||||
}
|
||||
|
||||
r.layouts = layouts
|
||||
return nil
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
// TODO: What todo on errors?
|
||||
r.Load()
|
||||
context, ok := r.layouts[name]
|
||||
if !ok {
|
||||
return nil, NewError(NoTemplateError, name)
|
||||
}
|
||||
|
||||
t, err := context.Template(r.layoutsFS, funcmap)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
r.cache.Store(name, t)
|
||||
|
||||
return t, nil
|
||||
}
|
||||
|
||||
func (r *LayoutRegistry) Default(funcmap *template.FuncMap) (*template.Template, error) {
|
||||
return r.Layout(DEFAULT_LAYOUT_NAME, funcmap)
|
||||
}
|
||||
120
templating/template_registry.go
Normal file
120
templating/template_registry.go
Normal file
@@ -0,0 +1,120 @@
|
||||
package templating
|
||||
|
||||
import (
|
||||
"html/template"
|
||||
"io/fs"
|
||||
"os"
|
||||
"strings"
|
||||
"sync"
|
||||
|
||||
"github.com/Theodor-Springmann-Stiftung/musenalm/helpers"
|
||||
"github.com/yalue/merged_fs"
|
||||
)
|
||||
|
||||
// INFO: this ist thread-safe and safe to call in a handler or middleware
|
||||
type TemplateRegistry struct {
|
||||
routesFS fs.FS
|
||||
once sync.Once
|
||||
// INFO: Template & cache keys are directory routing paths, with '/' as root
|
||||
// INFO: we don't need a mutex here since this is set in Load() protected by Once().
|
||||
templates map[string]TemplateContext
|
||||
cache sync.Map
|
||||
}
|
||||
|
||||
func NewTemplateRegistry(routes fs.FS) *TemplateRegistry {
|
||||
return &TemplateRegistry{
|
||||
routesFS: routes,
|
||||
}
|
||||
}
|
||||
|
||||
// INFO: This returns a new TemplateRegistry with the new fs added to the existing fs,
|
||||
// merging with the existing FS, possibly overwriting existing files.
|
||||
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()
|
||||
helpers.Assert(err, "Error loading templates. Exiting.")
|
||||
})
|
||||
return nil
|
||||
}
|
||||
|
||||
// TODO: Throw errors
|
||||
// TODO: what if there is no template in the directory above?
|
||||
// What if a certain path is or should uncallable since it has no index or body?
|
||||
func (r *TemplateRegistry) load() error {
|
||||
templates := make(map[string]TemplateContext)
|
||||
fs.WalkDir(r.routesFS, ".", func(path string, d fs.DirEntry, err error) error {
|
||||
if !d.IsDir() {
|
||||
return nil
|
||||
}
|
||||
|
||||
url := FSPathToPath(path)
|
||||
tc := NewTemplateContext(url)
|
||||
|
||||
if path != "." {
|
||||
pathelem := strings.Split(path, string(os.PathSeparator))
|
||||
pathabove := strings.Join(pathelem[:len(pathelem)-1], string(os.PathSeparator))
|
||||
pathabove = FSPathToPath(pathabove)
|
||||
|
||||
global, ok := templates[pathabove]
|
||||
if ok {
|
||||
tc.SetGlobals(global.Globals())
|
||||
}
|
||||
}
|
||||
|
||||
tc.Parse(r.routesFS)
|
||||
|
||||
templates[url] = tc
|
||||
|
||||
return nil
|
||||
})
|
||||
|
||||
r.templates = templates
|
||||
return nil
|
||||
}
|
||||
|
||||
// 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, funcmap *template.FuncMap) error {
|
||||
temp, ok := r.cache.Load(path)
|
||||
if !ok {
|
||||
r.Load()
|
||||
tc, ok := r.templates[path]
|
||||
if !ok {
|
||||
return NewError(NoTemplateError, path)
|
||||
}
|
||||
|
||||
template, err := tc.Template(r.routesFS, funcmap)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
r.cache.Store(path, template)
|
||||
|
||||
return r.Add(path, t, funcmap)
|
||||
}
|
||||
|
||||
casted := temp.(*template.Template)
|
||||
for _, st := range casted.Templates() {
|
||||
_, err := t.AddParseTree(st.Name(), st.Tree)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// TODO: get for a specific component
|
||||
func (r *TemplateRegistry) Get(path string) error {
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user