如何渲染多个模板

创建了一个基本模板。随着呈现的 first.html 又一个模板。


eg. :

    var tmpl = template.Must(template.ParseFiles(

    "templates/base.html",

    "templates/first.html",

    ))

但我也想添加更多的 .html 文件来渲染。有参考吗?


慕田峪7331174
浏览 186回答 3
3回答

慕的地10843

如果在模板文件夹中定义所有模板,则可以使用以下命令轻松解析整个目录:template.Must(template.ParseGlob("YOURDIRECTORY/*"))例如:头文件{{define "header"}}&nbsp; &nbsp; &nbsp;<head>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<title>Index</title>&nbsp; &nbsp; &nbsp;</head>{{end}}索引.html{{define "indexPage"}}&nbsp; &nbsp; <html>&nbsp; &nbsp; {{template "header"}}&nbsp; &nbsp; <body>&nbsp; &nbsp; &nbsp; &nbsp; <h1>Index</h1>&nbsp; &nbsp; </body>&nbsp; &nbsp; </html>{{end}}main.gopackage mainimport(&nbsp; &nbsp; "html/template")// compile all templates and cache themvar templates = template.Must(template.ParseGlob("YOURTEMPLATEDIR/*"))func main(){&nbsp; &nbsp; ...}func IndexHandler(w http.ResponseWriter, r *http.Request) {&nbsp; &nbsp; // you access the cached templates with the defined name, not the filename&nbsp; &nbsp; err := templates.ExecuteTemplate(w, "indexPage", nil)&nbsp; &nbsp; if err != nil {&nbsp; &nbsp; &nbsp; &nbsp; http.Error(w, err.Error(), http.StatusInternalServerError)&nbsp; &nbsp; &nbsp; &nbsp; return&nbsp; &nbsp; }}你执行你的 indexPage-Template templates.ExecuteTemplate(w, "indexPage", nil)

湖上湖

如果你想用特定文件解析多个目录,这里有一个代码片段://parse a pattern in a specific directory&nbsp;&nbsp;allTemplates := template.Must(template.ParseGlob("Directory/*"))//add another directory for parsing&nbsp;allTemplates = template.Must(allTemplates.ParseGlob("Another_Directory/*.tmpl"))//add specific file name&nbsp;allTemplates = template.Must(allTemplates.ParseFiles("path/to/file.tmpl"))func main() {&nbsp;...}func FileHandler(w http.ResponseWriter, r *http.Request) {&nbsp; &nbsp; // access cached template by file name (in case you didn't define its name)&nbsp;&nbsp; &nbsp; err := allTemplates.ExecuteTemplate(w, "file.tmpl", nil)&nbsp; &nbsp; if err != nil {&nbsp; &nbsp; &nbsp; &nbsp; http.Error(w, err.Error(), http.StatusInternalServerError)&nbsp; &nbsp; &nbsp; &nbsp; return&nbsp; &nbsp; }}func NameHandler(w http.ResponseWriter, r *http.Request) {&nbsp; &nbsp; // access cached template by handler name&nbsp;&nbsp; &nbsp; err := allTemplates.ExecuteTemplate(w, "handlerName", nil)&nbsp; &nbsp; if err != nil {&nbsp; &nbsp; &nbsp; &nbsp; http.Error(w, err.Error(), http.StatusInternalServerError)&nbsp; &nbsp; &nbsp; &nbsp; return&nbsp; &nbsp; }}快捷方式:allTemplates := template.Must(&nbsp; &nbsp; &nbsp; &nbsp; template.Must(&nbsp; &nbsp; &nbsp; &nbsp; template.Must(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; template.ParseGlob("directory/*.tmpl")).&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ParseGlob("another_directory/*.tmpl")).&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ParseFiles("path/to/file.tmpl")),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; )file.tmpl 可以如下所示:<html>This is a template file&nbsp;</html>name.tmpl 应该看起来像这样{{define "handlerName" }}<p>this is a handler</p>{{end}}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go