我正在嘗試更改 html 模板中的分隔符。不幸的是,它在 render 函數(shù)中不起作用,在 main 函數(shù)中也不起作用。請(qǐng)參閱https://pkg.go.dev/text/template#Template.Delimspackage mainimport ( "html/template" "io" "net/http" "github.com/labstack/echo/v4" "github.com/labstack/echo/v4/middleware")type Template struct { templates *template.Template}func (t *Template) Render(w io.Writer, name string, data interface{}, c echo.Context) error { t.templates.Delims("[[", "]]") return t.templates.ExecuteTemplate(w, name, data)}func Hello(c echo.Context) error { test := `{ "name" : "Ben", "country" : "Germany", "city" : "Berlin", "body":{"test":"test","test2":"test2"} }` return c.Render(http.StatusOK, "hello", test)}func main() { // Echo instance e := echo.New() t := &Template{ templates: template.Must(template.ParseGlob("public/views/*.html")), } t.templates.Delims("[[", "]]") e.Renderer = t e.GET("/hello", Hello) // Middleware e.Use(middleware.Logger()) e.Use(middleware.Recover()) // Start server e.Logger.Fatal(e.Start(":8000"))}
1 回答

幕布斯7119047
TA貢獻(xiàn)1794條經(jīng)驗(yàn) 獲得超8個(gè)贊
你必須Delims
先打電話ParseGlob
。像這樣:
t := &Template{ templates: template.Must(template.New("").Delims("[[", "]]").ParseGlob("public/views/*.html")), }
- 1 回答
- 0 關(guān)注
- 120 瀏覽
添加回答
舉報(bào)
0/150
提交
取消