feat: Enhance global build context and component handling

- Update NewGlobalBuildContext to accept a template parameter for improved context management.
- Refactor component handling in Compile method to include JIT components, enhancing dynamic component loading.
- Modify TemplateRender to utilize the updated global context, improving template processing.
- Adjust component attribute parsing to use "s:route" for better clarity in component identification.
This commit is contained in:
Max 2025-04-23 16:08:10 +08:00
parent 57e8389afd
commit 77efc03582
8 changed files with 31 additions and 8 deletions

View file

@ -164,7 +164,7 @@ func TemplateRender(process *process.Process) interface{} {
source := process.ArgsString(2)
page := tmpl.CreatePage(source)
route := page.Get().Route
globalCtx := core.NewGlobalBuildContext()
globalCtx := core.NewGlobalBuildContext(tmpl)
suicode, _, err := page.Get().CompileAsComponent(core.NewBuildContext(globalCtx), &core.BuildOption{
PublicRoot: root,
AssetRoot: assetRoot,

View file

@ -89,8 +89,27 @@ func (page *Page) Compile(ctx *BuildContext, option *BuildOption) (string, strin
}
// Page Components
if ctx != nil && ctx.components != nil && len(ctx.components) > 0 {
rawComponents, _ := jsoniter.MarshalToString(ctx.components)
if ctx != nil {
components := map[string]string{}
for _, component := range ctx.GetComponents() {
components[component] = component
}
// Add Jit Components
if ctx.global != nil && ctx.global.tmpl != nil {
patterns := ctx.GetJitComponents()
// Get all pages
routes, err := ctx.global.tmpl.GlobRoutes(patterns, true)
if err != nil {
return "", "", warnings, fmt.Errorf("Get jit components error: %s", err.Error())
}
for _, route := range routes {
components[route] = route
}
}
rawComponents, _ := jsoniter.MarshalToString(components)
body.AppendHtml("\n\n" + `<script name="imports" type="json">` + "\n" + rawComponents + "\n</script>\n\n")
}

View file

@ -26,9 +26,10 @@ func NewTranslateContext() *TranslateContext {
}
// NewGlobalBuildContext create a new global build context
func NewGlobalBuildContext() *GlobalBuildContext {
func NewGlobalBuildContext(tmpl ITemplate) *GlobalBuildContext {
return &GlobalBuildContext{
jitComponents: map[string]bool{},
tmpl: tmpl,
}
}

View file

@ -187,6 +187,7 @@ func (data Data) ReplaceUse(tokens Tokens, value string) (string, []StringValue)
values = append(values, v)
return v.Value
})
return res, values
}

View file

@ -69,6 +69,8 @@ type ITemplate interface {
ExecAfterBuildScripts() []TemplateScirptResult
Trans(option *BuildOption) ([]string, error)
GlobRoutes(patterns []string, unique ...bool) ([]string, error)
}
// IPage is the interface for the page

View file

@ -292,7 +292,7 @@ func (parser *TemplateParser) parseElementNode(sel *goquery.Selection) {
func (parser *TemplateParser) parseElementComponent(sel *goquery.Selection) {
parser.parsed(sel)
com := sel.AttrOr("s:cn", "")
com := sel.AttrOr("s:route", "")
props := map[string]interface{}{}
for _, attr := range sel.Nodes[0].Attr {
if strings.HasPrefix(attr.Key, "prop:") {

View file

@ -6,7 +6,7 @@ import (
var propTokens = Tokens{
{start: "{%", end: "%}"}, // {% xxx %}
{start: "[{", end: "}]"}, // [{ xxx }]
// {start: "[{", end: "}]"}, // [{ xxx }] Deprecated
}
var dataTokens = Tokens{

View file

@ -68,7 +68,7 @@ func (tmpl *Template) Build(option *core.BuildOption) ([]string, error) {
}
// Build all pages
ctx := core.NewGlobalBuildContext()
ctx := core.NewGlobalBuildContext(tmpl)
pages, err := tmpl.Pages()
if err != nil {
return warnings, err
@ -149,7 +149,7 @@ func (tmpl *Template) Trans(option *core.BuildOption) ([]string, error) {
var err error
warnings := []string{}
ctx := core.NewGlobalBuildContext()
ctx := core.NewGlobalBuildContext(tmpl)
pages, err := tmpl.Pages()
if err != nil {
return warnings, err