首页 > 解决方案 > 在 go 中执行多个模板

问题描述

我有这个变量,我想知道我是否可以执行多个模板,如果我不能用这个变量来做,那我该怎么办?

var (
    templates = template.Must(template.ParseGlob("web/Pages/*"))
)

提前致谢

标签: go

解决方案


您可以使用该{{ template "name" }}操作将模板包含在其他模板中。

假设您有文件a.html

<p>this is A</p>

然后你可以b.html像这样包含它:

{{ template "a.html" }}
<p>this is B</p>

然后如果你运行templates.ExecuteTemplate(os.Stdout, "b.html", nil)你会得到:

<p>this is A</p>
<p>this is B</p>

有关更充实的示例,请参阅操场链接:https: //play.golang.com/p/W0whRWlX119


推荐阅读