summaryrefslogtreecommitdiff
path: root/templ
diff options
context:
space:
mode:
authorRené Neumann <necoro@necoro.eu>2024-02-10 23:24:37 +0100
committerRené Neumann <necoro@necoro.eu>2024-02-10 23:24:37 +0100
commit91d14e414fc9c451879e834d7866ad765084a836 (patch)
tree1fa8a757feda3d2c721aa9382f32a69109626ea7 /templ
parent2d90d4e00a111ddf22440334d99323fe0d8216be (diff)
downloadgosten-91d14e414fc9c451879e834d7866ad765084a836.tar.gz
gosten-91d14e414fc9c451879e834d7866ad765084a836.tar.bz2
gosten-91d14e414fc9c451879e834d7866ad765084a836.zip
Add templating
Diffstat (limited to 'templ')
-rw-r--r--templ/base.tpl16
-rw-r--r--templ/index.tpl3
-rw-r--r--templ/template.go45
3 files changed, 64 insertions, 0 deletions
diff --git a/templ/base.tpl b/templ/base.tpl
new file mode 100644
index 0000000..e88d735
--- /dev/null
+++ b/templ/base.tpl
@@ -0,0 +1,16 @@
+<!DOCTYPE html>
+<html lang="de">
+<head>
+ <meta charset="UTF-8">
+ <title>{{block "title" .}}Kosten{{end}}</title>
+ <link rel="stylesheet" href="./style.css">
+</head>
+<body>
+{{block "body" .}}
+ Dummy Text
+{{end}}
+{{block "js" .}}
+ <script src="./index.js"></script>
+{{end}}
+</body>
+</html> \ No newline at end of file
diff --git a/templ/index.tpl b/templ/index.tpl
new file mode 100644
index 0000000..2c52810
--- /dev/null
+++ b/templ/index.tpl
@@ -0,0 +1,3 @@
+{{define "body"}}
+ Das ist die Basis
+{{end}} \ No newline at end of file
diff --git a/templ/template.go b/templ/template.go
new file mode 100644
index 0000000..8fed965
--- /dev/null
+++ b/templ/template.go
@@ -0,0 +1,45 @@
+package templ
+
+import (
+ "embed"
+ "html/template"
+ "sync"
+)
+
+//go:embed *.tpl
+var fs embed.FS
+
+var templates = make(map[string]*template.Template)
+var muTpl sync.RWMutex
+
+var baseTpl *template.Template
+
+func init() {
+ baseTpl = template.Must(template.ParseFS(fs, "base.tpl"))
+}
+
+func Lookup(name string) *template.Template {
+ muTpl.RLock()
+ tpl := templates[name]
+ muTpl.RUnlock()
+
+ if tpl == nil {
+ return parse(name)
+ }
+ return tpl
+}
+
+func parse(name string) *template.Template {
+ muTpl.Lock()
+ defer muTpl.Unlock()
+
+ if tpl := templates[name]; tpl != nil {
+ // might've been created by another goroutine
+ return tpl
+ }
+
+ b := template.Must(baseTpl.Clone())
+ t := template.Must(b.ParseFS(fs, name+".tpl"))
+ templates[name] = t
+ return t
+}