diff options
author | René 'Necoro' Neumann <necoro@necoro.eu> | 2024-10-17 16:37:23 +0200 |
---|---|---|
committer | René 'Necoro' Neumann <necoro@necoro.eu> | 2024-10-17 16:37:23 +0200 |
commit | b2447bc967df37b31282a97e32c581954bb8bcc9 (patch) | |
tree | 39758d1121fae6dc1d27e8a45035690421900d6c /templ | |
parent | 789d21034e526a03d3e91d5d284a4888be938340 (diff) | |
download | gosten-b2447bc967df37b31282a97e32c581954bb8bcc9.tar.gz gosten-b2447bc967df37b31282a97e32c581954bb8bcc9.tar.bz2 gosten-b2447bc967df37b31282a97e32c581954bb8bcc9.zip |
Move from html/template to templ
Diffstat (limited to '')
-rw-r--r-- | pages/login.templ (renamed from templ/login.tpl) | 16 | ||||
-rw-r--r-- | templ/404.tpl | 10 | ||||
-rw-r--r-- | templ/base.tpl | 20 | ||||
-rw-r--r-- | templ/categories.tpl | 9 | ||||
-rw-r--r-- | templ/content.tpl | 36 | ||||
-rw-r--r-- | templ/form.tpl | 22 | ||||
-rw-r--r-- | templ/index.tpl | 3 | ||||
-rw-r--r-- | templ/navlinks.tpl | 8 | ||||
-rw-r--r-- | templ/recur.tpl | 9 | ||||
-rw-r--r-- | templ/template.go | 73 |
10 files changed, 12 insertions, 194 deletions
diff --git a/templ/login.tpl b/pages/login.templ index 7a9a049..fed611e 100644 --- a/templ/login.tpl +++ b/pages/login.templ @@ -1,10 +1,18 @@ -{{define "body"}} +package pages + +import ( + "gosten/form" +) + +templ login(u user) { + @base() { <main class="d-flex align-items-center min-vh-100"> <form action="/login" method="post" class="container mx-auto" style="max-width: 440px;"> <img src="/static/euro-money.svg" width="96" height="96" class="mb-4"/> - {{inputs_and_errors_for . .Errors}} - {{.CsrfField}} + @form.Form(u, u.Errors) + @u.Csrf() <button class="btn btn-primary w-100" type="submit">Log In!</button> </form> </main> -{{end}}
\ No newline at end of file + } +}
\ No newline at end of file diff --git a/templ/404.tpl b/templ/404.tpl deleted file mode 100644 index b0a3658..0000000 --- a/templ/404.tpl +++ /dev/null @@ -1,10 +0,0 @@ -{{define "main"}} - <div class="alert alert-danger d-flex align-items-center" role="alert"> - <svg class="me-2" role="img" xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" viewBox="0 0 16 16"> - <path d="M9.05.435c-.58-.58-1.52-.58-2.1 0L.436 6.95c-.58.58-.58 1.519 0 2.098l6.516 6.516c.58.58 1.519.58 2.098 0l6.516-6.516c.58-.58.58-1.519 0-2.098zM8 4c.535 0 .954.462.9.995l-.35 3.507a.552.552 0 0 1-1.1 0L7.1 4.995A.905.905 0 0 1 8 4m.002 6a1 1 0 1 1 0 2 1 1 0 0 1 0-2"/> - </svg> - <div> - Seite <span class="fst-italic">{{.}}</span> nicht gefunden! - </div> - </div> -{{end}}
\ No newline at end of file diff --git a/templ/base.tpl b/templ/base.tpl deleted file mode 100644 index 7e42630..0000000 --- a/templ/base.tpl +++ /dev/null @@ -1,20 +0,0 @@ -<!DOCTYPE html> -<html lang="de"> -<head> - <meta charset="UTF-8"> - <meta name="viewport" content="width=device-width, initial-scale=1"> - <link rel="icon" type="image/svg+xml" href="/static/favicon.svg" sizes="16x16 32x32 48x48"> - <title>{{block "title" .}}Kosten{{end}}</title> - <link href="/static/custom.css" rel="stylesheet"> - <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-QWTKZyjpPEjISv5WaRU9OFeRpok6YctnYmDr5pNlyT2bRjXh0JMhjY6hW+ALEwIH" crossorigin="anonymous"> - <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js" integrity="sha384-YvpcrYf0tY3lHB60NNkmXc5s9fDVZLESaAA55NDzOxhy9GkcIdslK1eN7N6jIeHz" crossorigin="anonymous"></script> - {{block "head" .}}{{end}} -</head> -<body> -{{block "body" .}} - Dummy Text -{{end}} -{{block "js" .}} -{{end}} -</body> -</html>
\ No newline at end of file diff --git a/templ/categories.tpl b/templ/categories.tpl deleted file mode 100644 index 662e1e1..0000000 --- a/templ/categories.tpl +++ /dev/null @@ -1,9 +0,0 @@ -{{define "main"}} -<ul class="list-group"> - {{range .}} - <li class="list-group-item"> - {{.Name}} - </li> - {{end}} -</ul> -{{end}}
\ No newline at end of file diff --git a/templ/content.tpl b/templ/content.tpl deleted file mode 100644 index 47a7909..0000000 --- a/templ/content.tpl +++ /dev/null @@ -1,36 +0,0 @@ -{{define "body"}} - {{/* different inline svgs */}} - <svg xmlns="http://www.w3.org/2000/svg" class="d-none"> - <symbol id="search" viewBox="0 0 16 16"> - <path d="M11.742 10.344a6.5 6.5 0 1 0-1.397 1.398h-.001c.03.04.062.078.098.115l3.85 3.85a1 1 0 0 0 1.415-1.414l-3.85-3.85a1.007 1.007 0 0 0-.115-.1zM12 6.5a5.5 5.5 0 1 1-11 0 5.5 5.5 0 0 1 11 0z"></path> - </symbol> - </svg> - - {{/* The Navbar */}} - <nav class="navbar sticky-top bg-dark-subtle navbar-expand-lg shadow mb-2"> - <div class="container-fluid" > - <a class="navbar-brand" href="/"> - <img src="/static/euro-money.svg" alt="Logo" width="32" height="32" class="d-inline-block"/> Kosten - </a> - <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation"> - <span class="navbar-toggler-icon"></span> - </button> - <div class="collapse navbar-collapse" id="navbarNav"> - <ul class="navbar-nav nav-underline me-auto"> - {{template "navlinks"}} - </ul> - <form class="d-flex" role="search"> - <input class="form-control me-2" type="search" placeholder="Search" aria-label="Search"> - <button class="btn btn-outline-dark" type="submit"><svg class="svg"><use xlink:href="#search"></use></svg></button> - </form> - </div> - </div> - </nav> - - {{/* The Main Content */}} - <main class="container-lg"> - {{block "main" .}} - Dummy Content - {{end}} - </main> -{{end}}
\ No newline at end of file diff --git a/templ/form.tpl b/templ/form.tpl deleted file mode 100644 index 4d20557..0000000 --- a/templ/form.tpl +++ /dev/null @@ -1,22 +0,0 @@ -{{define "formItem"}} - {{- $cb := eq .Type "checkbox" -}} - <div class="{{if $cb}}form-check form-switch{{else}}form-floating{{end}} mb-3"> - <input - {{with .ID}}id="{{.}}"{{end}} - type="{{.Type}}" - name="{{.Name}}" - placeholder="{{.Placeholder}}" - {{with .Value}}value="{{.}}"{{end}} - {{with .Class}}class="{{.}}"{{end}} - class="{{if $cb}}form-check-input{{else}}form-control{{end}}" - {{range .Options}} {{.}} {{end}} - > - <label {{with .ID}}for="{{.}}"{{end}} - {{- if $cb}}class="form-check-label"{{end}}>{{.Label}}</label> - - {{range errors}} - <p style="color:red">{{.}}</p> - {{end}} - {{with .Footer}}<p>{{.}}</p>{{end}} - </div> -{{end}}
\ No newline at end of file diff --git a/templ/index.tpl b/templ/index.tpl deleted file mode 100644 index 76016bb..0000000 --- a/templ/index.tpl +++ /dev/null @@ -1,3 +0,0 @@ -{{define "main"}} - Logged in with user: {{.}} -{{end}}
\ No newline at end of file diff --git a/templ/navlinks.tpl b/templ/navlinks.tpl deleted file mode 100644 index cdd54d7..0000000 --- a/templ/navlinks.tpl +++ /dev/null @@ -1,8 +0,0 @@ -{{define "navlinks"}} - <li class="nav-item"> - <a class="nav-link" href="/categories">Kategorien</a> - </li> - <li class="nav-item"> - <a class="nav-link" href="/recur">Regelmäßig</a> - </li> -{{end}}
\ No newline at end of file diff --git a/templ/recur.tpl b/templ/recur.tpl deleted file mode 100644 index 316b768..0000000 --- a/templ/recur.tpl +++ /dev/null @@ -1,9 +0,0 @@ -{{define "main"}} -<ul class="list-group"> - {{range .}} - <li class="list-group-item"> - {{.Description.String}} - </li> - {{end}} -</ul> -{{end}}
\ No newline at end of file diff --git a/templ/template.go b/templ/template.go deleted file mode 100644 index 9cefadc..0000000 --- a/templ/template.go +++ /dev/null @@ -1,73 +0,0 @@ -package templ - -import ( - "embed" - "html/template" - "io/fs" - "os" - "sync" - - "gosten/form" -) - -//go:embed *.tpl -var fsEmbed embed.FS - -var templates = make(map[string]*template.Template) -var muTpl sync.RWMutex - -var baseTpl *template.Template - -var isLive = sync.OnceValue(checkLive) - -func init() { - loadBase(fsEmbed) -} - -func checkLive() bool { - return os.Getenv("GOSTEN_LIVE") != "" -} - -func loadBase(fs fs.FS) { - baseTpl = template.Must(template.New("base.tpl"). - Funcs(form.ParsingFuncMap()). - ParseFS(fs, "base.tpl", "form.tpl", "navlinks.tpl", "content.tpl")) - baseTpl.Funcs(form.FuncMap(baseTpl.Lookup("formItem"))) -} - -func Lookup(name string) *template.Template { - if isLive() { - fs := os.DirFS("templ/") - loadBase(fs) - return getTemplate(name, fs) - } - - 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 - } - - t := getTemplate(name, fsEmbed) - templates[name] = t - return t -} - -func getTemplate(name string, fs fs.FS) *template.Template { - b := template.Must(baseTpl.Clone()) - t := template.Must(b.ParseFS(fs, name+".tpl")) - return t -} |