我正在尝试html/template在 Go 中使用嵌入模板。我非常喜欢无逻辑模板设计,我相信它能够按预期安全地转义(有时其他模板库会出错)。
但是,我在尝试实现一个小助手以根据“最终”模板名称在我的 HTTP 处理程序中呈现我的模板时遇到了一些问题。我的 base.tmpl 在我的所有页面中都是有效的“标准”,如果不是,我可以{{ template checkoutJS }}在 base.tmpl 中设置并通过设置添加一些每页 JS {{ define checkoutJS }}https://path.to/extra.js {{ end }}。
我希望能够renderTemplate(w, "template_name.tmpl", data)在我的 HTTP 处理程序中说,data包含字符串或结构的 map[string]interface{}在哪里,我想填写任何内容。
这是到目前为止的代码:
基础文件
{{ define "base" }}
<!DOCTYPE html>
<html>
<head>
<title>{{ template "title" . }}</title>
</head>
<body>
<div id="sidebar">
...
</div>
{{ template "content" }}
<div id="footer">
...
</div>
</body>
</html>
create_listing.tmpl
{{ define "title" }}Create a New Listing{{ end }}
{{ define "content" }}
<form>
...
</form>
{{ end }}
login_form.tmpl
{{ define "title" }}Login{{ end }}
{{ define "content" }}
<form>
...
</form>
{{ end }}
摇曳的蔷薇
交互式爱情
相关分类