rebuilt template lookup to use a configured template directory before the builtins

This commit is contained in:
2025-09-17 12:23:33 -06:00
parent 9e5a24e03d
commit 9345e95b12
5 changed files with 20 additions and 40 deletions
+3
View File
@@ -21,6 +21,9 @@ type AmConfig struct {
Site struct {
Title string `yaml:"title"`
} `yaml:"site"`
Rendering struct {
TemplateDir string `yaml:"templatedir"`
} `yaml:"rendering"`
}
//go:embed default.yaml
+2
View File
@@ -8,3 +8,5 @@
#
site:
title: "Amsterdam Web Communities System"
rendering:
templatedir: custom_templates
+1
View File
@@ -35,6 +35,7 @@ func setupEcho() *echo.Echo {
// main is Ye Olde Main Function.
func main() {
ui.SetupTemplates()
e := setupEcho()
e.Logger.Fatal(e.Start(":1323"))
+2 -1
View File
@@ -15,6 +15,7 @@ import (
"fmt"
"mime"
"net/http"
"path/filepath"
"strings"
)
@@ -44,7 +45,7 @@ func AmServeImage(ctxt AmContext) (string, any, error) {
var err error = nil
if len(components) == 4 && components[2] == "builtin/" {
var b []byte
b, err = static_images.ReadFile(fmt.Sprintf("static_images/%s", components[3]))
b, err = static_images.ReadFile(filepath.Join("static_images", components[3]))
if err == nil {
ctxt.SetOutputType(mimeTypeFromFilename(components[3]))
return "bytes", b, nil
+12 -39
View File
@@ -12,57 +12,30 @@ package ui
import (
"embed"
"fmt"
"io"
"git.erbosoft.com/amy/amsterdam/config"
"github.com/CloudyKit/jet/v6"
"github.com/CloudyKit/jet/v6/loaders/embedfs"
"github.com/CloudyKit/jet/v6/loaders/multi"
"github.com/labstack/echo/v4"
)
//go:embed views/*
var static_views embed.FS
// EmbeddedLoader is our implementation of Loader that references an embedded filesystem.
type EmbeddedLoader struct {
efs embed.FS
prefix string
}
/* Exists (implements Loader) tests if a particular template exists.
* Parameters:
* templatePath - Path of the template to be tested.
* Returns:
* true if the template exists, false if not.
*/
func (l *EmbeddedLoader) Exists(templatePath string) bool {
file, err := l.efs.Open(fmt.Sprintf("%s%s", l.prefix, templatePath))
if err == nil {
file.Close()
return true
}
return false
}
/* Open (implements Loader) opens a template file.
* Parameters:
* templatePath - Path of the template to open.
* Returns:
* Handle to the opened template file
*. Standard Go error status.
*/
func (l *EmbeddedLoader) Open(templatePath string) (io.ReadCloser, error) {
return l.efs.Open((fmt.Sprintf("%s%s", l.prefix, templatePath)))
}
// views is the main Jet template repository.
var views = jet.NewSet(
&EmbeddedLoader{efs: static_views, prefix: "views"},
jet.DevelopmentMode(true),
)
var views *jet.Set
// init adds additional configuration for the views object.
func init() {
// SetupTemplates is called to set up the template renderer after the configuration is loaded.
func SetupTemplates() {
views = jet.NewSet(
multi.NewLoader(
jet.NewOSFileSystemLoader(config.GlobalConfig.Rendering.TemplateDir),
embedfs.NewLoader("views/", static_views),
),
jet.DevelopmentMode(true),
)
views.AddGlobal("GlobalConfig", config.GlobalConfig)
}