0

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.

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.

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 !!}".

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.

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:

<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.

A.Aly
  • 21
  • 2
  • Have you tried: `"title": template.HTML("

    Hello World

    ")`? Where `template.HTML` is the following: https://pkg.go.dev/html/template@go1.19.4#HTML
    – mkopriva Dec 26 '22 at 08:06
  • Ya I did but issue it works if you inserted the HTML directly but If I retrived the HTML from from DB via GORM for example, it fails to render the data, even thought it is just HTML,. – A.Aly Dec 26 '22 at 08:42
  • I have also attempted to change the returned data type and type cast it to a string, but I was not successful, the returned data type is "migration.Modalname" since I'm using Gorm for DB manipulation, but I'm unable to change it's data type. – A.Aly Dec 26 '22 at 08:45
  • @A.Aly could you provide an example code snippet? – Shahriar Ahmed Dec 26 '22 at 08:46
  • @ShahriarAhmed sure, but what more sample/code snippet do you wish me to share, as i had already shared an example of the code I'm using in the original post. – A.Aly Dec 26 '22 at 08:51
  • Issue Have been solved, I have managed to find a working solution for the issue based on answer on a combination of another thread, in addition to my extra solution adjustments on end to suite my use case scenario, I will share it shortly – A.Aly Dec 26 '22 at 09:14

2 Answers2

2

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.

Solution is:

create the following 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.

A.Aly
  • 21
  • 2
0

Go has a builtin library for rendering html template but it's syntax might be a little different then others like blade, mustache.

Shahriar Ahmed
  • 502
  • 4
  • 11
  • You can also use [goview](https://github.com/foolin/goview) which under the hood uses builtin html template. – Shahriar Ahmed Dec 26 '22 at 08:25
  • I will try to give it a try, instead of having to relay on Gin for templating, hopefully I can find a solution. Thanks – A.Aly Dec 26 '22 at 08:47
  • Issue Have been solved, I have managed to find a working solution for the issue based on answer on a combination of another thread, in addition to my extra solution adjustments on end to suite my use case scenario, I will share it shortly. – A.Aly Dec 26 '22 at 09:13