如何在gin gonic中不去除HTML模板中的HTML注释。

huangapple go评论91阅读模式
英文:

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 -->"}}

但是标签被转义为:

&lt;!-- foo --&gt;
英文:

I'm using Gin Gonic with a HTML template file.

My template file contains (multi line) HTML comments of the kind &lt;!-- my comment goes here--&gt;.
I want that the HTML content is preserved in the output which is returned by

c.HTML(http.StatusOK, &quot;static/templates/mytemplate.html&quot;, gin.H{
	&quot;name&quot;: &quot;World&quot;,
})

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:

&lt;!DOCTYPE html&gt;
&lt;html lang=&quot;de&quot;&gt;
&lt;body&gt;
&lt;!--
these lines are missing in the output
--&gt;
Hello World
&lt;/body&gt;
&lt;/html&gt;

My Handler:

func NewRouter() *gin.Engine {
	router := gin.Default()
	// ... load templates from file system ...
	router.GET(&quot;/foo&quot;, fooHandler)
	return router
}
func fooHandler(c *gin.Context) {
	c.HTML(http.StatusOK, &quot;static/templates/mytemplate.html&quot;, gin.H{
		&quot;name&quot;: &quot;World&quot;,
	})
}

Edit
I tried to add the comments as constants:

{{&quot;&lt;!-- my comment goes here --&gt;&quot;}}

But then the tags are escaped as

&amp;lt;!-- foo --&amp;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:

&lt;!DOCTYPE html&gt;
&lt;html lang=&quot;de&quot;&gt;
&lt;body&gt;
{{ .myComment }}
Hello World
&lt;/body&gt;
&lt;/html&gt;

And pass the HTML comment itself as parameter:

const myHtmlComment string = `
&lt;!--
these lines are (not) missing (anymore) in the output
--&gt;
`
func fooHandler(c *gin.Context) {
    c.HTML(http.StatusOK, &quot;static/templates/mytemplate.html&quot;, gin.H{
        &quot;name&quot;: &quot;World&quot;,
        &quot;myComment&quot;: template.HTML(myHtmlComment),
    })
}

with import &quot;html/template&quot;

huangapple
  • 本文由 发表于 2023年7月18日 03:46:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/76707663.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定