英文:
How to _not_ strip HTML comments from a HTML template in gin gonic
问题
我正在使用Gin Gonic和一个HTML模板文件。
我的模板文件包含(多行)HTML注释,格式如<!-- my comment goes here-->
。
我希望HTML内容在返回的输出中保留,这个输出是通过以下代码返回的:
c.HTML(http.StatusOK, "static/templates/mytemplate.html", gin.H{
"name": "World",
})
其中c
是一个*gin.Context
。
问题: 如何配置模板引擎或c.HTML,以便不从模板中删除HTML注释?
更详细的说明
/static/templates/mytemplate.html
:
<!DOCTYPE html>
<html lang="de">
<body>
<!--
这些行在输出中丢失了
-->
Hello World
</body>
</html>
我的处理程序:
func NewRouter() *gin.Engine {
router := gin.Default()
// ... 从文件系统加载模板 ...
router.GET("/foo", fooHandler)
return router
}
func fooHandler(c *gin.Context) {
c.HTML(http.StatusOK, "static/templates/mytemplate.html", gin.H{
"name": "World",
})
}
编辑
我尝试将注释添加为常量:
{{"<!-- my comment goes here -->"}}
但是标签被转义为:
<!-- foo -->
英文:
I'm using Gin Gonic with a HTML template file.
My template file contains (multi line) HTML comments of the kind <!-- my comment goes here-->
.
I want that the HTML content is preserved in the output which is returned by
c.HTML(http.StatusOK, "static/templates/mytemplate.html", gin.H{
"name": "World",
})
where c
is a *gin.Context
.
Question: How to configure the template engine or c.HTML to not strip the HTML comments from the template?
More Detailed
/static/templates/mytemplate.html
:
<!DOCTYPE html>
<html lang="de">
<body>
<!--
these lines are missing in the output
-->
Hello World
</body>
</html>
My Handler:
func NewRouter() *gin.Engine {
router := gin.Default()
// ... load templates from file system ...
router.GET("/foo", fooHandler)
return router
}
func fooHandler(c *gin.Context) {
c.HTML(http.StatusOK, "static/templates/mytemplate.html", gin.H{
"name": "World",
})
}
Edit
I tried to add the comments as constants:
{{"<!-- my comment goes here -->"}}
But then the tags are escaped as
&lt;!-- foo --&gt;
答案1
得分: 1
我猜测HTML注释被删除的原因是因为我将HTML模板作为字符串读取(而不是直接作为文件)。但我还不能确定具体的原因。无论如何,对我起作用的解决方法是在模板中使用占位符:
<!DOCTYPE html>
<html lang="de">
<body>
{{ .myComment }}
Hello World
</body>
</html>
然后将HTML注释本身作为参数传递:
const myHtmlComment string = `
<!--
这些行在输出中(不再)丢失
-->
`
func fooHandler(c *gin.Context) {
c.HTML(http.StatusOK, "static/templates/mytemplate.html", gin.H{
"name": "World",
"myComment": template.HTML(myHtmlComment),
})
}
需要导入"html/template"
。
英文:
I guess the reason why the HTML comment is stripped is because I read the HTML template as string (instead of as file directly). Still cannot nail it down exactly.
Anyway, the workaround that did the job for me, was to use a place holder in the template:
<!DOCTYPE html>
<html lang="de">
<body>
{{ .myComment }}
Hello World
</body>
</html>
And pass the HTML comment itself as parameter:
const myHtmlComment string = `
<!--
these lines are (not) missing (anymore) in the output
-->
`
func fooHandler(c *gin.Context) {
c.HTML(http.StatusOK, "static/templates/mytemplate.html", gin.H{
"name": "World",
"myComment": template.HTML(myHtmlComment),
})
}
with import "html/template"
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论