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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Rina0
    New Member
    • Jul 2023
    • 13

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

    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

    Code:
    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


    Code:
    /static/templates/mytemplate.html:
    Code:
    <!DOCTYPE html>
    <html lang="de">
    <body>
    <!--
    these lines are missing in the output
    -->
    Hello World
    </body>
    </html>
    My Handler:


    Code:
    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:

    Code:
    {{"<!-- my comment goes here -->"}}
    But then the tags are escaped as

    Code:
    &lt;!-- foo --&gt;
Working...