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

getBuiltinFuncs problem #2289

Open
nevata opened this issue Nov 27, 2023 · 0 comments
Open

getBuiltinFuncs problem #2289

nevata opened this issue Nov 27, 2023 · 0 comments
Assignees

Comments

@nevata
Copy link

nevata commented Nov 27, 2023

Describe the bug
The name parameter of the template built-in function has a scope problem

view/html.go
func (s *HTMLEngine) getBuiltinFuncs(name string) template.FuncMap {
funcs := template.FuncMap{
"part": func(partName string, binding interface{}) (template.HTML, error) {
nameTemp := strings.ReplaceAll(name, s.extension, "")
fullPartName := fmt.Sprintf("%s-%s", nameTemp, partName)
result, err := s.executeTemplateBuf(fullPartName, binding)
if err != nil {
return "", nil
}
return template.HTML(result), err
},
"current": func() (string, error) {
return name, nil
},
"partial": func(partialName string, binding interface{}) (template.HTML, error) {
fullPartialName := fmt.Sprintf("%s-%s", partialName, name)
if s.Templates.Lookup(fullPartialName) != nil {
result, err := s.executeTemplateBuf(fullPartialName, binding)
return template.HTML(result), err
}
return "", nil
},
// partial related to current page,
// it would be easier for adding pages' style/script inline
// for example when using partial_r '.script' in layout.html
// templates/users/index.html would load templates/users/index.script.html
"partial_r": func(partialName string, binding interface{}) (template.HTML, error) {
ext := filepath.Ext(name)
root := name[:len(name)-len(ext)]
fullPartialName := fmt.Sprintf("%s%s%s", root, partialName, ext)
if s.Templates.Lookup(fullPartialName) != nil {
result, err := s.executeTemplateBuf(fullPartialName, binding)
return template.HTML(result), err
}
return "", nil
},
"render": func(fullPartialName string, binding interface{}) (template.HTML, error) {
result, err := s.executeTemplateBuf(fullPartialName, binding)
return template.HTML(result), err
},
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants