minor: fix Context.ViewData ignored when nil is passed as view data on Context.View method

This commit is contained in:
Gerasimos (Makis) Maropoulos 2020-08-30 16:14:32 +03:00
parent 600eb645df
commit 6ee69ffd1c
No known key found for this signature in database
GPG Key ID: 5DBE766BD26A54E7
2 changed files with 17 additions and 1 deletions

View File

@ -4,6 +4,7 @@ import "github.com/kataras/iris/v12"
func main() {
app := iris.New()
app.Logger().SetLevel("debug")
// Init the handlebars engine
engine := iris.Handlebars("./templates", ".html").Reload(true)
@ -27,6 +28,21 @@ func main() {
ctx.View("example.html", viewData)
})
/* See context-view-data example: Set data through one or more middleware
app.Get("/view_data", func(ctx iris.Context) {
ctx.ViewData("author", map[string]string{"firstName": "Jean", "lastName": "Valjean"})
ctx.ViewData("body", "Life is difficult")
ctx.ViewData("comments", []iris.Map{{
"author": map[string]string{"firstName": "Marcel", "lastName": "Beliveau"},
"body": "LOL!",
}})
ctx.Next()
}, func(ctx iris.Context) {
ctx.View("example.html")
})
*/
// Read more about its syntax at:
// https://github.com/aymerick/raymond and
// https://handlebarsjs.com/guide

View File

@ -2792,7 +2792,7 @@ func (ctx *Context) View(filename string, optionalViewModel ...interface{}) erro
layout := ctx.values.GetString(cfg.GetViewLayoutContextKey())
var bindingData interface{}
if len(optionalViewModel) > 0 {
if len(optionalViewModel) > 0 && optionalViewModel[0] != nil {
// a nil can override the existing data or model sent by `ViewData`.
bindingData = optionalViewModel[0]
} else {