fix: set base.html as root template name and use ExecuteTemplate
template.New("base.html") makes base.html the root template instead of ""
This commit is contained in:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user