mirror of
https://github.com/Theodor-Springmann-Stiftung/kgpz_web.git
synced 2025-10-28 16:45:32 +00:00
added 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"
|
||||
141
templating/context.go
Normal file
141
templating/context.go
Normal file
@@ -0,0 +1,141 @@
|
||||
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) (*template.Template, error) {
|
||||
t, err := readTemplates(fsys, nil, c.globals)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
t, err = readTemplates(fsys, t, c.locals)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return t, nil
|
||||
}
|
||||
|
||||
func readTemplates(fsys fs.FS, t *template.Template, paths map[string]string) (*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))
|
||||
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
|
||||
}
|
||||
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
|
||||
}
|
||||
113
templating/layout_registry.go
Normal file
113
templating/layout_registry.go
Normal file
@@ -0,0 +1,113 @@
|
||||
package templating
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"html/template"
|
||||
"io/fs"
|
||||
"sync"
|
||||
|
||||
"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
|
||||
funcs template.FuncMap
|
||||
}
|
||||
|
||||
func NewLayoutRegistry(routes fs.FS) *LayoutRegistry {
|
||||
return &LayoutRegistry{
|
||||
layoutsFS: routes,
|
||||
funcs: template.FuncMap{
|
||||
"safe": func(s string) template.HTML {
|
||||
return template.HTML(s)
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
// 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))
|
||||
}
|
||||
|
||||
// TODO: Funcs are not used in executing the templates yet
|
||||
func (r *LayoutRegistry) RegisterFuncs(funcs template.FuncMap) {
|
||||
for k, v := range funcs {
|
||||
r.funcs[k] = v
|
||||
}
|
||||
}
|
||||
|
||||
func (r *LayoutRegistry) Parse() 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) (*template.Template, error) {
|
||||
cached, ok := r.cache.Load(name)
|
||||
if ok {
|
||||
return cached.(*template.Template), nil
|
||||
}
|
||||
|
||||
// TODO: What todo on errors?
|
||||
r.once.Do(func() {
|
||||
err := r.Parse()
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
panic(-1)
|
||||
}
|
||||
})
|
||||
|
||||
context, ok := r.layouts[name]
|
||||
if !ok {
|
||||
return nil, NewError(NoTemplateError, name)
|
||||
}
|
||||
|
||||
t, err := context.Template(r.layoutsFS)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
r.cache.Store(name, t)
|
||||
|
||||
return t, nil
|
||||
}
|
||||
|
||||
func (r *LayoutRegistry) Default() (*template.Template, error) {
|
||||
return r.Layout(DEFAULT_LAYOUT_NAME)
|
||||
}
|
||||
127
templating/template_registry.go
Normal file
127
templating/template_registry.go
Normal file
@@ -0,0 +1,127 @@
|
||||
package templating
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"html/template"
|
||||
"io/fs"
|
||||
"os"
|
||||
"strings"
|
||||
"sync"
|
||||
|
||||
"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 Parse() and never changed.
|
||||
// Parse() is called only once in a thread-safe manner
|
||||
templates map[string]TemplateContext
|
||||
cache sync.Map
|
||||
funcs template.FuncMap
|
||||
}
|
||||
|
||||
func NewTemplateRegistry(routes fs.FS) *TemplateRegistry {
|
||||
return &TemplateRegistry{
|
||||
routesFS: routes,
|
||||
funcs: template.FuncMap{
|
||||
"safe": func(s string) template.HTML {
|
||||
return template.HTML(s)
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
// 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) RegisterFuncs(funcs template.FuncMap) {
|
||||
for k, v := range funcs {
|
||||
r.funcs[k] = v
|
||||
}
|
||||
}
|
||||
|
||||
// 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) Parse() error {
|
||||
// INFO: Parse setrs r.templates, which is why you need to make sure to call Parse() once
|
||||
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) error {
|
||||
temp, ok := r.cache.Load(path)
|
||||
if !ok {
|
||||
// INFO: What todo on errors?
|
||||
r.once.Do(func() {
|
||||
err := r.Parse()
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
panic(-1)
|
||||
}
|
||||
})
|
||||
tc, ok := r.templates[path]
|
||||
if !ok {
|
||||
return NewError(NoTemplateError, path)
|
||||
}
|
||||
|
||||
template, err := tc.Template(r.routesFS)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
r.cache.Store(path, template)
|
||||
|
||||
return r.Add(path, t)
|
||||
}
|
||||
|
||||
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