Echo Golang -Vorlage Problem: Inhalte ändern sich nicht auf verschiedenen RoutenHTML

HTML-Programmierer
Guest
 Echo Golang -Vorlage Problem: Inhalte ändern sich nicht auf verschiedenen Routen

Post by Guest »

Ich benutze das Echo -Framework in Golang, um HTML -Vorlagen zu bedienen, aber ich habe ein Problem, bei dem die Titels korrekt aktualisiert werden. Der Inhalt bleibt jedoch sowohl für das Haus (/) als auch für (/über) Seiten gleich. Der Inhalt ist jedoch immer noch der gleiche wie die Startseite (/), anstatt den richtigen Überblicksinhalt zu zeigen.
Projektstruktur

Code: Select all

/app
/handlers
home.go
about.go
/views
renderer.go
/templates
/layouts
main.html
/pages
index.html
about.html
main.go
< /code>
Code -Implementierung
Main.go (Einstiegspunkt) < /p>
package main

import (
"app/handlers"
"app/views"

"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
)

func main() {
e := echo.New()

e.Use(middleware.Logger())
e.Use(middleware.Recover())

e.Renderer = views.NewRenderer()

e.GET("/", handlers.Home)
e.GET("/about", handlers.About)

e.Static("/static", "static")

e.Logger.Fatal(e.Start(":8080"))
}
views/renderer.go (Vorlagendrenderer)

Code: Select all

package views

import (
"html/template"
"io"
"path/filepath"
"strings"

"github.com/labstack/echo/v4"
)

type TemplateRenderer struct {
templates *template.Template
}

func NewRenderer() *TemplateRenderer {
funcMap := template.FuncMap{
"dict": func(values ...interface{}) map[string]interface{} {
if len(values)%2 != 0 {
panic("invalid dict call")
}
dict := make(map[string]interface{}, len(values)/2)
for i := 0; i < len(values); i += 2 {
key, ok := values[i].(string)
if !ok {
panic("dict keys must be strings")
}
dict[key] = values[i+1]
}
return dict
},
}

tmpl := template.New("").Funcs(funcMap)

layoutGlob := filepath.Join("templates", "layouts", "*.html")
pagesGlob := filepath.Join("templates", "pages", "*.html")

tmpl = template.Must(tmpl.ParseGlob(layoutGlob))
tmpl = template.Must(tmpl.ParseGlob(pagesGlob))

normalizedTemplates := template.New("").Funcs(funcMap)
for _, t := range tmpl.Templates() {
if t.Tree == nil || t.Tree.Root == nil {
continue
}
name := t.Name()
if strings.HasPrefix(name, "pages/") {
name = strings.TrimPrefix(name, "pages/")
}
name = strings.TrimSuffix(name, ".html")
_, err := normalizedTemplates.New(name).Parse(t.Tree.Root.String())
if err != nil {
panic("Failed to normalize template names: " + err.Error())
}
}

return &TemplateRenderer{
templates: normalizedTemplates,
}
}

func (t *TemplateRenderer) Render(w io.Writer, name string, data interface{}, c echo.Context) error {
return t.templates.ExecuteTemplate(w, name, data)
}
Handlers/home.go

Code: Select all

package handlers

import (
"net/http"

"github.com/labstack/echo/v4"
)

func Home(c echo.Context) error {
return c.Render(http.StatusOK, "index", map[string]interface{}{})
}
Handlers/about.go

Code: Select all

package handlers

import (
"net/http"

"github.com/labstack/echo/v4"
)

func About(c echo.Context) error {
return c.Render(http.StatusOK, "about", map[string]interface{}{})
}
Vorlagen
Templates/Layouts/main.html (Layout)

Code: Select all

{{ define "layout" }}





{{ block "title" .  }}{{ if .title }}{{ .title }} | Application{{ else }}Application{{ end }}{{ end }}




[url=/]Home[/url] | [url=/about]About[/url]


{{ template "content" . }}



{{ end }}
Templates/pages/index.html

Code: Select all

{{ template "layout" (dict "title" "Home") }}

{{ define "content" }}
Welcome to the Home Page
This is a simple multi-page app with Echo.
{{ end }}
Templates/pages/about.html

Code: Select all

{{ template "layout" (dict "title" "About") }}

{{ define "content" }}
About
This is a simple multi-page app with Echo.
{{ end }}
Erwartete Verhalten
/sollte index.html in layout/main.html. p>
Tatsächliches Verhalten
/funktioniert wie erwartet. /> Mögliches Problem
Ich vermute, dass {{{{Vorlage "" Layout "(dikte" title "" über ")}} in ungefähr.html direkt aufgerufen und das Layout direkt aufgerufen und" index.html "Inhalt anstelle seiner rensiert eigene.

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post