summary refs log tree commit diff
path: root/templates.go
diff options
context:
space:
mode:
authorMelonai <einebeere@gmail.com>2021-07-26 21:52:58 +0200
committerMelonai <einebeere@gmail.com>2021-07-26 21:53:36 +0200
commit0ac82c133b78a14239beb9034380c44d95d4bad3 (patch)
tree3ba65b4e3e5ceafaf2180ea697cc7794a07da669 /templates.go
parent7819a23171145e8a626e8357e88446817c8785dc (diff)
downloadportgate-0ac82c133b78a14239beb9034380c44d95d4bad3.tar.zst
portgate-0ac82c133b78a14239beb9034380c44d95d4bad3.zip
Portgate pages and templates
Diffstat (limited to 'templates.go')
-rw-r--r--templates.go55
1 files changed, 55 insertions, 0 deletions
diff --git a/templates.go b/templates.go
new file mode 100644
index 0000000..25cb480
--- /dev/null
+++ b/templates.go
@@ -0,0 +1,55 @@
+package portgate
+
+import (
+	"errors"
+	"html/template"
+	"io"
+	"io/fs"
+	"path/filepath"
+	"strings"
+)
+
+// Templates houses all the Portgate page templates and can render them with correct styling.
+type Templates struct {
+	templateMap map[string]*template.Template
+}
+
+// LoadAllTemplates loads all Portgate templates from ./assets/templates/ and bundles them
+// into a single Templates instance.
+func LoadAllTemplates() (Templates, error) {
+	templateMap := make(map[string]*template.Template)
+
+	// We walk through every file in the templates folder.
+	err := filepath.Walk("./assets/templates", func(path string, info fs.FileInfo, err error) error {
+		// We only care about files which are actual templates and are not of special use (like "_base")
+		if !strings.HasPrefix(info.Name(), "_") && strings.HasSuffix(path, ".template.html") {
+			// We bundle the templates together with the base template so that we can render them together later.
+			t, err := template.ParseFiles("./assets/templates/_base.template.html", path)
+			if err == nil {
+				// We keep the parsed template in the templateMap by it's filename.
+				templateMap[info.Name()] = t
+			}
+		}
+
+		return err
+	})
+
+	if err != nil {
+		return Templates{}, err
+	}
+
+	return Templates{
+		templateMap: templateMap,
+	}, nil
+}
+
+// ExecuteTemplate executes a single template with the given name and data and writes it to the given
+// io.Writer, which is usually fasthttp.RequestCtx.
+func (templates *Templates) ExecuteTemplate(w io.Writer, name string, data interface{}) error {
+	t := templates.templateMap[name]
+	if t == nil {
+		return errors.New("Unknown template name: " + name)
+	}
+
+	return t.ExecuteTemplate(w, "_base.template.html", data)
+}