我正在尝试呈现已经在字符串上的 HTML,而不是在 Gin 框架上呈现模板。
在c.HTML对功能GET("/")函数需要一个模板来渲染。
但是POST("/markdown")我已经在字符串上呈现了该 HTML。
我怎样才能在 Gin 上退货?
package main
import (
    "github.com/gin-gonic/gin"
    "github.com/russross/blackfriday"
    "log"
    "net/http"
    "os"
)
func main() {
    router := gin.New()
    router.Use(gin.Logger())
    router.LoadHTMLGlob("templates/*.tmpl.html")
    router.GET("/", func(c *gin.Context) {
        c.HTML(http.StatusOK, "index.tmpl.html", nil)
    })
    router.POST("/markdown", func(c *gin.Context) {
        body := c.PostForm("body")
        log.Println(body)
        markdown := blackfriday.MarkdownCommon([]byte(c.PostForm("body")))
        log.Println(markdown)
        // TODO: render markdown content on return
    })
    router.Run(":5000")
}
Run Code Online (Sandbox Code Playgroud)
    Sar*_*lai 10
您可以将处理后的降价字节数组返回为 aRAW Data并将内容类型设置为text/html; charset=utf-8
这就是它的样子
router.POST("/markdown", func(c *gin.Context) {
        body, ok := c.GetPostForm("body")
        if !ok {
            c.JSON(http.StatusBadRequest, "badrequest")
            return
        }
        markdown := blackfriday.MarkdownCommon([]byte(body))
        c.Data(http.StatusOK, "text/html; charset=utf-8", markdown)
    })
Run Code Online (Sandbox Code Playgroud)
        您还可以使用内容类型常量:
const (
    ContentTypeBinary = "application/octet-stream"
    ContentTypeForm   = "application/x-www-form-urlencoded"
    ContentTypeJSON   = "application/json"
    ContentTypeHTML   = "text/html; charset=utf-8"
    ContentTypeText   = "text/plain; charset=utf-8"
)
Run Code Online (Sandbox Code Playgroud)
c.Data(http.StatusOK, ContentTypeHTML, []byte("<html></html>"))
Run Code Online (Sandbox Code Playgroud)
        |   归档时间:  |  
           
  |  
        
|   查看次数:  |  
           9899 次  |  
        
|   最近记录:  |