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:
26
view/html.go
26
view/html.go
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user