-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy patheval_include.go
More file actions
61 lines (50 loc) · 2.02 KB
/
Copy patheval_include.go
File metadata and controls
61 lines (50 loc) · 2.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package vuego
import (
"fmt"
"golang.org/x/net/html"
"github.com/titpetric/vuego/internal/helpers"
"github.com/titpetric/vuego/internal/parser"
)
// evalInclude processes a <template include="..."> tag with the given vars map.
// Handles stack push/pop properly using defer to ensure cleanup even on error.
func (v *Vue) evalInclude(ctx VueContext, node *html.Node, vars map[string]any, depth int) ([]*html.Node, error) {
ctx.stack.Push(vars)
defer ctx.stack.Pop()
// Extract slot content from the component tag if not already processed
if ctx.SlotScope == nil {
ctx.SlotScope = extractSlotContent(node)
}
// Merge inherited slots from parent template (passed via __slotScope__ in data)
if inheritedSlotScopeData, ok := ctx.stack.EnvMap()["__slotScope__"]; ok {
if inheritedSlotScope, ok := inheritedSlotScopeData.(*SlotScope); ok {
for slotName, slotContent := range inheritedSlotScope.Slots {
if ctx.SlotScope.GetSlot(slotName) == nil {
// Only add if not already defined in the include tag
// Use the slot content directly (already parsed as DOM nodes)
ctx.SlotScope.SetSlot(slotName, slotContent)
}
}
}
}
name := helpers.GetAttr(node, "include")
frontMatter, templateBytes, err := v.loader.loadFragment(name)
if err != nil {
return nil, fmt.Errorf("error loading %s (included from %s): %w", name, ctx.FormatTemplateChain(), err)
}
// Merge front-matter data (authoritative - overrides passed data)
for k, v := range frontMatter {
ctx.stack.Set(k, v)
}
// Parse template bytes into DOM
compDom, err := parser.ParseTemplateBytes(templateBytes)
if err != nil {
return nil, fmt.Errorf("error parsing %s (included from %s): %w", name, ctx.FormatTemplateChain(), err)
}
// Validate and process template tag
processedDom, err := v.evalTemplate(ctx, compDom, ctx.stack.EnvMap(), depth+1)
if err != nil {
return nil, fmt.Errorf("error in %s (included from %s): %w", name, ctx.FormatTemplateChain(), err)
}
childCtx := ctx.WithTemplate(name)
return v.evaluate(childCtx, processedDom, depth+1)
}