简体   繁体   English

如何使用模板引擎中内置的 Gin Framwork 渲染从数据库中检索到的 HTML?

[英]How to render HTML retrived from DB, using Gin Framwork built in Template engine?

A bit over my head here, and the for the life me I could not find suitable solution to my usage case scenario.这里有点让我头疼,而且我一生都找不到适合我的用例场景的解决方案。

I'm currently working on a project where in some scenario, when a user visit a certain page "Using Gin Framework Templates", part of the page content is rendered via data retrieved from the DB.我目前正在做一个项目,在某些情况下,当用户访问某个页面“使用 Gin 框架模板”时,部分页面内容是通过从数据库检索的数据呈现的。

The issue with this approach is that Gin dose not seam to provide the ability to allow me to render any content Without fully escaping it.这种方法的问题是 Gin 没有提供让我在没有完全 escaping 的情况下呈现任何内容的能力。

I have developed previously with "PHP Framework", and used its template engine "Balde" I was able to if the need arises-ed to have the option to render HTML directly without escaping by using the following Directive "{.! $variableName !!}".我以前使用“PHP Framework”开发过,并使用它的模板引擎“Balde”,如果需要,我可以选择直接渲染 HTML 而无需使用以下指令“{.! $variableName ! !}”。

But in Using Gin, I was not successful into finding any builtin template directive that would allow me to render retrieved HTML content directly from DB.但是在使用 Gin 中,我没有成功找到任何允许我直接从 DB 呈现检索到的 HTML 内容的内置模板指令。

Hope if anyone could support or provide solution or direction on how to solve this issue.希望是否有人可以支持或提供有关如何解决此问题的解决方案或指导。

The following is a could sample and which results I get and which results I hope to get.以下是一个示例以及我得到的结果以及我希望得到的结果。

The following is a quick example of my current issue:以下是我当前问题的一个简单示例:

router := gin.Default()

router.LoadHTMLGlob("templates/*")

router.GET("/", func(c *gin.Context) {
    db, err := gorm.Open(mysql.Open(configs.DB_Connection()), &gorm.Config{})

    if err != nil {
       panic("failed to connect database")
    }

    c.HTML(http.StatusOK, "index.html", gin.H{
        "title": "<h1>Hello World</h1>",
    })
})

Using the builtin template engine in Gin, I get the following results:使用 Gin 中的内置模板引擎,我得到以下结果:

<h1>Hello World</h1>

What I'm hoping to get is:我希望得到的是:

Hello World你好世界

Please note that I'm getting that HTML directly from the DB.请注意,我直接从数据库中获取 HTML。

Go has a builtin library for rendering html template but it's syntax might be a little different then others like blade, mustache. Go 有一个用于渲染html 模板的内置库,但它的语法可能与刀片、小胡子等其他语法略有不同。

I have managed to find a solution to my use case scenario, based on the following Thread , and my own adjustments to make work with GORM as well.基于以下Thread以及我自己的调整以与 GORM 一起工作,我已经设法找到适合我的用例场景的解决方案。

Solution is:解决方法是:

create the following function:创建以下 function:

func getStructFeild(v *migrations.TableName, field string) string {
     r := reflect.ValueOf(v)
    f := reflect.Indirect(r).FieldByName(field).String()
    return f
}


router := gin.Default()

router.LoadHTMLGlob("templates/*")

router.GET("/", func(c *gin.Context) {
  db, err := gorm.Open(mysql.Open(configs.DB_Connection()), &gorm.Config{})

  if err != nil {
     panic("failed to connect database")
  }

    var table migrations.TableName
    db.Where("id = ?", 1).Select("column_name").Find(&table)

   c.HTML(http.StatusOK, "index.html", gin.H{
     "title": template.HTML(getStructFeild(&table, "ModalStructColumnName")),
   })
 })

and that's it Gin Template is now able fully render the HTML that is retrieved from the DB.就是这样,Gin 模板现在能够完全呈现从数据库中检索到的 HTML。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM