go - 在 go 中执行多个模板
问题描述
我有这个变量,我想知道我是否可以执行多个模板,如果我不能用这个变量来做,那我该怎么办?
var (
templates = template.Must(template.ParseGlob("web/Pages/*"))
)
提前致谢
解决方案
您可以使用该{{ 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
推荐阅读
- javascript - 如何将输入到输入中的单词存储为整个单词,而不是在键入时存储单词
- c++ - 等待接受方法 30 秒后退出服务器
- regex - 正则表达式,收集 1 行或多行的抓取信息
- deprecation-warning - 解决 Webpack 5.9.0 弃用调用 [DEP_WEBPACK_DEPRECATION_ARRAY_TO_SET] 和 [DEP_WEBPACK_CHUNK_MODULES_ITERABLE]
- python - 使用 Date 对象更新集合(百万+)中的所有文档
- c# - 从旋转矩阵转换为四元数时,不确定如何防止 270 度与 90 度混淆
- angular - 键盘隐藏输入 - Ionic 5
- arrays - 加载输入帮助 getline 并使用 sscanf
- android - 使用用户 ID 从 Firebase 获取数据
- javascript - Mongo DB 错误:未知的顶级运算符:$set; 如何更新此记录?