refactor: migrate from templ to html/template
This commit is contained in:
17
templates/base.gohtml
Normal file
17
templates/base.gohtml
Normal file
@@ -0,0 +1,17 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>{{template "title" .}} - MAL</title>
|
||||
<link rel="stylesheet" href="/static/style.css">
|
||||
</head>
|
||||
<body class="bg-gray-900 text-white">
|
||||
<header class="p-4">
|
||||
<h1 class="text-2xl font-bold">MAL</h1>
|
||||
</header>
|
||||
<main class="container mx-auto px-4 py-8">
|
||||
{{template "content" .}}
|
||||
</main>
|
||||
</body>
|
||||
</html>
|
||||
9
templates/components/anime_card.gohtml
Normal file
9
templates/components/anime_card.gohtml
Normal file
@@ -0,0 +1,9 @@
|
||||
{{define "anime_card"}}
|
||||
<div class="bg-gray-800 rounded-lg overflow-hidden shadow-lg">
|
||||
<img src="{{.ImageURL}}" alt="{{.DisplayTitle}}" class="w-full h-48 object-cover">
|
||||
<div class="p-4">
|
||||
<h3 class="text-lg font-semibold mb-2">{{.DisplayTitle}}</h3>
|
||||
<p class="text-sm text-gray-400">{{.Type}}</p>
|
||||
</div>
|
||||
</div>
|
||||
{{end}}
|
||||
9
templates/index.gohtml
Normal file
9
templates/index.gohtml
Normal file
@@ -0,0 +1,9 @@
|
||||
{{define "title"}}Hello World{{end}}
|
||||
{{define "content"}}
|
||||
<h2 class="text-3xl font-bold mb-8 text-center">Hello World</h2>
|
||||
<div class="grid grid-cols-2 md:grid-cols-4 gap-4">
|
||||
{{range .Animes}}
|
||||
{{template "anime_card" .}}
|
||||
{{end}}
|
||||
</div>
|
||||
{{end}}
|
||||
22
templates/login.gohtml
Normal file
22
templates/login.gohtml
Normal file
@@ -0,0 +1,22 @@
|
||||
{{define "title"}}Login{{end}}
|
||||
{{define "content"}}
|
||||
<div class="max-w-md mx-auto mt-20">
|
||||
<h2 class="text-3xl font-bold mb-8 text-center">Login</h2>
|
||||
{{if .Error}}
|
||||
<div class="bg-red-500 text-white p-3 rounded mb-4">{{.Error}}</div>
|
||||
{{end}}
|
||||
<form method="POST" action="/login" class="space-y-4">
|
||||
<div>
|
||||
<label for="username" class="block text-sm font-medium mb-2">Username</label>
|
||||
<input type="text" id="username" name="username" value="{{.Username}}" class="w-full p-2 rounded bg-gray-800 border border-gray-700 focus:border-blue-500 outline-none">
|
||||
</div>
|
||||
<div>
|
||||
<label for="password" class="block text-sm font-medium mb-2">Password</label>
|
||||
<input type="password" id="password" name="password" class="w-full p-2 rounded bg-gray-800 border border-gray-700 focus:border-blue-500 outline-none">
|
||||
</div>
|
||||
<button type="submit" class="w-full bg-blue-600 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded">
|
||||
Login
|
||||
</button>
|
||||
</form>
|
||||
</div>
|
||||
{{end}}
|
||||
7
templates/not_found.gohtml
Normal file
7
templates/not_found.gohtml
Normal file
@@ -0,0 +1,7 @@
|
||||
{{define "title"}}Not Found{{end}}
|
||||
{{define "content"}}
|
||||
<div class="text-center py-20">
|
||||
<h2 class="text-4xl font-bold mb-4">404</h2>
|
||||
<p class="text-xl text-gray-400">Page not found</p>
|
||||
</div>
|
||||
{{end}}
|
||||
77
templates/renderer.go
Normal file
77
templates/renderer.go
Normal file
@@ -0,0 +1,77 @@
|
||||
package templates
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"html/template"
|
||||
"io"
|
||||
"log"
|
||||
"path/filepath"
|
||||
"sync"
|
||||
)
|
||||
|
||||
var (
|
||||
once sync.Once
|
||||
renderer *Renderer
|
||||
)
|
||||
|
||||
type Renderer struct {
|
||||
templates map[string]*template.Template
|
||||
}
|
||||
|
||||
func GetRenderer() *Renderer {
|
||||
once.Do(func() {
|
||||
renderer = &Renderer{
|
||||
templates: make(map[string]*template.Template),
|
||||
}
|
||||
|
||||
funcs := template.FuncMap{
|
||||
"dict": func(values ...any) map[string]any {
|
||||
m := make(map[string]any)
|
||||
for i := 0; i < len(values)-1; i += 2 {
|
||||
key, ok := values[i].(string)
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
m[key] = values[i+1]
|
||||
}
|
||||
return m
|
||||
},
|
||||
}
|
||||
|
||||
pages, err := filepath.Glob(filepath.Join(".", "templates", "*.gohtml"))
|
||||
if err != nil {
|
||||
log.Fatalf("failed to glob page templates: %v", err)
|
||||
}
|
||||
|
||||
components, err := filepath.Glob(filepath.Join(".", "templates", "components", "*.gohtml"))
|
||||
if err != nil {
|
||||
log.Fatalf("failed to glob component templates: %v", err)
|
||||
}
|
||||
|
||||
for _, page := range pages {
|
||||
name := filepath.Base(page)
|
||||
if name == "base.gohtml" {
|
||||
continue
|
||||
}
|
||||
|
||||
tmpl := template.New(name).Funcs(funcs)
|
||||
tmpl = template.Must(tmpl.ParseFiles(filepath.Join(".", "templates", "base.gohtml")))
|
||||
tmpl = template.Must(tmpl.ParseFiles(page))
|
||||
if len(components) > 0 {
|
||||
tmpl = template.Must(tmpl.ParseFiles(components...))
|
||||
}
|
||||
|
||||
renderer.templates[name] = tmpl
|
||||
log.Printf("Loaded page template: %s", name)
|
||||
}
|
||||
})
|
||||
return renderer
|
||||
}
|
||||
|
||||
func (r *Renderer) ExecuteTemplate(wr io.Writer, name string, data any) error {
|
||||
tmpl, ok := r.templates[name]
|
||||
if !ok {
|
||||
return fmt.Errorf("template %s not found", name)
|
||||
}
|
||||
return tmpl.ExecuteTemplate(wr, "base.gohtml", data)
|
||||
}
|
||||
Reference in New Issue
Block a user