Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Need example or documentation on how to use a custom render #415

Closed
robvdl opened this issue Aug 26, 2015 · 4 comments
Closed

Need example or documentation on how to use a custom render #415

robvdl opened this issue Aug 26, 2015 · 4 comments

Comments

@robvdl
Copy link

robvdl commented Aug 26, 2015

I am trying to use a custom renderer so I can use pongo2 as a template engine, I have found a commit that added this feature a while back:

5ea7a92#diff-703ef7478e12fbd8acd9d1af87bda71cR72

However, this feature does not appear to be documented and I can't figure out how to get my own custom render to work, I've been trying out this for a while now, but without any luck.

The question was also asked here but no reply yet:

ngerakines/ginpongo2#3

However, I am not really too keen on the ginpongo2 library either, because it:

  • does not use the newer HTMLRender interface added in GIN 1.0
  • loads templates on each request which is not very efficient

Any help would be appreciated in how to write a custom renderer.

@robvdl
Copy link
Author

robvdl commented Aug 26, 2015

I figured it out in the end, though it would be really helpful for others to get this documented for the future. I did something like this, which seems to work:

type Pongo2Renderer struct {
}
func (p Pongo2Renderer) Instance(name string, data interface{}) render.Render {
    return Pongo2Renderer{}
}
func (p Pongo2Renderer) Render(http.ResponseWriter) error {
    fmt.Println("Custom renderer is working")
    return nil
}
r := gin.Default()
r.HTMLRender = Pongo2Renderer{}

I still need to finish it, but posting this here, in case it helps someone else.

@TheRealKira
Copy link

Could you post some response time benchmarks you get using this renderer in your application as apposed to the default one? Whenever I've used the default html/template I get response times like 50ms as apposed to writing a []byte slice to the writer manually where I get < 500 μs. I've always though maybe it was just me, but after running this test on a few of my machines I'm concerned with the html/template package :/

@robvdl
Copy link
Author

robvdl commented Aug 27, 2015

Sure, but I gotta make it work first, I just needed to figure out how to make a custom renderer the "proper" way in Gin, now I just have to make the Gin Pongo2 renderer itself. I can post updates when I have it working.

@robvdl
Copy link
Author

robvdl commented Aug 29, 2015

@TheRealKira I finished that little pongo2 template library for Gin in case you want to try it out: https://github.com/robvdl/pongo2gin

However, 50ms for using html/template doesn't sound right to me, it shouldn't be that slow unless either the template is very complex maybe, or maybe something isn't quite done right like templates being loaded from disk upon each request rather than cached in RAM.

Anyway, for a very simple template with pongo2gin I am getting 1ms for the initial render and < 1ms for subsequent renders of the same template because templates are cached.

However I believe html/template should be similar in speed, I just didn't produce any comparison benchmarks (will leave that upto you), but it somewhat surprises me that Pongo2 should be any faster.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants