fix: set base.html as root template name and use ExecuteTemplate
All checks were successful
CI / lint (push) Successful in 19s
CI / test (push) Successful in 20s
CI / build (push) Successful in 20s
CI / docker (push) Successful in 1m11s

template.New("base.html") makes base.html the root template instead of ""
This commit is contained in:
2026-06-04 17:30:46 +03:00
parent 3f2fe0043a
commit c22c860ec5

View File

@@ -65,7 +65,8 @@ func (h *Handler) profilePage(w http.ResponseWriter, r *http.Request) {
h.render(w, "profile.html", pageData{Title: "Профиль"})
}
// render executes the named page template.
// render executes the "base.html" template which {{template "content" .}}
// pulls in the per-page content block.
func (h *Handler) render(w http.ResponseWriter, page string, data pageData) {
w.Header().Set("Content-Type", "text/html; charset=utf-8")
tmpl, ok := h.templates[page]
@@ -74,7 +75,7 @@ func (h *Handler) render(w http.ResponseWriter, page string, data pageData) {
http.Error(w, "Template not found", http.StatusInternalServerError)
return
}
if err := tmpl.Execute(w, data); err != nil {
if err := tmpl.ExecuteTemplate(w, "base.html", data); err != nil {
log.Printf("Template error (%s): %v", page, err)
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
}
@@ -82,14 +83,14 @@ func (h *Handler) render(w http.ResponseWriter, page string, data pageData) {
// ── Template parsing ───────────────────────────────────────────
// parseTemplates parses each page template individually by cloning the base
// layout and adding the specific page content. This avoids the issue where
// parseTemplates parses each page template individually by combining the base
// layout with 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() {
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)
tmpl, err := template.New("base.html").Funcs(template.FuncMap{}).ParseFS(templateFS, "html/base.html", "html/"+page)
if err != nil {
log.Printf("Template parse error (%s): %v", page, err)
continue