diff --git a/config/config.go b/config/config.go index 3587c5a..c938bc3 100644 --- a/config/config.go +++ b/config/config.go @@ -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 diff --git a/config/default.yaml b/config/default.yaml index 07f7d4a..a793fe2 100644 --- a/config/default.yaml +++ b/config/default.yaml @@ -8,3 +8,5 @@ # site: title: "Amsterdam Web Communities System" +rendering: + templatedir: custom_templates diff --git a/main.go b/main.go index c8dce1b..a7e4ef5 100644 --- a/main.go +++ b/main.go @@ -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")) diff --git a/ui/images.go b/ui/images.go index 137001f..70273d3 100644 --- a/ui/images.go +++ b/ui/images.go @@ -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 diff --git a/ui/templates.go b/ui/templates.go index 4ccede7..62d9314 100644 --- a/ui/templates.go +++ b/ui/templates.go @@ -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) }