diff --git a/_examples/routing/macros/main.go b/_examples/routing/macros/main.go index 3a41b646..2a1aafca 100644 --- a/_examples/routing/macros/main.go +++ b/_examples/routing/macros/main.go @@ -1,4 +1,5 @@ // 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 import ( diff --git a/_examples/view/template_jet_0/main.go b/_examples/view/template_jet_0/main.go index a7cb43c8..1ab0af19 100644 --- a/_examples/view/template_jet_0/main.go +++ b/_examples/view/template_jet_0/main.go @@ -53,7 +53,7 @@ func (dt *doneTODOs) Range() (reflect.Value, reflect.Value, bool) { func (dt *doneTODOs) ProvidesIndex() bool { return true } func (dt *doneTODOs) Render(r *view.JetRuntime) { - r.Write([]byte(fmt.Sprintf("custom renderer"))) + r.Write([]byte("custom renderer")) } // Render implements jet.Renderer interface @@ -112,6 +112,7 @@ func main() { return } + ctx.ViewData("title", "Show TODO") ctx.View("todos/show.jet", todo) }) app.Get("/all-done", func(ctx iris.Context) { @@ -122,7 +123,6 @@ func main() { // ctx.View("todos/index.jet", (&doneTODOs{}).New(todos)) // // OR - ctx.ViewData("showingAllDone", true) ctx.ViewData("title", "Todos - All Done") diff --git a/context/context.go b/context/context.go index af8cde26..7f2a6512 100644 --- a/context/context.go +++ b/context/context.go @@ -3450,9 +3450,7 @@ func (ctx *Context) ViewData(key string, value interface{}) { return } - if data, ok := v.(map[string]interface{}); ok { - data[key] = value - } else if data, ok := v.(Map); ok { + if data, ok := v.(Map); ok { 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 // `viewData := ctx.Values().Get(ctx.Application().ConfigurationReadOnly().GetViewDataContextKey())`. func (ctx *Context) GetViewData() map[string]interface{} { - viewDataContextKey := ctx.app.ConfigurationReadOnly().GetViewDataContextKey() - v := ctx.values.Get(viewDataContextKey) + if v := ctx.values.Get(ctx.app.ConfigurationReadOnly().GetViewDataContextKey()); v != nil { + // 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 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 } diff --git a/view/jet.go b/view/jet.go index 365702d7..13d06a58 100644 --- a/view/jet.go +++ b/view/jet.go @@ -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 bindingData == nil { // 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 {