Skip to content

Commit 464e5bf

Browse files
authored
Merge pull request #22 from Fenny/master
Bump dependencies
2 parents 18838e8 + fad4629 commit 464e5bf

File tree

10 files changed

+345
-19
lines changed

10 files changed

+345
-19
lines changed

go.mod

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,8 @@ require (
99
github.com/aymerick/raymond v2.0.2+incompatible
1010
github.com/cbroglie/mustache v1.1.0
1111
github.com/eknkc/amber v0.0.0-20171010120322-cdade1c07385
12-
github.com/flosch/pongo2 v0.0.0-20200518135938-dfb43dbdc22a
13-
github.com/mattn/go-slim v0.0.0-20190926010428-236f1c35e4e6
12+
github.com/flosch/pongo2 v0.0.0-20200529170236-5abacdfa4915
13+
github.com/mattn/go-slim v0.0.0-20200618151855-bde33eecb5ee
1414
github.com/valyala/bytebufferpool v1.0.0
1515
github.com/yosssi/ace v0.0.5
16-
gopkg.in/yaml.v2 v2.3.0 // indirect
1716
)

go.sum

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
22
github.com/CloudyKit/fastprinter v0.0.0-20200109182630-33d98a066a53 h1:sR+/8Yb4slttB4vD+b9btVEnWgL3Q00OBTzVT8B9C0c=
33
github.com/CloudyKit/fastprinter v0.0.0-20200109182630-33d98a066a53/go.mod h1:+3IMCy2vIlbG1XG/0ggNQv0SvxCAIpPM5b1nCz56Xno=
4-
github.com/CloudyKit/jet v2.1.2+incompatible h1:ybZoYzMBdoijK6I+Ke3vg9GZsmlKo/ZhKdNMWz0P26c=
5-
github.com/CloudyKit/jet v2.1.2+incompatible/go.mod h1:HPYO+50pSWkPoj9Q/eq0aRGByCL6ScRlUmiEX5Zgm+w=
64
github.com/CloudyKit/jet/v3 v3.0.0 h1:1PwO5w5VCtlUUl+KTOBsTGZlhjWkcybsGaAau52tOy8=
75
github.com/CloudyKit/jet/v3 v3.0.0/go.mod h1:HKQPgSJmdK8hdoAbKUUWajkHyHo4RaU5rMdUywE7VMo=
86
github.com/Joker/hpp v0.0.0-20180418125244-6893e659854a h1:PiDAizhfJbwZMISZ1Itx1ZTFeOFCml89Ofmz3V8rhoU=
@@ -23,28 +21,22 @@ github.com/cpuguy83/go-md2man v1.0.10/go.mod h1:SmD6nW6nTyfqj6ABTjUi3V3JVMnlJmwc
2321
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
2422
github.com/eknkc/amber v0.0.0-20171010120322-cdade1c07385 h1:clC1lXBpe2kTj2VHdaIu9ajZQe4kcEY9j0NsnDDBZ3o=
2523
github.com/eknkc/amber v0.0.0-20171010120322-cdade1c07385/go.mod h1:0vRUJqYpeSZifjYj7uP3BG/gKcuzL9xWVV/Y+cK33KM=
26-
github.com/flosch/pongo2 v0.0.0-20200518135938-dfb43dbdc22a h1:8Dw1FO25BORXdlipopkXixOKPe3qjfqfxUOb327zP48=
27-
github.com/flosch/pongo2 v0.0.0-20200518135938-dfb43dbdc22a/go.mod h1:StS3bHLP8nf6A+gzLIW2rrGeSCZrS0DMNTrIEEPRHz0=
24+
github.com/flosch/pongo2 v0.0.0-20200529170236-5abacdfa4915 h1:rNVrewdFbSujcoKZifC6cHJfqCTbCIR7XTLHW5TqUWU=
25+
github.com/flosch/pongo2 v0.0.0-20200529170236-5abacdfa4915/go.mod h1:fB4mx6dzqFinCxIf3a7Mf5yLk+18Bia9mPAnuejcvDA=
2826
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
2927
github.com/go-check/check v0.0.0-20180628173108-788fd7840127 h1:0gkP6mzaMqkmpcJYCFOLkIBwI7xFExG03bbkOkCvUPI=
3028
github.com/go-check/check v0.0.0-20180628173108-788fd7840127/go.mod h1:9ES+weclKsC9YodN5RgxqK/VD9HM9JsCSh7rNhMZE98=
3129
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
3230
github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM=
3331
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
34-
github.com/juju/errors v0.0.0-20181118221551-089d3ea4e4d5 h1:rhqTjzJlm7EbkELJDKMTU7udov+Se0xZkWmugr6zGok=
35-
github.com/juju/errors v0.0.0-20181118221551-089d3ea4e4d5/go.mod h1:W54LbzXuIE0boCoNJfwqpmkKJ1O4TCTZMetAt6jGk7Q=
36-
github.com/juju/loggo v0.0.0-20180524022052-584905176618 h1:MK144iBQF9hTSwBW/9eJm034bVoG30IshVm688T2hi8=
37-
github.com/juju/loggo v0.0.0-20180524022052-584905176618/go.mod h1:vgyd7OREkbtVEN/8IXZe5Ooef3LQePvuBm9UWj6ZL8U=
38-
github.com/juju/testing v0.0.0-20180920084828-472a3e8b2073 h1:WQM1NildKThwdP7qWrNAFGzp4ijNLw8RlgENkaI4MJs=
39-
github.com/juju/testing v0.0.0-20180920084828-472a3e8b2073/go.mod h1:63prj8cnj0tU0S9OHjGJn+b1h0ZghCndfnbQolrYTwA=
4032
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
4133
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
4234
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
4335
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
4436
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
4537
github.com/magiconair/properties v1.8.0/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ=
46-
github.com/mattn/go-slim v0.0.0-20190926010428-236f1c35e4e6 h1:GF43XHtQ/ewS07mITQZyi9gXhWZ7x7crGWp2hvHUEkM=
47-
github.com/mattn/go-slim v0.0.0-20190926010428-236f1c35e4e6/go.mod h1:ma9TUJeni8LGZMJvOwbAv/FOwiwqIMQN570LnpqCBSM=
38+
github.com/mattn/go-slim v0.0.0-20200618151855-bde33eecb5ee h1:M3l0/LNEemy7ykM6qCZylkWmFPyvDLDCl7bZPLTfaBQ=
39+
github.com/mattn/go-slim v0.0.0-20200618151855-bde33eecb5ee/go.mod h1:ma9TUJeni8LGZMJvOwbAv/FOwiwqIMQN570LnpqCBSM=
4840
github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
4941
github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y=
5042
github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic=
@@ -66,6 +58,7 @@ github.com/xordataexchange/crypt v0.0.3-0.20170626215501-b2862e3d0a77/go.mod h1:
6658
github.com/yosssi/ace v0.0.5 h1:tUkIP/BLdKqrlrPwcmH0shwEEhTRHoGnc1wFIWmaBUA=
6759
github.com/yosssi/ace v0.0.5/go.mod h1:ALfIzm2vT7t5ZE7uoIZqF3TQ7SAOyupFZnkrF5id+K0=
6860
golang.org/x/crypto v0.0.0-20181203042331-505ab145d0a9/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
61+
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2 h1:VklqNMn3ovrHsnt90PveolxSbWFaJdECFbxSq0Mqo2M=
6962
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
7063
golang.org/x/lint v0.0.0-20191125180803-fdd1cda4f05f h1:J5lckAjkw6qYlOZNj90mLYNTEKDvWeuc1yieZ8qUzUE=
7164
golang.org/x/lint v0.0.0-20191125180803-fdd1cda4f05f/go.mod h1:5qLYkcX4OjUUV8bRuDixDT3tpyyb+LUpUlRWLxfhWrs=
@@ -84,9 +77,6 @@ golang.org/x/tools v0.0.0-20191125144606-a911d9008d1f/go.mod h1:b+2E5dAYhXwXZwtn
8477
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
8578
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
8679
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
87-
gopkg.in/mgo.v2 v2.0.0-20180705113604-9856a29383ce h1:xcEWjVhvbDy+nHP67nPDDpbYrY+ILlfndk4bRioVHaU=
88-
gopkg.in/mgo.v2 v2.0.0-20180705113604-9856a29383ce/go.mod h1:yeKp02qBN3iKW1OzL3MGk2IdtZzaj7SFntXj72NppTA=
8980
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
81+
gopkg.in/yaml.v2 v2.2.7 h1:VUgggvou5XRW9mHwD/yXxIYSMtY0zoKQf/v226p2nyo=
9082
gopkg.in/yaml.v2 v2.2.7/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
91-
gopkg.in/yaml.v2 v2.3.0 h1:clyUAQHOM3G0M3f5vQj7LuJrETvjVot3Z5el9nffUtU=
92-
gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=

slim/slim.go

Lines changed: 205 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,205 @@
1+
package slim
2+
3+
import (
4+
"bytes"
5+
"fmt"
6+
"io"
7+
"net/http"
8+
"os"
9+
"path/filepath"
10+
"strings"
11+
"sync"
12+
13+
"github.com/gofiber/template/utils"
14+
"github.com/mattn/go-slim"
15+
"github.com/valyala/bytebufferpool"
16+
)
17+
18+
// Engine struct
19+
type Engine struct {
20+
// delimiters
21+
left string
22+
right string
23+
// views folder
24+
directory string
25+
// http.FileSystem supports embedded files
26+
fileSystem http.FileSystem
27+
// views extension
28+
extension string
29+
// layout variable name that incapsulates the template
30+
layout string
31+
// reload on each render
32+
reload bool
33+
// debug prints the parsed templates
34+
debug bool
35+
// lock for funcmap and templates
36+
mutex sync.RWMutex
37+
// template funcmap
38+
funcmap map[string]interface{}
39+
// templates
40+
Templates map[string]*slim.Template
41+
}
42+
43+
// New returns a Handlebar render engine for Fiber
44+
func New(directory, extension string) *Engine {
45+
engine := &Engine{
46+
left: "{{",
47+
right: "}}",
48+
directory: directory,
49+
extension: extension,
50+
layout: "embed",
51+
funcmap: make(map[string]interface{}),
52+
}
53+
return engine
54+
}
55+
56+
func NewFileSystem(fs http.FileSystem, extension string) *Engine {
57+
engine := &Engine{
58+
left: "{{",
59+
right: "}}",
60+
directory: "/",
61+
fileSystem: fs,
62+
extension: extension,
63+
layout: "embed",
64+
funcmap: make(map[string]interface{}),
65+
}
66+
return engine
67+
}
68+
69+
// Layout defines the variable name that will incapsulate the template
70+
func (e *Engine) Layout(key string) *Engine {
71+
e.layout = key
72+
return e
73+
}
74+
75+
// Delims sets the action delimiters to the specified strings, to be used in
76+
// templates. An empty delimiter stands for the
77+
// corresponding default: {{ or }}.
78+
func (e *Engine) Delims(left, right string) *Engine {
79+
fmt.Println("delims: this method is not supported for django")
80+
return e
81+
}
82+
83+
// AddFunc adds the function to the template's function map.
84+
// It is legal to overwrite elements of the default actions
85+
func (e *Engine) AddFunc(name string, fn interface{}) *Engine {
86+
e.mutex.Lock()
87+
e.funcmap[name] = fn
88+
e.mutex.Unlock()
89+
return e
90+
}
91+
92+
// Reload if set to true the templates are reloading on each render,
93+
// use it when you're in development and you don't want to restart
94+
// the application when you edit a template file.
95+
func (e *Engine) Reload(enabled bool) *Engine {
96+
e.reload = enabled
97+
return e
98+
}
99+
100+
// Debug will print the parsed templates when Load is triggered.
101+
func (e *Engine) Debug(enabled bool) *Engine {
102+
e.debug = enabled
103+
return e
104+
}
105+
106+
// Parse is deprecated, please use Load() instead
107+
func (e *Engine) Parse() error {
108+
fmt.Println("Parse() is deprecated, please use Load() instead.")
109+
return e.Load()
110+
}
111+
112+
// Load parses the templates to the engine.
113+
func (e *Engine) Load() error {
114+
// race safe
115+
e.mutex.Lock()
116+
defer e.mutex.Unlock()
117+
118+
e.Templates = make(map[string]*slim.Template)
119+
120+
// Loop trough each directory and register template files
121+
walkFn := func(path string, info os.FileInfo, err error) error {
122+
// Return error if exist
123+
if err != nil {
124+
return err
125+
}
126+
// Skip file if it's a directory or has no file info
127+
if info == nil || info.IsDir() {
128+
return nil
129+
}
130+
// Get file extension of file
131+
ext := filepath.Ext(path)
132+
// Skip file if it does not equal the given template extension
133+
if ext != e.extension {
134+
return nil
135+
}
136+
// Get the relative file path
137+
// ./views/html/index.tmpl -> index.tmpl
138+
rel, err := filepath.Rel(e.directory, path)
139+
if err != nil {
140+
return err
141+
}
142+
// Reverse slashes '\' -> '/' and
143+
// partials\footer.tmpl -> partials/footer.tmpl
144+
name := filepath.ToSlash(rel)
145+
// Remove ext from name 'index.tmpl' -> 'index'
146+
name = strings.Replace(name, e.extension, "", -1)
147+
// Read the file
148+
// #gosec G304
149+
buf, err := utils.ReadFile(path, e.fileSystem)
150+
if err != nil {
151+
return err
152+
}
153+
// Create new template associated with the current one
154+
tmpl, err := slim.Parse(bytes.NewReader(buf))
155+
if err != nil {
156+
return err
157+
}
158+
// tmpl.FuncMap(e.funcmap)
159+
e.Templates[name] = tmpl
160+
// Debugging
161+
if e.debug {
162+
fmt.Printf("views: parsed template: %s\n", name)
163+
}
164+
return err
165+
}
166+
if e.fileSystem != nil {
167+
return utils.Walk(e.fileSystem, e.directory, walkFn)
168+
}
169+
return filepath.Walk(e.directory, walkFn)
170+
}
171+
172+
// Execute will render the template by name
173+
func (e *Engine) Render(out io.Writer, template string, binding interface{}, layout ...string) error {
174+
if e.reload {
175+
if err := e.Load(); err != nil {
176+
return err
177+
}
178+
}
179+
tmpl := e.Templates[template]
180+
if tmpl == nil {
181+
return fmt.Errorf("render: template %s does not exist", template)
182+
}
183+
if len(layout) > 0 {
184+
buf := bytebufferpool.Get()
185+
defer bytebufferpool.Put(buf)
186+
if err := tmpl.Execute(buf, binding); err != nil {
187+
return err
188+
}
189+
var bind map[string]interface{}
190+
if bind == nil {
191+
bind = make(map[string]interface{}, 1)
192+
} else if context, ok := binding.(map[string]interface{}); ok {
193+
bind = context
194+
} else {
195+
bind = make(map[string]interface{}, 1)
196+
}
197+
bind[e.layout] = buf.String()
198+
lay := e.Templates[layout[0]]
199+
if lay == nil {
200+
return fmt.Errorf("render: layout %s does not exist", layout[0])
201+
}
202+
return lay.Execute(out, bind)
203+
}
204+
return tmpl.Execute(out, binding)
205+
}

slim/slim_test.go

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
package slim
2+
3+
// import (
4+
// "bytes"
5+
// "io/ioutil"
6+
// "net/http"
7+
// "regexp"
8+
// "strings"
9+
// "testing"
10+
// )
11+
12+
// func trim(str string) string {
13+
// trimmed := strings.TrimSpace(regexp.MustCompile(`\s+`).ReplaceAllString(str, " "))
14+
// trimmed = strings.Replace(trimmed, " <", "<", -1)
15+
// trimmed = strings.Replace(trimmed, "> ", ">", -1)
16+
// return trimmed
17+
// }
18+
19+
// func Test_Render(t *testing.T) {
20+
// engine := New("./views", ".slim").Debug(true)
21+
// if err := engine.Load(); err != nil {
22+
// t.Fatalf("load: %v\n", err)
23+
// }
24+
// // Partials
25+
// var buf bytes.Buffer
26+
// if err := engine.Render(&buf, "index", map[string]interface{}{
27+
// "Title": "Hello, World!",
28+
// }); err != nil {
29+
// t.Fatalf("render: %v\n", err)
30+
// }
31+
// expect := `<h2>Header</h2><h1>Hello, World!</h1><h2>Footer</h2>`
32+
// result := trim(buf.String())
33+
// if expect != result {
34+
// t.Fatalf("Expected:\n%s\nResult:\n%s\n", expect, result)
35+
// }
36+
// // Single
37+
// buf.Reset()
38+
// engine.Render(&buf, "errors/404", map[string]interface{}{
39+
// "title": "Hello, World!",
40+
// })
41+
// expect = `<h1>Hello, World!</h1>`
42+
// result = trim(buf.String())
43+
// if expect != result {
44+
// t.Fatalf("Expected:\n%s\nResult:\n%s\n", expect, result)
45+
// }
46+
// }
47+
48+
// func Test_Layout(t *testing.T) {
49+
// engine := New("./views", ".slim")
50+
// engine.Debug(true)
51+
// if err := engine.Load(); err != nil {
52+
// t.Fatalf("load: %v\n", err)
53+
// }
54+
55+
// var buf bytes.Buffer
56+
// err := engine.Render(&buf, "index", map[string]interface{}{
57+
// "Title": "Hello, World!",
58+
// }, "layouts/main")
59+
// if err != nil {
60+
// t.Fatalf("render: %v", err)
61+
// }
62+
// expect := `<!DOCTYPE html><html><head><title>Main</title></head><body><h2>Header</h2><h1>Hello, World!</h1><h2>Footer</h2></body></html>`
63+
// result := trim(buf.String())
64+
// if expect != result {
65+
// t.Fatalf("Expected:\n%s\nResult:\n%s\n", expect, result)
66+
// }
67+
// }
68+
69+
// func Test_FileSystem(t *testing.T) {
70+
// engine := NewFileSystem(http.Dir("./views"), ".slim")
71+
// engine.Debug(true)
72+
// if err := engine.Load(); err != nil {
73+
// t.Fatalf("load: %v\n", err)
74+
// }
75+
76+
// var buf bytes.Buffer
77+
// err := engine.Render(&buf, "index", map[string]interface{}{
78+
// "Title": "Hello, World!",
79+
// }, "layouts/main")
80+
// if err != nil {
81+
// t.Fatalf("render: %v", err)
82+
// }
83+
// expect := `<!DOCTYPE html><html><head><title>Main</title></head><body><h2>Header</h2><h1>Hello, World!</h1><h2>Footer</h2></body></html>`
84+
// result := trim(buf.String())
85+
// if expect != result {
86+
// t.Fatalf("Expected:\n%s\nResult:\n%s\n", expect, result)
87+
// }
88+
// }
89+
90+
// func Test_Reload(t *testing.T) {
91+
// engine := NewFileSystem(http.Dir("./views"), ".ace")
92+
// engine.Reload(true) // Optional. Default: false
93+
94+
// engine.AddFunc("isAdmin", func(user string) bool {
95+
// return user == "admin"
96+
// })
97+
// if err := engine.Load(); err != nil {
98+
// t.Fatalf("load: %v\n", err)
99+
// }
100+
101+
// if err := ioutil.WriteFile("./views/reload.ace", []byte("after reload\n"), 0644); err != nil {
102+
// t.Fatalf("write file: %v\n", err)
103+
// }
104+
// defer func() {
105+
// if err := ioutil.WriteFile("./views/reload.ace", []byte("before reload\n"), 0644); err != nil {
106+
// t.Fatalf("write file: %v\n", err)
107+
// }
108+
// }()
109+
110+
// engine.Load()
111+
112+
// var buf bytes.Buffer
113+
// engine.Render(&buf, "reload", nil)
114+
// expect := "<after>reload</after>"
115+
// result := trim(buf.String())
116+
// if expect != result {
117+
// t.Fatalf("Expected:\n%s\nResult:\n%s\n", expect, result)
118+
// }
119+
// }

0 commit comments

Comments
 (0)