This commit is contained in:
Gerasimos (Makis) Maropoulos 2023-01-08 13:58:46 +02:00
parent 196063d4db
commit 41a2396ea1
No known key found for this signature in database
GPG Key ID: 403EEB7885C79503

View File

@ -64,16 +64,20 @@ var (
// HTML(embed.FS, ".html") or HTML(AssetFile(), ".html") for embedded data. // HTML(embed.FS, ".html") or HTML(AssetFile(), ".html") for embedded data.
func HTML(dirOrFS interface{}, extension string) *HTMLEngine { func HTML(dirOrFS interface{}, extension string) *HTMLEngine {
s := &HTMLEngine{ s := &HTMLEngine{
name: "HTML", name: "HTML",
fs: getFS(dirOrFS), fs: getFS(dirOrFS),
rootDir: "/", rootDir: "/",
extension: extension, extension: extension,
reload: false, reload: false,
left: "{{", left: "{{",
right: "}}", right: "}}",
layout: "", layout: "",
layoutFuncs: make(template.FuncMap), layoutFuncs: template.FuncMap{
funcs: make(template.FuncMap), "yield": func(binding interface{}) template.HTML {
return template.HTML("")
},
},
funcs: make(template.FuncMap),
bufPool: &sync.Pool{New: func() interface{} { bufPool: &sync.Pool{New: func() interface{} {
return new(bytes.Buffer) return new(bytes.Buffer)
}}, }},
@ -361,13 +365,20 @@ func (s *HTMLEngine) executeTemplateBuf(name string, binding interface{}) (strin
return result, err return result, err
} }
func (s *HTMLEngine) getBuiltinFuncs(name string) template.FuncMap { func (s *HTMLEngine) getBuiltinRuntimeLayoutFuncs(name string) template.FuncMap {
funcs := template.FuncMap{ funcs := template.FuncMap{
"yield": func(binding interface{}) (template.HTML, error) { "yield": func(binding interface{}) (template.HTML, error) {
result, err := s.executeTemplateBuf(name, binding) result, err := s.executeTemplateBuf(name, binding)
// Return safe HTML here since we are rendering our own template. // Return safe HTML here since we are rendering our own template.
return template.HTML(result), err return template.HTML(result), err
}, },
}
return funcs
}
func (s *HTMLEngine) getBuiltinFuncs(name string) template.FuncMap {
funcs := template.FuncMap{
"part": func(partName string, binding interface{}) (template.HTML, error) { "part": func(partName string, binding interface{}) (template.HTML, error) {
nameTemp := strings.ReplaceAll(name, s.extension, "") nameTemp := strings.ReplaceAll(name, s.extension, "")
fullPartName := fmt.Sprintf("%s-%s", nameTemp, partName) fullPartName := fmt.Sprintf("%s-%s", nameTemp, partName)
@ -433,7 +444,7 @@ func (s *HTMLEngine) ExecuteWriter(w io.Writer, name string, layout string, bind
return ErrNotExist{Name: layout, IsLayout: true, Data: bindingData} return ErrNotExist{Name: layout, IsLayout: true, Data: bindingData}
} }
return lt.Execute(w, bindingData) return lt.Funcs(s.getBuiltinRuntimeLayoutFuncs(name)).Execute(w, bindingData)
} }
t := s.Templates.Lookup(name) t := s.Templates.Lookup(name)