3 回答

TA貢獻1876條經驗 獲得超5個贊
我認為,在解析模板后調整路徑為時已晚。
可行的方法(雖然我不確定這里是否是最優雅的解決方案)是使用以下AddParseTree
方法:
AddParseTree 為具有給定名稱的模板添加解析樹并將其與 t 相關聯。如果模板尚不存在,它將創建一個新模板。如果模板確實存在,它將被替換。
適用于您的情況,根據您將Parse
相關模板文件(子模板 1 或 2)的條件,然后在執行之前將其添加到AddParseTree
to 。tpl

TA貢獻1810條經驗 獲得超4個贊
最后我讓它工作了,但只有在不遵循手冊的情況下。
解決方案第 1 部分
跳過模板中的 {{define}} 和 {{end}}。詭異的...
<html>
? <head>
? ? <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
? ? <title>Go Web Programming</title>
? </head>
? <body>
? ? layout level
? ? {{ template "content" . }}
? </body>
</html>
在子模板中也是如此......
<h1 style="color: red;">Page 1!</h1>
解決方案第 2 部分
我找到了一個帶有 AddParsTree 的代碼片段,這是代碼(經過簡化,沒有錯誤處理)
package main
import (
? ? "html/template"
? ? "net/http"
? ? "strings"
)
var tpl *template.Template
func init() {
? ? tpl = template.Must(template.ParseGlob("templates/*.html"))
}
func main() {
? ? http.HandleFunc("/", index)
? ? http.ListenAndServe(":8080", nil)
}
func index(w http.ResponseWriter, r *http.Request) {
? ? path := strings.Trim(r.URL.Path, "/")
? ? switch path {
? ? case "":
? ? ? ? path = ("home.html")
? ? default:
? ? ? ? path = (path + ".html")
? ? }
? ? layout := tpl.Lookup("layout.html")
? ? layout, _ = layout.Clone()
? ? t := tpl.Lookup(path)
? ? _, _ = layout.AddParseTree("content", t.Tree)
? ? layout.Execute(w, "")
我真的不明白為什么我必須不遵守手冊才能讓它工作。任何啟發我的評論將不勝感激。

TA貢獻1772條經驗 獲得超8個贊
我遇到了同樣的問題并以這種方式解決了它:
這對我來說適用于{{ define "content1" }}...{{ end }}語法,但你必須給你的模板唯一的名稱:content1和content2。
我制作了一個用于處理模板內容的包。
package templates
import (
"html/template"
"log"
"net/http"
)
var tmpls *template.Template
// LoadTemplates parses a template folder
func LoadTemplates(tmplPath string) {
tmpls = template.Must(template.ParseGlob(tmplPath))
}
// ExecTmpl executes a template and writes it
func ExecTmpl(w http.ResponseWriter, tmpl string, data interface{}) {
tmpls.ExecuteTemplate(w, tmpl, data)
}
// ExecTmplTree combines and executes two templates
func ExecTmplTree(w http.ResponseWriter, outer, inner string, data interface{}) {
layout, err := tmpls.Lookup(outer).Clone()
if err != nil || layout == nil {
log.Fatal("ExecTmplTree: Could not get a copy of template", outer)
}
content, err := tmpls.Lookup(inner).Clone()
if err != nil || content == nil {
log.Fatal("ExecTmplTree: Could not get a copy of template", inner)
}
_, err = layout.AddParseTree("content", content.Tree)
if err != nil {
log.Fatal("ExecTmplTree: Templates could not be combined.")
}
layout.Execute(w, data)
}
然后我調用函數如下(在 main.go 或其他地方):
// call first to load templates from the folder where your templates are stored
// you might need fit the file ending, too, I simply use .html for html-templates
func init() {
templates.LoadTemplates("web/templates/*.html")
}
// write a basic template without nesting
func exampleHandler(w http.ResponseWriter, r *http.Request) {
templates.ExecTmplTree(w, "content1", "messageForm", nil)
}
// write nested templates. here, index is the outer template
// and code is the inner
func indexHandler(w http.ResponseWriter, r *http.Request) {
templates.ExecTmplTree(w, "layout", "content1", nil)
}
- 3 回答
- 0 關注
- 166 瀏覽
添加回答
舉報