英文:
How to extend a template in go?
问题
以下是翻译好的内容:
这是问题:每个页面的“content”部分有几个“article”,我想在每个文章下面插入一个“likebar”模板。
所以“base.tmpl”如下:
<html>
<head>
{{template "head.tmpl" .}}
</head>
<body>
{{template "content.tmpl" .}}
</body>
</html>
在“article.tmpl”中,我想要:
{{define "content"}}
<div>article 1
{{template "likebar.tmpl" .}}
</div>
<div>article 2
{{template "likebar.tmpl" .}}
</div>
... //这些div是动态生成的
{{end}}
如何使用“html/template”实现这一点?
我尝试在“base.tmpl”中插入一个“{{template "iconbar" .}}”,然后在“{{define "content"}`中嵌套“{{template "likebar.tmpl" .}}”,但是出现了以下错误:
> 模板文件错误:html/template:base.tmpl:122:12: 没有名为“likebar.tmpl”的模板。
英文:
Here is the problem: There are several article
s on each page's content
section and I'd like to insert a likebar
template below each article.
So the base.tmpl
is like:
<html>
<head>
{{template "head.tmpl" .}}
</head>
<body>
{{template "content.tmpl" .}}
</body>
</html>
and in article.tmpl
I want to have :
{{define "content"}}
<div>article 1
{{template "likebar.tmpl" .}}
</div>
<div>article 2
{{template "likebar.tmpl" .}}
</div>
... //these divs are generated dynamically
{{end}}
How can I achieve this with html/template
?
I have tried to insert a {{template "iconbar" .}}
in base.tmpl
and then nested {{template "likebar.tmpl" .}}
inside {{define "content"
but it failed with:
> Template File Error: html/template:base.tmpl:122:12: no such template
> "likebar.tmpl"
答案1
得分: 3
你只能包含/插入关联模板。
如果你有多个模板文件,可以使用template.ParseFiles()
或template.ParseGlob()
来解析它们,结果模板将包含所有已关联的模板,因此它们可以相互引用。
如果你确实使用上述函数来解析模板,那么找不到likebar.tmpl
的原因是因为你使用了无效的名称引用它(例如,缺少文件夹名称)。
当从string
源解析时,你可以使用Template.Parse()
方法,它还将嵌套模板关联到顶级模板。
看看这两个可行的示例:
func main() {
t := template.Must(template.New("").Parse(templ1))
if err := t.Execute(os.Stdout, nil); err != nil {
panic(err)
}
t2 := template.Must(template.New("").Parse(templ2))
template.Must(t2.Parse(templ2Like))
if err := t2.Execute(os.Stdout, nil); err != nil {
panic(err)
}
}
const templ1 = `Base template #1
And included one: {{template "likebar"}}
{{define "likebar"}}I'm likebar #1.{{end}}
`
const templ2 = `Base template #2
And included one: {{template "likebar"}}
`
const templ2Like = `{{define "likebar"}}I'm likebar #2.{{end}}`
输出结果(在Go Playground上尝试):
Base template #1
And included one: I'm likebar #1.
Base template #2
And included one: I'm likebar #2.
英文:
You can only include / insert associated templates.
If you have multiple template files, use template.ParseFiles()
or template.ParseGlob()
to parse them all, and the result template will have all the templates, already associated, so they can refer to each other.
If you do use the above functions to parse your templates, then the reason why it can't find likebar.tmpl
is because you are referring to it by an invalid name (e.g. missing folder name).
When parsing from string
source, you may use the Template.Parse()
method, which also associates nested templates to the top level template.
See these 2 working examples:
func main() {
t := template.Must(template.New("").Parse(templ1))
if err := t.Execute(os.Stdout, nil); err != nil {
panic(err)
}
t2 := template.Must(template.New("").Parse(templ2))
template.Must(t2.Parse(templ2Like))
if err := t2.Execute(os.Stdout, nil); err != nil {
panic(err)
}
}
const templ1 = `Base template #1
And included one: {{template "likebar"}}
{{define "likebar"}}I'm likebar #1.{{end}}
`
const templ2 = `Base template #2
And included one: {{template "likebar"}}
`
const templ2Like = `{{define "likebar"}}I'm likebar #2.{{end}}`
Output (try it on the Go Playground):
Base template #1
And included one: I'm likebar #1.
Base template #2
And included one: I'm likebar #2.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论