Golang索引模板包括
Golang index template including
我在 my project 中有两个模板,如下所示:
var indextemplate = template.Must(template.New("").Parse(`<!DOCTYPE html>
<form action="/compare" method="post">
<input type="date" name="from" required>
<input type="submit">
</form>`))
var comparetemplate = template.Must(template.New("").Parse("Hours since {{.From}} are {{.Duration}}"))
我不明白如何构建代码,所以我有 HTML 模板(有一个头部和 </html>
在末尾)并将这些模板包含到正文中。
我也不太明白构建代码以使模板与处理程序匹配的最佳做法是什么。由于 IIUC,您最好在处理程序之外编译模板。
你应该知道的是template.Template
can be a collection of multiple templates, see its Template.Templates()
这个集合中returns方法的一个值。
集合中的每个模板都有一个唯一的名称,可以用来引用它(请参阅 Template.Name()
)。还有一个 {{template "name" pipeline}}
操作,您可以使用它在一个模板中包含其他模板,另一个模板是集合的一部分。
看这个例子。让我们定义 2 个模板:
const tmain = `<html><body>
Some body. Now include the other template:
{{template "content" .}}
</body></html>
`
const tcontent = `I'M THE CONTENT, param passed is: {{.Param}}`
如您所见,tmain
包含另一个名为 "content"
的模板。您可以使用 Template.New()
method (stressing: method, not to be confused with the func template.New()
) 创建一个新的关联命名模板,该模板将成为您正在调用其方法的模板的一部分。因此,它们可以相互引用,例如他们可以互相包含。
让我们看看将这 2 个模板解析为一个 template.Template
以便它们可以相互引用的代码(为简洁起见省略了错误检查):
t := template.Must(template.New("main").Parse(tmain))
t.New("content").Parse(tcontent)
param := struct{ Param string }{"paramvalue"}
if err := t.ExecuteTemplate(os.Stdout, "main", param); err != nil {
fmt.Println(err)
}
输出(在 Go Playground 上尝试):
<html><body>
Some body. Now include the other template:
I'M THE CONTENT, param passed is: paramvalue
</body></html>
备选
另请注意,如果您有更多更大的模板,那么它的可读性和可维护性就会降低。您应该考虑将模板保存为文件,并且可以使用 template.ParseFiles()
and template.ParseGlob()
,它们都可以一次解析多个文件并从中构建模板集合,因此它们可以相互引用。模板的名称将是文件的名称。
我在 my project 中有两个模板,如下所示:
var indextemplate = template.Must(template.New("").Parse(`<!DOCTYPE html>
<form action="/compare" method="post">
<input type="date" name="from" required>
<input type="submit">
</form>`))
var comparetemplate = template.Must(template.New("").Parse("Hours since {{.From}} are {{.Duration}}"))
我不明白如何构建代码,所以我有 HTML 模板(有一个头部和 </html>
在末尾)并将这些模板包含到正文中。
我也不太明白构建代码以使模板与处理程序匹配的最佳做法是什么。由于 IIUC,您最好在处理程序之外编译模板。
你应该知道的是template.Template
can be a collection of multiple templates, see its Template.Templates()
这个集合中returns方法的一个值。
集合中的每个模板都有一个唯一的名称,可以用来引用它(请参阅 Template.Name()
)。还有一个 {{template "name" pipeline}}
操作,您可以使用它在一个模板中包含其他模板,另一个模板是集合的一部分。
看这个例子。让我们定义 2 个模板:
const tmain = `<html><body>
Some body. Now include the other template:
{{template "content" .}}
</body></html>
`
const tcontent = `I'M THE CONTENT, param passed is: {{.Param}}`
如您所见,tmain
包含另一个名为 "content"
的模板。您可以使用 Template.New()
method (stressing: method, not to be confused with the func template.New()
) 创建一个新的关联命名模板,该模板将成为您正在调用其方法的模板的一部分。因此,它们可以相互引用,例如他们可以互相包含。
让我们看看将这 2 个模板解析为一个 template.Template
以便它们可以相互引用的代码(为简洁起见省略了错误检查):
t := template.Must(template.New("main").Parse(tmain))
t.New("content").Parse(tcontent)
param := struct{ Param string }{"paramvalue"}
if err := t.ExecuteTemplate(os.Stdout, "main", param); err != nil {
fmt.Println(err)
}
输出(在 Go Playground 上尝试):
<html><body>
Some body. Now include the other template:
I'M THE CONTENT, param passed is: paramvalue
</body></html>
备选
另请注意,如果您有更多更大的模板,那么它的可读性和可维护性就会降低。您应该考虑将模板保存为文件,并且可以使用 template.ParseFiles()
and template.ParseGlob()
,它们都可以一次解析多个文件并从中构建模板集合,因此它们可以相互引用。模板的名称将是文件的名称。