summaryrefslogtreecommitdiffstats
path: root/cmd/web/templates.go
diff options
context:
space:
mode:
authorYulqen <246857+yulqen@users.noreply.github.com>2024-04-18 13:58:11 +0100
committerGitHub <noreply@github.com>2024-04-18 13:58:11 +0100
commit69017cc225e2754466b8444ca42cb1122208425d (patch)
tree6a7ac8d150b95c1fad9229d124a23737abc17964 /cmd/web/templates.go
parent530f08071fc1295fabdaedf9724b8cda48780927 (diff)
parentf08ac7b887e0bae0cb67362eec90a575faec07f1 (diff)
Merge pull request #4 from defencedigital/changes
Changes
Diffstat (limited to '')
-rw-r--r--cmd/web/templates.go48
1 files changed, 48 insertions, 0 deletions
diff --git a/cmd/web/templates.go b/cmd/web/templates.go
new file mode 100644
index 0000000..845fe25
--- /dev/null
+++ b/cmd/web/templates.go
@@ -0,0 +1,48 @@
+package main
+
+import (
+ "html/template"
+ "path/filepath"
+
+ "github.com/defencedigital/ded-web/internal/models"
+)
+
+// TODO: At the moment we are using a struct will have fields for all the data
+// we will need. Not sure whether this is good or bad at the moment.
+type templateData struct {
+ Operations []models.Operation
+ Organisations []models.Organisation
+ Persons []models.Person
+}
+
+func newTemplateCache() (map[string]*template.Template, error) {
+ cache := map[string]*template.Template{}
+
+ // get a slice of filepaths match the pattern for our page templates
+ // e.g. [ui/html/pages/home.tmpl.html ui/html/pages/view.tmpl.html]
+ pages, err := filepath.Glob("./ui/html/pages/*.html")
+ if err != nil {
+ return nil, err
+ }
+
+ // loop through the page filepaths one by one
+ for _, page := range pages {
+ // extract "home.tmp.html" from the full filepath.
+ name := filepath.Base(page)
+
+ files := []string{
+ "./ui/html/base.tmpl.html",
+ "./ui/html/partials/nav.tmpl.html",
+ page,
+ }
+
+ ts, err := template.ParseFiles(files...)
+ if err != nil {
+ return nil, err
+ }
+
+ cache[name] = ts
+ }
+
+ return cache, nil
+}