Gin: How to insert html code into templates

Created on 29 Jul 2017  路  1Comment  路  Source: gin-gonic/gin

<div>
{{.content}}
</div>
c.HTML(http.StatusOK, "index.tmpl", gin.H{
        "content":        "<p><li>six</li></p>"
    })

I need the 'content' is not be as a text , but is be as a html code to be rendered

question

Most helpful comment

@takfate the issue is related with html/template, not gin, but here is your solution:

https://play.golang.org/p/_MP_YWpeH1 (runnable)

package main

import (
    "html/template"
    "os"
)

func main() {
    const tpl = `
<body>

    {{.Escaped}}

    {{.NotEscaped}}

</body>
    `

    t, _ := template.New("webpage").Parse(tpl)

    data := struct {
        Escaped string
        NotEscaped template.HTML
    }{
        Escaped: "<b>escaped<b>",
        NotEscaped: "<b>not escaped</b>",
    }

    _ = t.Execute(os.Stdout, data)


}

check https://golang.org/pkg/html/template/#hdr-Typed_Strings for more docs about html/template

>All comments

@takfate the issue is related with html/template, not gin, but here is your solution:

https://play.golang.org/p/_MP_YWpeH1 (runnable)

package main

import (
    "html/template"
    "os"
)

func main() {
    const tpl = `
<body>

    {{.Escaped}}

    {{.NotEscaped}}

</body>
    `

    t, _ := template.New("webpage").Parse(tpl)

    data := struct {
        Escaped string
        NotEscaped template.HTML
    }{
        Escaped: "<b>escaped<b>",
        NotEscaped: "<b>not escaped</b>",
    }

    _ = t.Execute(os.Stdout, data)


}

check https://golang.org/pkg/html/template/#hdr-Typed_Strings for more docs about html/template

Was this page helpful?
0 / 5 - 0 ratings

Related issues

mdnight picture mdnight  路  3Comments

frederikhors picture frederikhors  路  3Comments

sofish picture sofish  路  3Comments

nxvl picture nxvl  路  3Comments

rawoke083 picture rawoke083  路  3Comments