猿问

在 go 中执行多个模板

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


var (

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

)

提前致谢


慕斯王
浏览 137回答 1
1回答

绝地无双

您可以使用该{{ template "name" }}操作将模板包含在其他模板中。假设您有文件a.html:<p>this&nbsp;is&nbsp;A</p>然后你可以b.html像这样包含它:{{&nbsp;template&nbsp;"a.html"&nbsp;}} <p>this&nbsp;is&nbsp;B</p>然后如果你运行templates.ExecuteTemplate(os.Stdout, "b.html", nil)你会得到:<p>this&nbsp;is&nbsp;A</p> <p>this&nbsp;is&nbsp;B</p>有关更充实的示例,请参阅操场链接:https:&nbsp;//play.golang.com/p/W0whRWlX119
随时随地看视频慕课网APP

相关分类

Go
我要回答