view: django: accept struct as template data. Rel to: #1683

This commit is contained in:
Gerasimos (Makis) Maropoulos 2020-12-04 12:48:53 +02:00
parent b839b5cdb9
commit 3e86301b7a
No known key found for this signature in database
GPG Key ID: 5DBE766BD26A54E7
3 changed files with 17 additions and 12 deletions

6
NOTICE
View File

@ -101,9 +101,9 @@ Revision ID: 5fc50a00491616d5cd0cbce3abd8b699838e25ca
toml 3012a1dbe2e4bd1 https://github.com/BurntSushi/toml toml 3012a1dbe2e4bd1 https://github.com/BurntSushi/toml
391d42b32f0577c 391d42b32f0577c
b7bbc7f005 b7bbc7f005
jwt e87a84e0681e206 https://github.com/kataras/jwt jwt 933b4a74659b074 https://github.com/kataras/jwt
f3733932450e25d 00070920d0700b9
7747daf215 63fa545d6c
uuid cb32006e483f2a2 https://github.com/google/uuid uuid cb32006e483f2a2 https://github.com/google/uuid
3230e24209cf185 3230e24209cf185
c65b477dbf c65b477dbf

4
go.mod
View File

@ -11,7 +11,7 @@ require (
github.com/dgraph-io/badger/v2 v2.2007.2 github.com/dgraph-io/badger/v2 v2.2007.2
github.com/eknkc/amber v0.0.0-20171010120322-cdade1c07385 github.com/eknkc/amber v0.0.0-20171010120322-cdade1c07385
github.com/fatih/structs v1.1.0 github.com/fatih/structs v1.1.0
github.com/flosch/pongo2/v4 v4.0.0 github.com/flosch/pongo2/v4 v4.0.1
github.com/go-redis/redis/v8 v8.4.0 github.com/go-redis/redis/v8 v8.4.0
github.com/google/uuid v1.1.2 github.com/google/uuid v1.1.2
github.com/hashicorp/go-version v1.2.1 github.com/hashicorp/go-version v1.2.1
@ -21,7 +21,7 @@ require (
github.com/json-iterator/go v1.1.10 github.com/json-iterator/go v1.1.10
github.com/kataras/blocks v0.0.4 github.com/kataras/blocks v0.0.4
github.com/kataras/golog v0.1.5 github.com/kataras/golog v0.1.5
github.com/kataras/jwt v0.0.6 github.com/kataras/jwt v0.0.7
github.com/kataras/neffos v0.0.18 github.com/kataras/neffos v0.0.18
github.com/kataras/pio v0.0.10 github.com/kataras/pio v0.0.10
github.com/kataras/sitemap v0.0.5 github.com/kataras/sitemap v0.0.5

View File

@ -12,6 +12,7 @@ import (
"github.com/kataras/iris/v12/context" "github.com/kataras/iris/v12/context"
"github.com/fatih/structs"
"github.com/flosch/pongo2/v4" "github.com/flosch/pongo2/v4"
) )
@ -261,15 +262,19 @@ func getPongoContext(templateData interface{}) pongo2.Context {
return nil return nil
} }
if contextData, isPongoContext := templateData.(pongo2.Context); isPongoContext { switch data := templateData.(type) {
return contextData case pongo2.Context:
return data
case context.Map:
return pongo2.Context(data)
default:
// if struct, convert it to map[string]interface{}
if structs.IsStruct(data) {
return pongo2.Context(structs.Map(data))
} }
if contextData, isContextViewData := templateData.(context.Map); isContextViewData { panic("django: template data: should be a map or struct")
return pongo2.Context(contextData)
} }
return templateData.(map[string]interface{})
} }
func (s *DjangoEngine) fromCache(relativeName string) *pongo2.Template { func (s *DjangoEngine) fromCache(relativeName string) *pongo2.Template {