1
0
mirror of https://github.com/kataras/iris.git synced 2026-01-10 05:25:58 +00:00
Former-commit-id: 292d155c877ba3f9d1210db54c3df3fedd1d0c1c
This commit is contained in:
Gerasimos (Makis) Maropoulos
2018-01-15 00:54:01 +02:00
parent ad9c76adb3
commit a19e570c8a
19 changed files with 154 additions and 47 deletions

View File

@@ -20,9 +20,8 @@ type AmberEngine struct {
namesFn func() []string // for embedded, in combination with directory & extension
reload bool
//
rmu sync.RWMutex // locks for funcs
rmu sync.RWMutex // locks for `ExecuteWiter` when `reload` is true.
funcs map[string]interface{}
mu sync.Mutex // locks for template files load
templateCache map[string]*template.Template
}
@@ -57,6 +56,10 @@ func (s *AmberEngine) Binary(assetFn func(name string) ([]byte, error), namesFn
// Reload if setted to true the templates are reloading on each render,
// use it when you're in development and you're boring of restarting
// the whole app when you edit a template file.
//
// Note that if `true` is passed then only one `View -> ExecuteWriter` will be render each time,
// no concurrent access across clients, use it only on development status.
// It's good to be used side by side with the https://github.com/kataras/rizla reloader for go source files.
func (s *AmberEngine) Reload(developmentMode bool) *AmberEngine {
s.reload = developmentMode
return s
@@ -116,8 +119,6 @@ func (s *AmberEngine) loadDirectory() error {
templates, err := amber.CompileDir(dir, opt, amber.DefaultOptions) // this returns the map with stripped extension, we want extension so we copy the map
if err == nil {
s.mu.Lock()
defer s.mu.Unlock()
s.templateCache = make(map[string]*template.Template)
for k, v := range templates {
name := filepath.ToSlash(k + opt.Ext)
@@ -155,9 +156,6 @@ func (s *AmberEngine) loadAssets() error {
}
amber.FuncMap = funcs //set the funcs
s.mu.Lock()
defer s.mu.Unlock()
names := namesFn()
for _, path := range names {
@@ -192,21 +190,22 @@ func (s *AmberEngine) loadAssets() error {
}
func (s *AmberEngine) fromCache(relativeName string) *template.Template {
s.mu.Lock()
tmpl, ok := s.templateCache[relativeName]
if ok {
s.mu.Unlock()
return tmpl
}
s.mu.Unlock()
return nil
}
// ExecuteWriter executes a template and writes its result to the w writer.
// layout here is useless.
func (s *AmberEngine) ExecuteWriter(w io.Writer, filename string, layout string, bindingData interface{}) error {
// reload the templates if reload configuration field is true
// re-parse the templates if reload is enabled.
if s.reload {
// locks to fix #872, it's the simplest solution and the most correct,
// to execute writers with "wait list", one at a time.
s.rmu.Lock()
defer s.rmu.Unlock()
if err := s.Load(); err != nil {
return err
}