These are chat archives for gin-gonic/gin

23rd
May 2015
Oleg Lebedev
@olebedev
May 23 2015 15:48
@manucorporat why c.Engine was hidden in v1?
I am using it to render React app, it really need to test server app or make requests to server app instance directly programmatically. What is the reason?
Manu MA
@manucorporat
May 23 2015 18:01
@olebedev the type Engine has not been hidden. do you read the Engine from the Context?
@olebedev
router.GET("/path", func(c *gin.Context) {
    c.Engine // << you can not do this
})
I have reviewed your code, and you do not do that... so don't worry :)
Joonathan Mägi
@joonathan
May 23 2015 21:01
So one should not do: c.Engine.SetHTMLTemplate(template.Must(template.ParseFiles("templates/base.tmpl", "templates/view.tmpl"))) in 1.0?
or based on "[FIX] Context.Engine renamed to Context.engine" i should just go for c.engine.SetHTMLTemplate?
Manu MA
@manucorporat
May 23 2015 21:09
@joonathan I removed c.Engine exactly because of that! c.Engine.SetHTMLTemplate is a horrible idea. You are modifying a global variable without a lock, a race condition could happen in any moment
router.SetHTMLTemplate() should be set at the beginning just once.
why anybody would need to change the template?
what do you want to do?
Joonathan Mägi
@joonathan
May 23 2015 21:20
stupid shortcut to keep ParseFiles to the view func. I'll just prep these ahead and just use c.HTML in the view
And yeah, that c.Engine removal makes perfect sense
Manu MA
@manucorporat
May 23 2015 21:33
yeah! you should prep these ahead, or use template.ExecuteTemplate() directly
or...
c.Render(200, render.HTML {
    Template: template.Must(template.ParseFiles("templates/base.tmpl", "templates/view.tmpl")),
    Name: "base.tmpl",
    Data: yourData,
})
Joonathan Mägi
@joonathan
May 23 2015 21:34
ah, that's a nice trick, thanks
Manu MA
@manucorporat
May 23 2015 21:35
but, since parsing the template files is a very expensive operation, you should do that ahead!
when running in development, Gin automatically reloads the templates files if they change
so you do not have to restart the server
Joonathan Mägi
@joonathan
May 23 2015 21:36
roger that, thanks