golang web框架gin 字符串转换html

2020-04-14  本文已影响0人  南苑bert
package main

import (
    "html/template"

    "github.com/gin-gonic/gin"
)

func main() {
    router := gin.Default()
    router.SetFuncMap(template.FuncMap{
        "safe": func(str string) template.HTML {
            return template.HTML(str)
        },
    })

    router.LoadHTMLFiles("./index.tmpl")
    router.GET("/index", func(c *gin.Context) {
        c.HTML(200, "index.tmpl", "<a href='lizhouwen.com'>1232</a>")
    })

    router.Run(":17111")
}

index.tmpl

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <title>bert</title>
</head>
<body>
<h1>2134</h1>
<div>{{ . | safe }}</div>
</body>
</html>
image.png
上一篇下一篇

猜你喜欢

热点阅读