How to not strip HTML comments in HTML template in gin gonic
P粉193307465
P粉193307465 2023-07-18 19:17:03
0
1
445

I'm using Gin Gonic and an HTML template file.

My template file contains (multi-line) HTML comments similar to <!--This is my comment -->. I wish to preserve the HTML content in the returned output.

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

Question: How to configure the template engine or c.HTML to not strip HTML comments in templates?

More detailed answer

/static/templates/mytemplate.html:

<!DOCTYPE html>
<html lang="de">
<body>
<!--
这些行在输出中缺失。
-->
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",
    })
}

After editing, I tried adding the annotation as a constant :

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

but the tag is escaped as

<!-- foo --> 


P粉193307465
P粉193307465

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!