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

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

How to _not_ strip HTML comments from a HTML template in gin gonic

问题

我正在使用Gin Gonic和一个HTML模板文件。

我的模板文件包含(多行)HTML注释,格式如<!-- my comment goes here-->
我希望HTML内容在返回的输出中保留,这个输出是通过以下代码返回的:

  1. c.HTML(http.StatusOK, "static/templates/mytemplate.html", gin.H{
  2. "name": "World",
  3. })

其中c是一个*gin.Context

问题: 如何配置模板引擎或c.HTML,以便不从模板中删除HTML注释?

更详细的说明

/static/templates/mytemplate.html:

  1. <!DOCTYPE html>
  2. <html lang="de">
  3. <body>
  4. <!--
  5. 这些行在输出中丢失了
  6. -->
  7. Hello World
  8. </body>
  9. </html>

我的处理程序:

  1. func NewRouter() *gin.Engine {
  2. router := gin.Default()
  3. // ... 从文件系统加载模板 ...
  4. router.GET("/foo", fooHandler)
  5. return router
  6. }
  7. func fooHandler(c *gin.Context) {
  8. c.HTML(http.StatusOK, "static/templates/mytemplate.html", gin.H{
  9. "name": "World",
  10. })
  11. }

编辑
我尝试将注释添加为常量:

  1. {{"<!-- my comment goes here -->"}}

但是标签被转义为:

  1. &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

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

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:

  1. &lt;!DOCTYPE html&gt;
  2. &lt;html lang=&quot;de&quot;&gt;
  3. &lt;body&gt;
  4. &lt;!--
  5. these lines are missing in the output
  6. --&gt;
  7. Hello World
  8. &lt;/body&gt;
  9. &lt;/html&gt;

My Handler:

  1. func NewRouter() *gin.Engine {
  2. router := gin.Default()
  3. // ... load templates from file system ...
  4. router.GET(&quot;/foo&quot;, fooHandler)
  5. return router
  6. }
  7. func fooHandler(c *gin.Context) {
  8. c.HTML(http.StatusOK, &quot;static/templates/mytemplate.html&quot;, gin.H{
  9. &quot;name&quot;: &quot;World&quot;,
  10. })
  11. }

Edit
I tried to add the comments as constants:

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

But then the tags are escaped as

  1. &amp;lt;!-- foo --&amp;gt;

答案1

得分: 1

我猜测HTML注释被删除的原因是因为我将HTML模板作为字符串读取(而不是直接作为文件)。但我还不能确定具体的原因。无论如何,对我起作用的解决方法是在模板中使用占位符:

  1. <!DOCTYPE html>
  2. <html lang="de">
  3. <body>
  4. {{ .myComment }}
  5. Hello World
  6. </body>
  7. </html>

然后将HTML注释本身作为参数传递:

  1. const myHtmlComment string = `
  2. <!--
  3. 这些行在输出中(不再)丢失
  4. -->
  5. `
  6. func fooHandler(c *gin.Context) {
  7. c.HTML(http.StatusOK, "static/templates/mytemplate.html", gin.H{
  8. "name": "World",
  9. "myComment": template.HTML(myHtmlComment),
  10. })
  11. }

需要导入"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:

  1. &lt;!DOCTYPE html&gt;
  2. &lt;html lang=&quot;de&quot;&gt;
  3. &lt;body&gt;
  4. {{ .myComment }}
  5. Hello World
  6. &lt;/body&gt;
  7. &lt;/html&gt;

And pass the HTML comment itself as parameter:

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

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:

确定