[英]How to assign an alias to a module on import?
手冊給出了一個例子:
t := &Template{
templates: template.Must(template.ParseGlob("public/views/*.html")),
}
其中模板沿路徑public/views/*.html
。
我需要更改此代碼,以便模板不是在一個中編譯,而是在兩個指定路徑中編譯,即pjt_*/templates/include/*.html
和pjt_*/templates/site/*.html
。
我的代碼:
package main
import(
"html/template"
"io"
"net/http"
"github.com/labstack/echo/v4"
)
type TemplateRenderer struct {
templates *template.Template
}
func (t *TemplateRenderer) Render(w io.Writer, name string, data interface{}, c echo.Context) error {
if viewContext, isMap := data.(map[string]interface{}); isMap {
viewContext["reverse"] = c.Echo().Reverse
}
return t.templates.ExecuteTemplate(w, name, data)
}
func main() {
e := echo.New()
e.Renderer = &TemplateRenderer{
// here I want templates to be compiled, not only for
// "pjt_*/templates/include/*.html" but also "pjt_*/templates/site/*.html"
templates: template.Must(template.ParseGlob("pjt_*/templates/include/*.html")),
}
e.GET("/something", func(c echo.Context) error {
return c.Render(http.StatusOK, "template.html", "World")
})
e.Logger.Fatal(e.Start(":8000"))
}
PS指南,以防萬一: https://echo.labstack.com/guide/templates/
template.ParseGlob接受一個模式並從該模式標識的文件中解析模板定義。
使用匹配pjt_*/templates/include/*.html
和pjt_*/templates/site/*.html
的模式調用它。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.