1
0
mirror of https://github.com/kataras/iris.git synced 2025-12-17 18:07:01 +00:00

Add a 'ParseTemplate' method on view engines to manually parse and add a template from a text

examples at: https://github.com/kataras/iris/tree/master/_examples/view/parse-template

relative to: https://github.com/kataras/iris/issues/1617
This commit is contained in:
Gerasimos (Makis) Maropoulos
2020-09-08 07:55:33 +03:00
parent 64038b09e3
commit a4996b90c8
13 changed files with 307 additions and 100 deletions

View File

@@ -235,18 +235,8 @@ func (s *HTMLEngine) Load() error {
})
}
func (s *HTMLEngine) initRootTmpl() { // protected by the caller.
if s.Templates == nil {
// the root template should be the same,
// no matter how many reloads as the
// following unexported fields cannot be modified.
s.Templates = template.New(s.rootDir)
s.Templates.Delims(s.left, s.right)
}
}
// ParseTemplate adds a custom template to the root template.
func (s *HTMLEngine) ParseTemplate(name string, contents []byte, funcMap template.FuncMap) (err error) {
func (s *HTMLEngine) ParseTemplate(name string, contents []byte, funcs template.FuncMap) (err error) {
s.rmu.Lock()
defer s.rmu.Unlock()
@@ -268,13 +258,23 @@ func (s *HTMLEngine) ParseTemplate(name string, contents []byte, funcMap templat
}
tmpl.Funcs(emptyFuncs).Funcs(s.funcs)
if len(funcMap) > 0 {
tmpl.Funcs(funcMap) // custom for this template.
if len(funcs) > 0 {
tmpl.Funcs(funcs) // custom for this template.
}
_, err = tmpl.Parse(text)
return
}
func (s *HTMLEngine) initRootTmpl() { // protected by the caller.
if s.Templates == nil {
// the root template should be the same,
// no matter how many reloads as the
// following unexported fields cannot be modified.
s.Templates = template.New(s.rootDir)
s.Templates.Delims(s.left, s.right)
}
}
func (s *HTMLEngine) executeTemplateBuf(name string, binding interface{}) (*bytes.Buffer, error) {
buf := new(bytes.Buffer)
err := s.Templates.ExecuteTemplate(buf, name, binding)