Improve SUI TemplateRender
This commit is contained in:
parent
0f6bc06172
commit
ce96d89c31
1 changed files with 33 additions and 7 deletions
|
|
@ -2,8 +2,10 @@ package api
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"path/filepath"
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
|
jsoniter "github.com/json-iterator/go"
|
||||||
"github.com/yaoapp/gou/process"
|
"github.com/yaoapp/gou/process"
|
||||||
"github.com/yaoapp/kun/exception"
|
"github.com/yaoapp/kun/exception"
|
||||||
"github.com/yaoapp/yao/sui/core"
|
"github.com/yaoapp/yao/sui/core"
|
||||||
|
|
@ -127,29 +129,50 @@ func TemplateRender(process *process.Process) interface{} {
|
||||||
}
|
}
|
||||||
|
|
||||||
opt := process.ArgsMap(4, map[string]interface{}{})
|
opt := process.ArgsMap(4, map[string]interface{}{})
|
||||||
params, ok := opt["params"].(map[string]interface{})
|
buildOptionData, ok := opt["data"].(map[string]interface{})
|
||||||
if !ok {
|
if !ok {
|
||||||
params = map[string]interface{}{}
|
buildOptionData = map[string]interface{}{}
|
||||||
}
|
}
|
||||||
|
|
||||||
root, err := sui.PublicRoot(params)
|
root, err := sui.PublicRoot(buildOptionData)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
exception.New(err.Error(), 500).Throw()
|
exception.New(err.Error(), 500).Throw()
|
||||||
}
|
}
|
||||||
|
assetRoot := filepath.Join(root, "assets")
|
||||||
|
|
||||||
source := process.ArgsString(2)
|
source := process.ArgsString(2)
|
||||||
page := tmpl.CreatePage(source)
|
page := tmpl.CreatePage(source)
|
||||||
route := page.Get().Route
|
route := page.Get().Route
|
||||||
doc, _, err := page.Get().Build(core.NewBuildContext(nil), &core.BuildOption{
|
globalCtx := core.NewGlobalBuildContext()
|
||||||
|
suicode, _, err := page.Get().CompileAsComponent(core.NewBuildContext(globalCtx), &core.BuildOption{
|
||||||
PublicRoot: root,
|
PublicRoot: root,
|
||||||
|
AssetRoot: assetRoot,
|
||||||
IgnoreDocument: true,
|
IgnoreDocument: true,
|
||||||
JitMode: true,
|
Data: buildOptionData,
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
exception.New(err.Error(), 500).Throw()
|
exception.New(err.Error(), 500).Throw()
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
doc, err := core.NewDocument([]byte(suicode))
|
||||||
|
if err != nil {
|
||||||
|
exception.New(err.Error(), 500).Throw()
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
var imports map[string]string
|
||||||
|
importsSel := doc.Find("script[name=imports]")
|
||||||
|
if importsSel != nil && importsSel.Length() > 0 {
|
||||||
|
importsRaw := importsSel.Text()
|
||||||
|
importsSel.Remove()
|
||||||
|
err := jsoniter.UnmarshalFromString(importsRaw, &imports)
|
||||||
|
if err != nil {
|
||||||
|
exception.New(err.Error(), 500).Throw()
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
data := process.ArgsMap(3)
|
data := process.ArgsMap(3)
|
||||||
option := core.ParserOption{
|
option := core.ParserOption{
|
||||||
Theme: opt["theme"],
|
Theme: opt["theme"],
|
||||||
|
|
@ -159,8 +182,11 @@ func TemplateRender(process *process.Process) interface{} {
|
||||||
Route: route,
|
Route: route,
|
||||||
Root: root,
|
Root: root,
|
||||||
Script: nil,
|
Script: nil,
|
||||||
Imports: nil,
|
Imports: imports,
|
||||||
Request: nil,
|
Request: &core.Request{
|
||||||
|
Theme: opt["theme"],
|
||||||
|
Locale: opt["locale"],
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
parser := core.NewTemplateParser(core.Data(data), &option)
|
parser := core.NewTemplateParser(core.Data(data), &option)
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue