Skip to content

Commit

Permalink
fix #1876
Browse files Browse the repository at this point in the history
  • Loading branch information
kataras committed Apr 23, 2022
1 parent 90750d0 commit 677ddd7
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 26 deletions.
1 change: 1 addition & 0 deletions _examples/routing/macros/main.go
Original file line number Diff line number Diff line change
@@ -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 (
Expand Down
4 changes: 2 additions & 2 deletions _examples/view/template_jet_0/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand All @@ -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")

Expand Down
35 changes: 11 additions & 24 deletions context/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand All @@ -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 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 v := ctx.values.Get(ctx.app.ConfigurationReadOnly().GetViewDataContextKey()); v != nil {
// if pure map[string]interface{}
if viewData, ok := v.(Map); ok {
return viewData
}

// if context#Map
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 failure, then return nil
// if no values found, then return nil
return nil
}

Expand Down
15 changes: 15 additions & 0 deletions view/jet.go
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand All @@ -348,6 +362,7 @@ func (s *JetEngine) ExecuteWriter(w io.Writer, filename string, layout string, b
}
}
}

}

if bindingData == nil {
Expand Down

0 comments on commit 677ddd7

Please sign in to comment.