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)
- Can UnmarshalParam support custom slice of string
- With RedirectFixedPath enabled, the request execution panics when a 404 is expected HOT 1
- RunQUIC does not connect via browser HOT 2
- Why do long requests block each other? HOT 3
- time_format tag not work with json tag HOT 1
- gin context with tracing not work HOT 2
- c.Redirect(http.StatusTemporaryRedirect, "") leads to open redirect vulnerability HOT 4
- Retry Middleware II
- How do you hide "TLS handshake errors" messages? HOT 2
- The stream is not response as a HOT 1
- runtime error: makeslice: cap out of range when HandleMethodNotAllowed=true and no request handler registered HOT 1
- Strange, the router is not registered HOT 3
- (gin.Context)File unable to stream to body
- Templates automatically replace escape problems HOT 1
- Load HTML templates from a separate module HOT 1
- CORS issue when trying to access an endpoint from a React application 😱 HOT 2
- ctx.Keys is overwritten by obj in methods like ctx.HTML HOT 2
- Gin SetMode not Passing Value to Function, Cause Panic Error On Minikube/Microk8s HOT 4
- [GIN-debug] [WARNING] Headers were already written. Wanted to override status code 200 with 500 HOT 4
- Advertisement, deleted a bunch of useless dependency packages to speed up compilation
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.