+ add custom render test(TODO)

+ add custom render in readme.md
This commit is contained in:
SCys 2015-11-09 00:41:31 +08:00
parent 36dce1a73f
commit 4c20e9de09
2 changed files with 84 additions and 0 deletions

View File

@ -459,6 +459,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
Issuing a HTTP redirect is easy:

View File

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