Merge pull request #740 from trheyi/main

chore: Refactor TemplateRender to handle optional request parameters
This commit is contained in:
Max 2024-08-23 12:22:02 +08:00 committed by GitHub
commit 99b78cfe98
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 27 additions and 4 deletions

View file

@ -173,6 +173,28 @@ func TemplateRender(process *process.Process) interface{} {
}
}
r := core.Request{Theme: opt["theme"], Locale: opt["locale"]}
if process.NumOfArgs() > 5 {
raw, err := jsoniter.Marshal(process.Args[5])
if err != nil {
exception.New(err.Error(), 500).Throw()
}
err = jsoniter.Unmarshal(raw, &r)
if err != nil {
exception.New(err.Error(), 500).Throw()
}
if r.Theme == "" {
r.Theme = opt["theme"]
}
if r.Locale == "" {
r.Locale = opt["locale"]
}
}
data := process.ArgsMap(3)
option := core.ParserOption{
Theme: opt["theme"],
@ -183,10 +205,7 @@ func TemplateRender(process *process.Process) interface{} {
Root: root,
Script: nil,
Imports: imports,
Request: &core.Request{
Theme: opt["theme"],
Locale: opt["locale"],
},
Request: &r,
}
parser := core.NewTemplateParser(core.Data(data), &option)

View file

@ -97,6 +97,10 @@ func (script *Script) Call(r *Request, method string, args ...any) (interface{},
return nil, err
}
defer ctx.Close()
if args == nil {
args = []any{}
}
args = append(args, r)
// Set the sid
ctx.Sid = r.Sid