Comments (10)
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.
I think I fixed this issue in this commit: 46225ea
Check out the develop
branch for the latest features
from gin.
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.
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.
Really?How to use ?
from gin.
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:
- http://gold.yoss.si
- ffjson: https://journal.paul.querna.org/articles/2014/03/31/ffjson-faster-json-in-go/
You can change the default HTML render using this code:
r := gin.Default()
r.HTMLRender = your_awesome_render
from gin.
Thanks a lot!
from gin.
I started some work around a development mode for GIN. #96
Ideas are welcome
from gin.
It doesn't work at context.HTML()
from gin.
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)
- Does gin.Recovery() run before or after Group middleware? HOT 1
- How can i prevent a http connection to close HOT 3
- BasicAuth is not suitable for proxy auth
- Can't get status as expected from context.Status() HOT 1
- Gin Context implementation breaks context.Context contract HOT 3
- [Suggestions] Introducing an AI-powered robot to assist with daily issue and pull request (PR) tasks.
- Need c.JSON() response like behaviour for c.SSEvent() response for supporting first party clients like OpenAI SDK HOT 2
- custom time.Time type can not use bindQuery get a value. HOT 3
- configure server parameters HOT 1
- The binding:"required" tag does not seem to work for struct types HOT 3
- Is there a way to preserve order in JSON on response? HOT 1
- redirect is not success HOT 1
- I want to use shouldmindbodywith first and then use formafile, but it doesn't work right now HOT 3
- Extend the routing tree entry
- Transparent decompression for gzip, deflate, etc? HOT 1
- Custom time.Time type can not use bindQuery get a value 2 HOT 2
- http: response.Write on hijacked connection from github.com/gin-gonic/gin.(*responseWriter).Write (response_writer.go:83) HOT 1
- Suggestion: Move Print Debug to Run() functions? HOT 7
- AbortWithStatusJSON doesn't abort context HOT 2
- I translated `tree.go` into java
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from gin.