fis*_*ker 3 go go-html-template
我已经创建了这个非常简单的测试程序。
package main
import (
"fmt"
"github.com/microcosm-cc/bluemonday"
"github.com/pressly/chi"
"github.com/russross/blackfriday"
"github.com/unrolled/render"
"net/http"
)
func main() {
r := chi.NewRouter()
r.Get("/", homepageGET)
http.ListenAndServe(":8080", r)
}
func homepageGET(w http.ResponseWriter, r *http.Request) {
Renderer := render.New(render.Options{
Directory: "frontend",
Extensions: []string{".tmpl", ".html"},
UnEscapeHTML: true,
})
unsafe := blackfriday.MarkdownCommon([]byte("**bolded text**"))
markdownContent := bluemonday.UGCPolicy().SanitizeBytes(unsafe)
fmt.Print(string(markdownContent))
Renderer.HTML(w, http.StatusOK, "index", map[string]interface{}{
"content": fmt.Sprintf(string(markdownContent))})
}
Run Code Online (Sandbox Code Playgroud)
然后我有一个 HTML 文件,除了:
<body>
{{ .content }}
</body>
Run Code Online (Sandbox Code Playgroud)
fmt.Print 命令打印“ <p><strong>bolded text</strong></p>”,而它被插入到 HTML 页面中为:“ <p><strong>bolded text</strong></p>”。
我相信它与转义的 HTML 相关,但对于展开/渲染包,我将其配置为未转义..我非常感谢任何帮助使测试程序正常工作(最好与展开/渲染一起)。
在 Go 中,您可以将已知的安全 html 字符串转换为template.HTML类型,并且由于unrolled/render使用 Gohtml/template来呈现 html,因此您应该能够使用它。
Renderer.HTML(w, http.StatusOK, "index", map[string]interface{}{
"content": template.HTML(markdownContent),
})
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3397 次 |
| 最近记录: |