fix: parse templates individually to prevent content block overwrite
Root cause: ParseFS with wildcard html/*.html caused all {{define "content"}}
blocks to overwrite each other — last alphabetically (register.html) won for
all pages. Now each page is parsed separately with base.html as its own template.
Also fix footer link: GitHub → Gitea project page.
This commit is contained in:
@@ -23,14 +23,14 @@ type pageData struct {
|
||||
|
||||
// Handler serves template-rendered pages.
|
||||
type Handler struct {
|
||||
db *database.DB
|
||||
cfg *config.Config
|
||||
tmpl *template.Template
|
||||
db *database.DB
|
||||
cfg *config.Config
|
||||
templates map[string]*template.Template
|
||||
}
|
||||
|
||||
// NewHandler creates a new templates handler and parses embedded templates.
|
||||
func NewHandler(db *database.DB, cfg *config.Config) *Handler {
|
||||
h := &Handler{db: db, cfg: cfg}
|
||||
h := &Handler{db: db, cfg: cfg, templates: make(map[string]*template.Template)}
|
||||
h.parseTemplates()
|
||||
return h
|
||||
}
|
||||
@@ -65,28 +65,36 @@ func (h *Handler) profilePage(w http.ResponseWriter, r *http.Request) {
|
||||
h.render(w, "profile.html", pageData{Title: "Профиль"})
|
||||
}
|
||||
|
||||
// render executes the base layout template which calls {{template "content" .}}
|
||||
// to inject the named page content.
|
||||
// render executes the named page template.
|
||||
func (h *Handler) render(w http.ResponseWriter, page string, data pageData) {
|
||||
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
||||
if h.tmpl == nil {
|
||||
http.Error(w, "Templates not loaded", http.StatusInternalServerError)
|
||||
tmpl, ok := h.templates[page]
|
||||
if !ok {
|
||||
log.Printf("Template not found: %s", page)
|
||||
http.Error(w, "Template not found", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
if err := h.tmpl.ExecuteTemplate(w, "base.html", data); err != nil {
|
||||
log.Printf("Template error (base.html → %s): %v", page, err)
|
||||
if err := tmpl.Execute(w, data); err != nil {
|
||||
log.Printf("Template error (%s): %v", page, err)
|
||||
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
|
||||
}
|
||||
}
|
||||
|
||||
// ── Template parsing ───────────────────────────────────────────
|
||||
|
||||
// parseTemplates parses each page template individually by cloning the base
|
||||
// layout and adding the specific page content. This avoids the issue where
|
||||
// multiple {{define "content"}} blocks in wildcard-parsed files overwrite
|
||||
// each other (last alphabetically wins).
|
||||
func (h *Handler) parseTemplates() {
|
||||
tmpl, err := template.New("").Funcs(template.FuncMap{}).ParseFS(templateFS, "html/*.html")
|
||||
if err != nil {
|
||||
log.Printf("Template parse error: %v", err)
|
||||
return
|
||||
pages := []string{"index.html", "login.html", "register.html", "profile.html"}
|
||||
for _, page := range pages {
|
||||
tmpl, err := template.New("").Funcs(template.FuncMap{}).ParseFS(templateFS, "html/base.html", "html/"+page)
|
||||
if err != nil {
|
||||
log.Printf("Template parse error (%s): %v", page, err)
|
||||
continue
|
||||
}
|
||||
h.templates[page] = tmpl
|
||||
log.Printf("Loaded template: %s", page)
|
||||
}
|
||||
h.tmpl = tmpl
|
||||
log.Println("Loaded embedded HTML templates")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user