【发布时间】:2022-08-20 01:18:12
【问题描述】:
我正在使用带有内置 HTML 模板引擎的 Go 1.19。有没有办法测试一个块是否在特定的模板文件中定义?
具体来说,我想在 Go HTML 模板中实现可选的标题条目。
我有一个通用布局模板,在呈现时包含一个内容模板。
我想实现如下...
目前,<meta name=\"description\" content=\"{{block \"description\" .}}{{end}}\"> 导致一个空的描述标签。我想根本没有标签,里面什么都没有。
有任何想法吗?
layout.gohtml(简化)[更新]
<html>
<head>
<title>{{block \"title\" .}}The Title{{end}}</title>
{{if .renderDescription}}
<meta name=\"description\" content=\"{{template \"description\" .}}\">
{{end}
</head>
<body>
<header></header>
{{template \"content\" .}}
<footer></footer>
</body>
</html>
content1.gohtml
{{define \"title\"}}The 2hO Network{{end}}
{{define \"description\"}}An options description{{end}}
{{define \"content\"}}
Vestibulum ante ipsum primis in faucibus...
{{end}}
content2.gohtml
{{define \"title\"}}The 2hO Network{{end}}
{{define \"content\"}}
Vestibulum ante ipsum primis in faucibus...
{{end}}
标签: go go-templates