4
4
"bytes"
5
5
"io/ioutil"
6
6
"path"
7
+ "strings"
7
8
8
9
"github.com/iris-contrib/jade"
9
10
)
@@ -22,28 +23,34 @@ import (
22
23
// https://github.com/kataras/iris/tree/master/_examples/view/template_pug_3
23
24
func Pug (directory , extension string ) * HTMLEngine {
24
25
s := HTML (directory , extension )
26
+
25
27
s .middleware = func (name string , text []byte ) (contents string , err error ) {
28
+ name = path .Join (path .Clean (directory ), name )
26
29
tmpl := jade .New (name )
27
- // Fixes: https://github.com/kataras/iris/issues/1450
28
- // by adding a custom ReadFunc inside the jade parser.
29
- // And Also able to use relative paths on "extends" and "include" directives:
30
- // e.g. instead of extends "templates/layout.pug" we use "layout.pug"
31
- // so contents of templates are independent of their root location.
32
30
tmpl .ReadFunc = func (name string ) ([]byte , error ) {
33
- name = path .Join (directory , name )
31
+ if ! strings .HasPrefix (path .Clean (name ), path .Clean (directory )) {
32
+ name = path .Join (directory , name )
33
+ }
34
+
34
35
if s .assetFn != nil {
35
36
return s .assetFn (name )
36
37
}
37
38
return ioutil .ReadFile (name )
38
39
}
39
40
40
- tmpl , err = tmpl .Parse (text )
41
+ // Fixes: https://github.com/kataras/iris/issues/1450
42
+ // by adding a custom ReadFunc inside the jade parser.
43
+ // And Also able to use relative paths on "extends" and "include" directives:
44
+ // e.g. instead of extends "templates/layout.pug" we use "layout.pug"
45
+ // so contents of templates are independent of their root location.
46
+
47
+ exec , err := tmpl .Parse (text )
41
48
if err != nil {
42
49
return
43
50
}
44
51
45
52
b := new (bytes.Buffer )
46
- tmpl .WriteIn (b )
53
+ exec .WriteIn (b )
47
54
return b .String (), nil
48
55
}
49
56
return s
0 commit comments