This commit is contained in:
Gerasimos (Makis) Maropoulos 2022-04-23 13:18:54 +03:00
parent 90750d089d
commit 677ddd7539
No known key found for this signature in database
GPG Key ID: 66FCC29BD385FCA6
4 changed files with 30 additions and 27 deletions

View File

@ -1,4 +1,5 @@
// Package main shows how you can register a custom parameter type and macro functions that belongs to it. // Package main shows how you can register a custom parameter type and macro functions that belongs to it.
// See _examples/routing/dynamic-path/main.go first.
package main package main
import ( import (

View File

@ -53,7 +53,7 @@ func (dt *doneTODOs) Range() (reflect.Value, reflect.Value, bool) {
func (dt *doneTODOs) ProvidesIndex() bool { return true } func (dt *doneTODOs) ProvidesIndex() bool { return true }
func (dt *doneTODOs) Render(r *view.JetRuntime) { func (dt *doneTODOs) Render(r *view.JetRuntime) {
r.Write([]byte(fmt.Sprintf("custom renderer"))) r.Write([]byte("custom renderer"))
} }
// Render implements jet.Renderer interface // Render implements jet.Renderer interface
@ -112,6 +112,7 @@ func main() {
return return
} }
ctx.ViewData("title", "Show TODO")
ctx.View("todos/show.jet", todo) ctx.View("todos/show.jet", todo)
}) })
app.Get("/all-done", func(ctx iris.Context) { app.Get("/all-done", func(ctx iris.Context) {
@ -122,7 +123,6 @@ func main() {
// ctx.View("todos/index.jet", (&doneTODOs{}).New(todos)) // ctx.View("todos/index.jet", (&doneTODOs{}).New(todos))
// //
// OR // OR
ctx.ViewData("showingAllDone", true) ctx.ViewData("showingAllDone", true)
ctx.ViewData("title", "Todos - All Done") ctx.ViewData("title", "Todos - All Done")

View File

@ -3450,9 +3450,7 @@ func (ctx *Context) ViewData(key string, value interface{}) {
return return
} }
if data, ok := v.(map[string]interface{}); ok { if data, ok := v.(Map); ok {
data[key] = value
} else if data, ok := v.(Map); ok {
data[key] = value data[key] = value
} }
} }
@ -3467,30 +3465,19 @@ func (ctx *Context) ViewData(key string, value interface{}) {
// Similarly to `viewData := ctx.Values().Get("iris.view.data")` or // Similarly to `viewData := ctx.Values().Get("iris.view.data")` or
// `viewData := ctx.Values().Get(ctx.Application().ConfigurationReadOnly().GetViewDataContextKey())`. // `viewData := ctx.Values().Get(ctx.Application().ConfigurationReadOnly().GetViewDataContextKey())`.
func (ctx *Context) GetViewData() map[string]interface{} { func (ctx *Context) GetViewData() map[string]interface{} {
viewDataContextKey := ctx.app.ConfigurationReadOnly().GetViewDataContextKey() if v := ctx.values.Get(ctx.app.ConfigurationReadOnly().GetViewDataContextKey()); v != nil {
v := ctx.values.Get(viewDataContextKey) // if pure map[string]interface{}
if viewData, ok := v.(Map); ok {
return viewData
}
// if struct, convert it to map[string]interface{}
if structs.IsStruct(v) {
return structs.Map(v)
}
}
// if no values found, then return nil // if no values found, then return nil
if v == nil {
return nil
}
// if struct, convert it to map[string]interface{}
if structs.IsStruct(v) {
return structs.Map(v)
}
// if pure map[string]interface{}
if viewData, ok := v.(map[string]interface{}); ok {
return viewData
}
// if context#Map
if viewData, ok := v.(Map); ok {
return viewData
}
// if failure, then return nil
return nil return nil
} }

View File

@ -334,6 +334,20 @@ func (s *JetEngine) ExecuteWriter(w io.Writer, filename string, layout string, b
} }
} }
if viewContextData := ctx.GetViewData(); len(viewContextData) > 0 { // fix #1876
if vars == nil {
vars = make(JetRuntimeVars)
}
for k, v := range viewContextData {
val, ok := v.(reflect.Value)
if !ok {
val = reflect.ValueOf(v)
}
vars[k] = val
}
}
if v := ctx.Values().Get(s.jetDataContextKey); v != nil { if v := ctx.Values().Get(s.jetDataContextKey); v != nil {
if bindingData == nil { if bindingData == nil {
// if bindingData is nil, try to fill them by context key (a middleware can set data). // if bindingData is nil, try to fill them by context key (a middleware can set data).
@ -348,6 +362,7 @@ func (s *JetEngine) ExecuteWriter(w io.Writer, filename string, layout string, b
} }
} }
} }
} }
if bindingData == nil { if bindingData == nil {