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"))
}
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)
}
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{}{})
}
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{}{})
}
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 }}
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 }}
Code: Select all
{{ template "layout" (dict "title" "About") }}
{{ define "content" }}
About
This is a simple multi-page app with Echo.
{{ end }}
/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.