1
0
mirror of https://github.com/kataras/iris.git synced 2025-12-30 08:17:18 +00:00

add 'iris.Ace' template engine: _examples/view/template_ace_0

This commit is contained in:
Gerasimos (Makis) Maropoulos
2020-08-03 05:46:04 +03:00
parent dfa08041a1
commit 6844be57ea
20 changed files with 197 additions and 52 deletions

57
view/ace.go Normal file
View File

@@ -0,0 +1,57 @@
package view
import (
"path"
"sync"
"github.com/yosssi/ace"
)
// Ace returns a new ace view engine.
// It shares the same exactly logic with the
// html view engine, it uses the same exactly configuration.
//
// Read more about the Ace Go Parser: https://github.com/yosssi/ace
func Ace(directory, extension string) *HTMLEngine {
s := HTML(directory, extension)
funcs := make(map[string]interface{}, 0)
once := new(sync.Once)
s.middleware = func(name string, text []byte) (contents string, err error) {
once.Do(func() { // on first template parse, all funcs are given.
for k, v := range s.funcs {
funcs[k] = v
}
for k, v := range emptyFuncs {
funcs[k] = v
}
})
name = path.Join(path.Clean(directory), name)
src := ace.NewSource(
ace.NewFile(name, text),
ace.NewFile("", []byte{}),
[]*ace.File{},
)
rslt, err := ace.ParseSource(src, nil)
if err != nil {
return "", err
}
t, err := ace.CompileResult(name, rslt, &ace.Options{
Extension: extension[1:],
FuncMap: funcs,
DelimLeft: s.left,
DelimRight: s.right,
})
if err != nil {
return "", err
}
return t.Lookup(name).Tree.Root.String(), nil
}
return s
}