ViewData saves one or more key-value pair in order to be passed if and when .View is being called afterwards, in the same request. Useful when need to set or/and change template data from previous hanadlers in the chain. If .View's "binding" argument is not nil and it's not a type of map then these
(key string, value interface{})
| 3739 | // |
| 3740 | // Example: https://github.com/kataras/iris/tree/main/_examples/view/context-view-data/ |
| 3741 | func (ctx *Context) ViewData(key string, value interface{}) { |
| 3742 | viewDataContextKey := ctx.app.ConfigurationReadOnly().GetViewDataContextKey() |
| 3743 | if key == "" { |
| 3744 | ctx.values.Set(viewDataContextKey, value) |
| 3745 | return |
| 3746 | } |
| 3747 | |
| 3748 | v := ctx.values.Get(viewDataContextKey) |
| 3749 | if v == nil { |
| 3750 | ctx.values.Set(viewDataContextKey, Map{key: value}) |
| 3751 | return |
| 3752 | } |
| 3753 | |
| 3754 | if data, ok := v.(Map); ok { |
| 3755 | data[key] = value |
| 3756 | } |
| 3757 | } |
| 3758 | |
| 3759 | // GetViewData returns the values registered by `context#ViewData`. |
| 3760 | // The return value is `map[string]interface{}`, this means that |