How to return html on Gin?

huangapple go评论79阅读模式
英文:

How to return html on Gin?

问题

我正在尝试渲染一个已经存在于字符串中的HTML,而不是在Gin框架上渲染一个模板。

GET("/")函数中,c.HTML函数期望渲染一个模板。

但是在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: 在返回时渲染markdown内容
	})

	router.Run(":5000")
}
英文:

I'm trying to render a HTML that's already on a string instead of rendering a template on Gin framework.

The c.HTML function on GET("/") function expects a template to be rendered.

But on POST("/markdown") I've rendered that HTML on a string already.

How can I return it on 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")
}

答案1

得分: 18

你可以将处理过的 Markdown 字节数组作为 RAW Data 返回,并将 content-type 设置为 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)
})
英文:

You can return the processed markdown byte array as a RAW Data and set content-type as text/html; charset=utf-8

This is how it may look like

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)
	})

答案2

得分: 4

你还可以使用常量来表示内容类型:

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"
)
c.Data(http.StatusOK, ContentTypeHTML, []byte("<html></html>"))
英文:

You can also use constants for content types:

const (
	ContentTypeBinary = &quot;application/octet-stream&quot;
	ContentTypeForm   = &quot;application/x-www-form-urlencoded&quot;
	ContentTypeJSON   = &quot;application/json&quot;
	ContentTypeHTML   = &quot;text/html; charset=utf-8&quot;
	ContentTypeText   = &quot;text/plain; charset=utf-8&quot;
)
c.Data(http.StatusOK, ContentTypeHTML, []byte(&quot;&lt;html&gt;&lt;/html&gt;&quot;))

答案3

得分: 1

blackfriday.MarkdownCommon()的输出转换为template.HTML,如下所示:

markdown := blackfriday.MarkdownCommon([]byte(c.PostForm("body")))
c.HTML(http.StatusOK, "markdown.html", gin.H {
    "markdown": template.HTML(markdown),
})
英文:

Convert the output of blackfriday.MarkdownCommon() to template.HTML like:

markdown := blackfriday.MarkdownCommon([]byte(c.PostForm(&quot;body&quot;)))
c.HTML(http.StatusOK, &quot;markdown.html&quot;, gin.H {
    &quot;markdown&quot;: template.HTML(markdown),
})

huangapple
  • 本文由 发表于 2017年1月5日 19:31:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/41483970.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定