Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parent context in partial with hash parameters #17

Closed
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
11 changes: 10 additions & 1 deletion eval.go
Original file line number Diff line number Diff line change
Expand Up @@ -702,8 +702,17 @@ func (v *evalVisitor) partialContext(node *ast.PartialStatement) reflect.Value {
}

if node.Hash != nil {
// mix the hash parameters and the current context into one map
hash, _ := node.Hash.Accept(v).(map[string]interface{})
return reflect.ValueOf(hash)
curCtx, _ := v.curCtx().Interface().(map[string]interface{})
newCtx := make(map[string]interface{})
for k, v := range curCtx {
newCtx[k] = v
}
for k, v := range hash {
newCtx[k] = v
}
return reflect.ValueOf(newCtx)
}

return zero
Expand Down
16 changes: 16 additions & 0 deletions partial.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,22 @@ func RegisterPartialTemplate(name string, tpl *Template) {
partials[name] = newPartial(name, "", tpl)
}

// RemovePartial removes the partial registered under the given name. The partial will not be available globally anymore. This does not affect partials registered on a specific template.
func RemovePartial(name string) {
partialsMutex.Lock()
defer partialsMutex.Unlock()

delete(partials, name)
}

// RemoveAllPartials removes all globally registered partials. This does not affect partials registered on a specific template.
func RemoveAllPartials() {
partialsMutex.Lock()
defer partialsMutex.Unlock()

partials = make(map[string]*partial)
}

// findPartial finds a registered global partial
func findPartial(name string) *partial {
partialsMutex.RLock()
Expand Down