mirror of
https://github.com/kataras/iris.git
synced 2025-12-17 09:57:01 +00:00
add a ParseTemplate to the HTML view engine.
relative to: #1617 Wait for an answer from the issuer and if that's the expected behavior, do the same for the rest of the view engines
This commit is contained in:
22
view/fs.go
22
view/fs.go
@@ -49,6 +49,10 @@ func assetNames(fs http.FileSystem, name string) ([]string, error) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if f == nil {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
infos, err := f.Readdir(-1)
|
||||
f.Close()
|
||||
if err != nil {
|
||||
@@ -82,9 +86,17 @@ func asset(fs http.FileSystem, name string) ([]byte, error) {
|
||||
}
|
||||
|
||||
func getFS(fsOrDir interface{}) (fs http.FileSystem) {
|
||||
if fsOrDir == nil {
|
||||
return noOpFS{}
|
||||
}
|
||||
|
||||
switch v := fsOrDir.(type) {
|
||||
case string:
|
||||
fs = httpDirWrapper{http.Dir(v)}
|
||||
if v == "" {
|
||||
fs = noOpFS{}
|
||||
} else {
|
||||
fs = httpDirWrapper{http.Dir(v)}
|
||||
}
|
||||
case http.FileSystem:
|
||||
fs = v
|
||||
default:
|
||||
@@ -94,6 +106,10 @@ func getFS(fsOrDir interface{}) (fs http.FileSystem) {
|
||||
return
|
||||
}
|
||||
|
||||
type noOpFS struct{}
|
||||
|
||||
func (fs noOpFS) Open(name string) (http.File, error) { return nil, nil }
|
||||
|
||||
// fixes: "invalid character in file path"
|
||||
// on amber engine (it uses the virtual fs directly
|
||||
// and it uses filepath instead of the path package...).
|
||||
@@ -101,6 +117,6 @@ type httpDirWrapper struct {
|
||||
http.Dir
|
||||
}
|
||||
|
||||
func (d httpDirWrapper) Open(name string) (http.File, error) {
|
||||
return d.Dir.Open(filepath.ToSlash(name))
|
||||
func (fs httpDirWrapper) Open(name string) (http.File, error) {
|
||||
return fs.Dir.Open(filepath.ToSlash(name))
|
||||
}
|
||||
|
||||
67
view/html.go
67
view/html.go
@@ -215,12 +215,6 @@ func (s *HTMLEngine) Funcs(funcMap template.FuncMap) *HTMLEngine {
|
||||
//
|
||||
// Returns an error if something bad happens, caller is responsible to handle that.
|
||||
func (s *HTMLEngine) Load() error {
|
||||
s.rmu.Lock()
|
||||
defer s.rmu.Unlock()
|
||||
|
||||
s.Templates = template.New(s.rootDir)
|
||||
s.Templates.Delims(s.left, s.right)
|
||||
|
||||
return walk(s.fs, s.rootDir, func(path string, info os.FileInfo, err error) error {
|
||||
if info == nil || info.IsDir() {
|
||||
return nil
|
||||
@@ -236,30 +230,51 @@ func (s *HTMLEngine) Load() error {
|
||||
if err != nil {
|
||||
return fmt.Errorf("%s: %w", path, err)
|
||||
}
|
||||
contents := string(buf)
|
||||
|
||||
name := strings.TrimPrefix(path, "/")
|
||||
tmpl := s.Templates.New(name)
|
||||
tmpl.Option(s.options...)
|
||||
|
||||
if s.middleware != nil {
|
||||
contents, err = s.middleware(name, buf)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
// Add our funcmaps.
|
||||
_, err = tmpl.
|
||||
Funcs(emptyFuncs).
|
||||
// Funcs(s.makeDefaultLayoutFuncs(name)).
|
||||
// Funcs(s.layoutFuncs).
|
||||
Funcs(s.funcs).
|
||||
Parse(contents)
|
||||
return err
|
||||
return s.ParseTemplate(path, buf, nil)
|
||||
})
|
||||
}
|
||||
|
||||
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) {
|
||||
s.rmu.Lock()
|
||||
defer s.rmu.Unlock()
|
||||
|
||||
s.initRootTmpl()
|
||||
|
||||
name = strings.TrimPrefix(name, "/")
|
||||
tmpl := s.Templates.New(name)
|
||||
tmpl.Option(s.options...)
|
||||
|
||||
var text string
|
||||
|
||||
if s.middleware != nil {
|
||||
text, err = s.middleware(name, contents)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
} else {
|
||||
text = string(contents)
|
||||
}
|
||||
|
||||
tmpl.Funcs(emptyFuncs).Funcs(s.funcs)
|
||||
if len(funcMap) > 0 {
|
||||
tmpl.Funcs(funcMap) // custom for this template.
|
||||
}
|
||||
_, err = tmpl.Parse(text)
|
||||
return
|
||||
}
|
||||
|
||||
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