Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions render/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ type Context interface {
// RenderFile parses and renders a template. It's used in the implementation of the {% include %} tag.
// RenderFile does not cache the compiled template.
RenderFile(string, map[string]any) (string, error)
// RenderFileIsolated parses and renders a template with an isolated scope.
// Unlike RenderFile, it does not inherit variables from the parent context.
// It's used in the implementation of the {% render %} tag.
RenderFileIsolated(string, map[string]any) (string, error)
// Set updates the value of a variable in the current lexical environment.
// It's used in the implementation of the {% assign %} and {% capture %} tags.
Set(name string, value any)
Expand Down Expand Up @@ -193,6 +197,40 @@ func (c rendererContext) RenderFile(filename string, b map[string]any) (string,
return buf.String(), nil
}

// RenderFileIsolated renders a template with an isolated scope (no parent variables).
// This is used by the {% render %} tag to provide true variable isolation.
func (c rendererContext) RenderFileIsolated(filename string, b map[string]any) (string, error) {
source, err := c.ctx.config.TemplateStore.ReadTemplate(filename)
if err != nil && os.IsNotExist(err) {
// Is it cached?
if cval, ok := c.ctx.config.Cache[filename]; ok {
source = cval
} else {
return "", err
}
} else if err != nil {
return "", err
}

root, err := c.ctx.config.Compile(string(source), c.node.SourceLoc)
if err != nil {
return "", err
}

// Use only the provided bindings (isolated scope - no parent context)
bindings := map[string]any{}
for k, v := range b {
bindings[k] = v
}

buf := new(bytes.Buffer)
if err := Render(root, buf, bindings, c.ctx.config); err != nil {
return "", err
}

return buf.String(), nil
}

// InnerString renders the children to a string.
func (c rendererContext) InnerString() (string, error) {
buf := new(bytes.Buffer)
Expand Down
Loading
Loading