rebuilt template lookup to use a configured template directory before the builtins
This commit is contained in:
@@ -21,6 +21,9 @@ type AmConfig struct {
|
|||||||
Site struct {
|
Site struct {
|
||||||
Title string `yaml:"title"`
|
Title string `yaml:"title"`
|
||||||
} `yaml:"site"`
|
} `yaml:"site"`
|
||||||
|
Rendering struct {
|
||||||
|
TemplateDir string `yaml:"templatedir"`
|
||||||
|
} `yaml:"rendering"`
|
||||||
}
|
}
|
||||||
|
|
||||||
//go:embed default.yaml
|
//go:embed default.yaml
|
||||||
|
|||||||
@@ -8,3 +8,5 @@
|
|||||||
#
|
#
|
||||||
site:
|
site:
|
||||||
title: "Amsterdam Web Communities System"
|
title: "Amsterdam Web Communities System"
|
||||||
|
rendering:
|
||||||
|
templatedir: custom_templates
|
||||||
|
|||||||
@@ -35,6 +35,7 @@ func setupEcho() *echo.Echo {
|
|||||||
|
|
||||||
// main is Ye Olde Main Function.
|
// main is Ye Olde Main Function.
|
||||||
func main() {
|
func main() {
|
||||||
|
ui.SetupTemplates()
|
||||||
e := setupEcho()
|
e := setupEcho()
|
||||||
|
|
||||||
e.Logger.Fatal(e.Start(":1323"))
|
e.Logger.Fatal(e.Start(":1323"))
|
||||||
|
|||||||
+2
-1
@@ -15,6 +15,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"mime"
|
"mime"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -44,7 +45,7 @@ func AmServeImage(ctxt AmContext) (string, any, error) {
|
|||||||
var err error = nil
|
var err error = nil
|
||||||
if len(components) == 4 && components[2] == "builtin/" {
|
if len(components) == 4 && components[2] == "builtin/" {
|
||||||
var b []byte
|
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 {
|
if err == nil {
|
||||||
ctxt.SetOutputType(mimeTypeFromFilename(components[3]))
|
ctxt.SetOutputType(mimeTypeFromFilename(components[3]))
|
||||||
return "bytes", b, nil
|
return "bytes", b, nil
|
||||||
|
|||||||
+11
-38
@@ -12,57 +12,30 @@ package ui
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"embed"
|
"embed"
|
||||||
"fmt"
|
|
||||||
"io"
|
"io"
|
||||||
|
|
||||||
"git.erbosoft.com/amy/amsterdam/config"
|
"git.erbosoft.com/amy/amsterdam/config"
|
||||||
"github.com/CloudyKit/jet/v6"
|
"github.com/CloudyKit/jet/v6"
|
||||||
|
"github.com/CloudyKit/jet/v6/loaders/embedfs"
|
||||||
|
"github.com/CloudyKit/jet/v6/loaders/multi"
|
||||||
"github.com/labstack/echo/v4"
|
"github.com/labstack/echo/v4"
|
||||||
)
|
)
|
||||||
|
|
||||||
//go:embed views/*
|
//go:embed views/*
|
||||||
var static_views embed.FS
|
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.
|
// views is the main Jet template repository.
|
||||||
var views = jet.NewSet(
|
var views *jet.Set
|
||||||
&EmbeddedLoader{efs: static_views, prefix: "views"},
|
|
||||||
|
// 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),
|
jet.DevelopmentMode(true),
|
||||||
)
|
)
|
||||||
|
|
||||||
// init adds additional configuration for the views object.
|
|
||||||
func init() {
|
|
||||||
views.AddGlobal("GlobalConfig", config.GlobalConfig)
|
views.AddGlobal("GlobalConfig", config.GlobalConfig)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user