Giter VIP home page Giter VIP logo

Comments (10)

pinscript avatar pinscript commented on April 19, 2024 1

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

from gin.

manucorporat avatar manucorporat commented on April 19, 2024 1

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

Check out the developbranch for the latest features

from gin.

muei avatar muei commented on April 19, 2024

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,
})

from gin.

manucorporat avatar manucorporat commented on April 19, 2024

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

from gin.

muei avatar muei commented on April 19, 2024

Really?How to use ?

from gin.

manucorporat avatar manucorporat commented on April 19, 2024

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

from gin.

muei avatar muei commented on April 19, 2024

Thanks a lot!

from gin.

manucorporat avatar manucorporat commented on April 19, 2024

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

from gin.

AielloChan avatar AielloChan commented on April 19, 2024

It doesn't work at context.HTML()

from gin.

kazhuravlev avatar kazhuravlev commented on April 19, 2024

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)

from gin.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.