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

minor fixes on jet and pug view engines

This commit is contained in:
Gerasimos (Makis) Maropoulos
2022-09-27 22:42:42 +03:00
parent 29010bfd7c
commit 50d346de84
6 changed files with 37 additions and 16 deletions

View File

@@ -34,6 +34,8 @@ type HTMLEngine struct {
//
middleware func(name string, contents []byte) (string, error)
onLoad func()
onLoaded func()
Templates *template.Template
customCache []customTmp // required to load them again if reload is true.
bufPool *sync.Pool
@@ -250,6 +252,10 @@ func (s *HTMLEngine) Load() error {
}
func (s *HTMLEngine) load() error {
if s.onLoad != nil {
s.onLoad()
}
if err := s.reloadCustomTemplates(); err != nil {
return err
}
@@ -284,6 +290,10 @@ func (s *HTMLEngine) load() error {
return s.parseTemplate(path, buf, nil)
})
if s.onLoaded != nil {
s.onLoaded()
}
if err != nil {
return err
}

View File

@@ -215,11 +215,13 @@ var _ jet.Loader = (*jetLoader)(nil)
// Open opens a file from file system.
func (l *jetLoader) Open(name string) (io.ReadCloser, error) {
name = strings.TrimPrefix(name, "/")
return l.fs.Open(name)
}
// Exists checks if the template name exists by walking the list of template paths.
func (l *jetLoader) Exists(name string) bool {
name = strings.TrimPrefix(name, "/")
_, err := l.fs.Open(name)
return err == nil
}

View File

@@ -2,8 +2,9 @@ package view
import (
"bytes"
"os"
"github.com/iris-contrib/jade"
"github.com/Joker/jade"
)
// Pug (or Jade) returns a new pug view engine.
@@ -26,18 +27,12 @@ import (
func Pug(fs interface{}, extension string) *HTMLEngine {
s := HTML(fs, extension)
s.name = "Pug"
s.middleware = func(name string, text []byte) (contents string, err error) {
tmpl := jade.New(name)
tmpl.ReadFunc = func(name string) ([]byte, error) {
return asset(s.fs, name)
jade.ReadFunc = func(filename string) ([]byte, error) {
return asset(s.fs, filename)
}
// Fixes: https://github.com/kataras/iris/issues/1450
// by adding a custom ReadFunc inside the jade parser.
// And Also able to use relative paths on "extends" and "include" directives:
// e.g. instead of extends "templates/layout.pug" we use "layout.pug"
// so contents of templates are independent of their root location.
tmpl := jade.New(name)
exec, err := tmpl.Parse(text)
if err != nil {
return
@@ -45,6 +40,7 @@ func Pug(fs interface{}, extension string) *HTMLEngine {
b := new(bytes.Buffer)
exec.WriteIn(b)
jade.ReadFunc = os.ReadFile // reset to original.
return b.String(), nil
}
return s