-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
pug.go
57 lines (49 loc) · 1.68 KB
/
pug.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package view
import (
"bytes"
"io/ioutil"
"path"
"strings"
"github.com/iris-contrib/jade"
)
// Pug (or Jade) returns a new pug view engine.
// It shares the same exactly logic with the
// html view engine, it uses the same exactly configuration.
// It has got some features and a lot of functions
// which will make your life easier.
// Read more about the Jade Go Parser: https://github.com/Joker/jade
//
// Examples:
// https://github.com/kataras/iris/tree/master/_examples/view/template_pug_0
// https://github.com/kataras/iris/tree/master/_examples/view/template_pug_1
// https://github.com/kataras/iris/tree/master/_examples/view/template_pug_2
// https://github.com/kataras/iris/tree/master/_examples/view/template_pug_3
func Pug(directory, extension string) *HTMLEngine {
s := HTML(directory, extension)
s.middleware = func(name string, text []byte) (contents string, err error) {
name = path.Join(path.Clean(directory), name)
tmpl := jade.New(name)
tmpl.ReadFunc = func(name string) ([]byte, error) {
if !strings.HasPrefix(path.Clean(name), path.Clean(directory)) {
name = path.Join(directory, name)
}
if s.assetFn != nil {
return s.assetFn(name)
}
return ioutil.ReadFile(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.
exec, err := tmpl.Parse(text)
if err != nil {
return
}
b := new(bytes.Buffer)
exec.WriteIn(b)
return b.String(), nil
}
return s
}