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

How to turn off caching in development mode? #61

Closed
muei opened this issue Jul 9, 2014 · 10 comments
Closed

How to turn off caching in development mode? #61

muei opened this issue Jul 9, 2014 · 10 comments

Comments

@muei
Copy link

muei commented Jul 9, 2014

Now ,when I update the ".tmpl" files, I can't see the latest changes

@pinscript
Copy link
Contributor

I assume your code looks something like this:

r := gin.Default()
r.LoadHTMLTemplates("*.tmpl")

r.GET("/test", func(c *gin.Context) {
    c.HTML(200, "index.tmpl", "")
})

r.Run(":8080")

As you can see, the call to LoadHTMLTemplates is invoked only when the program starts. Hence the templates is only loaded once.

One solution is to put the call to LoadHTMLTemplates inside the route function. This will reload the templates on every request.

r.GET("/test", func(c *gin.Context) {
    r.LoadHTMLTemplates("*.tmpl")
    c.HTML(200, "index.tmpl", "")
})

However, this may not be what you want in production. There is, unfortunately with the same name, a library[0] that enables live reload. I have not experimented with it yet so I cannot say if it works.

[0] https://github.com/codegangsta/gin

@muei
Copy link
Author

muei commented Jul 9, 2014

Yeah! Whether can draw lessons from this example:
https://github.com/unrolled/render
I tried to try the library with https://github.com/codegangsta/negroni,like this:

r := render.New(render.Options{
    Delims:        render.Delims{"[[", "]]"},
    IsDevelopment: true,
})

@manucorporat
Copy link
Contributor

The new flexible render system allows us to easily change the default HTML render.
We could create a debugHTML render or something like that.

@muei
Copy link
Author

muei commented Jul 16, 2014

Really?How to use ?

@manucorporat
Copy link
Contributor

Now:

c.JSON(200, data)

is the same as:

c.Render(200, render.JSON, data)

render.JSON is a instance that responds to the interface render.Render, it is a very simple interface:

type Render interface {
        Render(http.ResponseWriter, int, ...interface{}) error
}

so you can create your own struct that implements Render() and you can use it in c.Render().
c.HTML() uses the default HTML render in the Engine class: https://github.com/gin-gonic/gin/blob/develop/gin.go#L74

This new feature also allows us to change the default HTML render or even the default JSON/XML render! we could easily use solutions such as:

You can change the default HTML render using this code:

r := gin.Default()
r.HTMLRender = your_awesome_render

@muei
Copy link
Author

muei commented Jul 16, 2014

Thanks a lot!

@manucorporat
Copy link
Contributor

I started some work around a development mode for GIN. #96
Ideas are welcome

@manucorporat
Copy link
Contributor

I think I fixed this issue in this commit: 46225ea

Check out the developbranch for the latest features

@AielloChan
Copy link

It doesn't work at context.HTML()

@kazhuravlev
Copy link

kazhuravlev commented Apr 11, 2020

gin has special method for development mode. See this example:

// enable debug mode. Is this mode is set - gin will use HTMLDebugRenderer 
// which load template on each call
gin.SetMode(gin.DebugMode)

router := gin.Default()
router.LoadHTMLGlob("templates/*")
router.GET("/index", s.handleIndex)

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

5 participants