Former-commit-id: 2b8dfec7d43298bc9ace94595359867180634f04
This commit is contained in:
Gerasimos (Makis) Maropoulos 2020-02-15 21:16:21 +02:00
parent 19778972e4
commit 66c0f632c3
2 changed files with 16 additions and 9 deletions

2
go.mod
View File

@ -18,7 +18,7 @@ require (
github.com/iris-contrib/go.uuid v2.0.0+incompatible github.com/iris-contrib/go.uuid v2.0.0+incompatible
github.com/iris-contrib/pongo2 v0.0.1 github.com/iris-contrib/pongo2 v0.0.1
github.com/iris-contrib/schema v0.0.1 github.com/iris-contrib/schema v0.0.1
github.com/iris-contrib/jade v1.1.1 github.com/iris-contrib/jade v1.1.2
github.com/json-iterator/go v1.1.9 github.com/json-iterator/go v1.1.9
github.com/kataras/golog v0.0.10 github.com/kataras/golog v0.0.10
github.com/kataras/neffos v0.0.14 github.com/kataras/neffos v0.0.14

View File

@ -4,6 +4,7 @@ import (
"bytes" "bytes"
"io/ioutil" "io/ioutil"
"path" "path"
"strings"
"github.com/iris-contrib/jade" "github.com/iris-contrib/jade"
) )
@ -22,28 +23,34 @@ import (
// https://github.com/kataras/iris/tree/master/_examples/view/template_pug_3 // https://github.com/kataras/iris/tree/master/_examples/view/template_pug_3
func Pug(directory, extension string) *HTMLEngine { func Pug(directory, extension string) *HTMLEngine {
s := HTML(directory, extension) s := HTML(directory, extension)
s.middleware = func(name string, text []byte) (contents string, err error) { s.middleware = func(name string, text []byte) (contents string, err error) {
name = path.Join(path.Clean(directory), name)
tmpl := jade.New(name) tmpl := jade.New(name)
// 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.ReadFunc = func(name string) ([]byte, error) { tmpl.ReadFunc = func(name string) ([]byte, error) {
if !strings.HasPrefix(path.Clean(name), path.Clean(directory)) {
name = path.Join(directory, name) name = path.Join(directory, name)
}
if s.assetFn != nil { if s.assetFn != nil {
return s.assetFn(name) return s.assetFn(name)
} }
return ioutil.ReadFile(name) return ioutil.ReadFile(name)
} }
tmpl, err = tmpl.Parse(text) // 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.
exec, err := tmpl.Parse(text)
if err != nil { if err != nil {
return return
} }
b := new(bytes.Buffer) b := new(bytes.Buffer)
tmpl.WriteIn(b) exec.WriteIn(b)
return b.String(), nil return b.String(), nil
} }
return s return s