Merge af3876c120018648141b2816dcedead2d8369b3a into 61fae4997db3dc0011e9015f4f69f57c81e9ce4a

This commit is contained in:
Chen.Yanshen 2016-01-27 02:20:16 +00:00
commit a83d44fbfc
5 changed files with 141 additions and 8 deletions

View File

@ -503,6 +503,86 @@ func main() {
``` ```
#### Custom rendering
Custom your template engine use SetCustomRenderFunc()
```go
package main
import (
"github.com/flosch/pongo2"
"github.com/gin-gonic/gin"
"net/http"
"errors"
)
func CustomRenderFunc(w http.ResponseWriter, name string, obj interface{}) error {
var err error
tpl, err := pongo2.FromFile(name)
if err != nil {
return err
}
if obj == nil {
err = tpl.ExecuteWriter(nil, w)
} else {
if data,ok := obj.(gin.H); ok {
err = tpl.ExecuteWriter(pongo2.Context(data), w)
} else if data,ok := obj.(pongo2.Context); ok {
err = tpl.ExecuteWriter(data, w)
} else {
return errors.New("invalid obj type")
}
}
return err
}
func main() {
router := gin.Default()
pongo2.DefaultLoader.SetBaseDir("templates")
router.SetCustomRenderFunc(CustomRenderFunc)
router.GET("/", func(c *gin.Context) {
c.CustomRender(200, "index.html", nil)
})
router.GET("/msg1", func(c *gin.Context) {
c.CustomRender(200, "message.html", pongo2.Context{
"message": c.Query("name"),
})
})
router.GET("/msg2", func(c *gin.Context) {
c.CustomRender(200, "message.html", gin.H{
"message": c.Query("name"),
})
})
router.Run("[::]:8888")
}
```
templates/index.html
```html
<html><h1>
hi
</h1>
</html>
```
templates/message.html
```html
<html><h1>
hi {{ message }}
</h1>
</html>
```
#### Redirects #### Redirects
Issuing a HTTP redirect is easy: Issuing a HTTP redirect is easy:

View File

@ -383,6 +383,11 @@ func (c *Context) HTML(code int, name string, obj interface{}) {
c.Render(code, instance) c.Render(code, instance)
} }
func (c *Context) CustomRender(code int, name string, obj interface{}) {
instance := c.engine.CustomRender.Instance(name, obj)
c.Render(code, instance)
}
// IndentedJSON serializes the given struct as pretty JSON (indented + endlines) into the response body. // IndentedJSON serializes the given struct as pretty JSON (indented + endlines) into the response body.
// It also sets the Content-Type as "application/json". // It also sets the Content-Type as "application/json".
// WARNING: we recommend to use this only for development propuses since printing pretty JSON is // WARNING: we recommend to use this only for development propuses since printing pretty JSON is

21
gin.go
View File

@ -11,7 +11,7 @@ import (
"os" "os"
"sync" "sync"
"github.com/gin-gonic/gin/render" "github.com/SCys/gin/render"
) )
// Framework's version // Framework's version
@ -44,13 +44,14 @@ type (
// Create an instance of Engine, by using New() or Default() // Create an instance of Engine, by using New() or Default()
Engine struct { Engine struct {
RouterGroup RouterGroup
HTMLRender render.HTMLRender HTMLRender render.HTMLRender
allNoRoute HandlersChain CustomRender render.CustomRender
allNoMethod HandlersChain allNoRoute HandlersChain
noRoute HandlersChain allNoMethod HandlersChain
noMethod HandlersChain noRoute HandlersChain
pool sync.Pool noMethod HandlersChain
trees methodTrees pool sync.Pool
trees methodTrees
// Enables automatic redirection if the current route can't be matched but a // Enables automatic redirection if the current route can't be matched but a
// handler for the path with (without) the trailing slash exists. // handler for the path with (without) the trailing slash exists.
@ -147,6 +148,10 @@ func (engine *Engine) SetHTMLTemplate(templ *template.Template) {
engine.HTMLRender = render.HTMLProduction{Template: templ} engine.HTMLRender = render.HTMLProduction{Template: templ}
} }
func (engine *Engine) SetCustomRenderFunc(f render.CustomRenderFunc) {
engine.CustomRender = render.CustomRenderProduction{f}
}
// Adds handlers for NoRoute. It return a 404 code by default. // Adds handlers for NoRoute. It return a 404 code by default.
func (engine *Engine) NoRoute(handlers ...HandlerFunc) { func (engine *Engine) NoRoute(handlers ...HandlerFunc) {
engine.noRoute = handlers engine.noRoute = handlers

39
render/custom_render.go Normal file
View File

@ -0,0 +1,39 @@
// Copyright 2014 Manu Martinez-Almeida. All rights reserved.
// Use of this source code is governed by a MIT style
// license that can be found in the LICENSE file.
package render
import (
"net/http"
)
type (
CustomRenderFunc func(http.ResponseWriter, string, interface{}) error
CustomRender interface {
Instance(string, interface{}) Render
}
CustomRenderProduction struct {
RenderFunc CustomRenderFunc
}
Custom struct {
RenderFunc CustomRenderFunc
Name string
Data interface{}
}
)
func (r CustomRenderProduction) Instance(name string, data interface{}) Render {
return Custom{
RenderFunc: r.RenderFunc,
Name: name,
Data: data,
}
}
func (r Custom) Render(w http.ResponseWriter) error {
return r.RenderFunc(w, r.Name, r.Data)
}

View File

@ -128,3 +128,7 @@ func TestRenderHTMLTemplate(t *testing.T) {
assert.Equal(t, w.Body.String(), "Hello alexandernyquist") assert.Equal(t, w.Body.String(), "Hello alexandernyquist")
assert.Equal(t, w.Header().Get("Content-Type"), "text/html; charset=utf-8") assert.Equal(t, w.Header().Get("Content-Type"), "text/html; charset=utf-8")
} }
func TestRenderCustom(t *testing.T) {
// TODO
}