forked from revel/modules
-
Notifications
You must be signed in to change notification settings - Fork 0
/
init.go
144 lines (121 loc) · 3.95 KB
/
init.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package app
import (
"fmt"
"html/template"
"io"
"strings"
"github.com/revel/revel"
"github.com/yosssi/ace"
)
const ACE_TEMPLATE = "ace"
// Adapter for Go Templates.
type AceTemplate struct {
*template.Template
engine *AceEngine
*revel.TemplateView
File *ace.File
Inner *ace.File
}
// A bit trick of an implementation
// If the arg contains an ace_inner field then that will be used
// to fetch a new template.
func (acetmpl AceTemplate) Render(wr io.Writer, arg interface{}) error {
// We can redirect this render to another template if the arguments contain ace_content in them
if argmap, ok := arg.(map[string]interface{}); ok {
if acecontentraw, ok := argmap["ace-inner"]; ok {
acecontent := acecontentraw.(string)
newtemplatename := acetmpl.TemplateName + "-" + acecontent
// Now lookup the template again
if _, ok := acetmpl.engine.templatesByName[newtemplatename]; !ok {
inner, ok := acetmpl.engine.templatesByName[acecontent]
if ok {
return fmt.Errorf("inner content %s not found in ace templates", acecontent)
}
acetmpl.engine.templatesByName[newtemplatename] = &AceTemplate{
File: acetmpl.File,
Inner: inner.File,
engine: acetmpl.engine,
TemplateView: acetmpl.TemplateView,
}
}
return acetmpl.engine.templatesByName[newtemplatename].renderInternal(wr, arg)
}
}
return acetmpl.renderInternal(wr, arg)
}
func (acetmpl AceTemplate) renderInternal(wr io.Writer, arg interface{}) error {
if acetmpl.Template == nil {
// Compile the template first
if acetmpl.Inner == nil {
acetmpl.Inner = ace.NewFile("", nil)
}
source := ace.NewSource(acetmpl.File, acetmpl.Inner, acetmpl.engine.files)
result, err := ace.ParseSource(source, acetmpl.engine.Options)
if err != nil {
return err
}
gtemplate, err := ace.CompileResult(acetmpl.TemplateName, result, acetmpl.engine.Options)
if err != nil {
return err
}
acetmpl.Template = gtemplate
}
return acetmpl.Execute(wr, arg)
}
type AceEngine struct {
loader *revel.TemplateLoader
templatesByName map[string]*AceTemplate
files []*ace.File
Options *ace.Options
CaseInsensitive bool
}
func (engine *AceEngine) ConvertPath(path string) string {
if engine.CaseInsensitive {
return strings.ToLower(path)
}
return path
}
func (engine *AceEngine) Handles(templateView *revel.TemplateView) bool {
return revel.EngineHandles(engine, templateView)
}
func (engine *AceEngine) ParseAndAdd(baseTemplate *revel.TemplateView) error {
// Ace templates must only render views specified for it (no trial and error)
if baseTemplate.EngineType != ACE_TEMPLATE {
return &revel.Error{
Title: "Template Compilation Error",
Path: baseTemplate.FilePath,
Description: "Not correct template for engine",
Line: 1,
SourceLines: baseTemplate.Content(),
}
}
baseTemplate.TemplateName = engine.ConvertPath(baseTemplate.TemplateName)
file := ace.NewFile(baseTemplate.TemplateName, baseTemplate.FileBytes)
engine.files = append(engine.files, file)
engine.templatesByName[baseTemplate.TemplateName] = &AceTemplate{File: file, engine: engine, TemplateView: baseTemplate}
return nil
}
func (engine *AceEngine) Lookup(templateName string) revel.Template {
if tpl, found := engine.templatesByName[engine.ConvertPath(templateName)]; found {
return tpl
}
return nil
}
func (engine *AceEngine) Name() string {
return ACE_TEMPLATE
}
func (engine *AceEngine) Event(action revel.Event, i interface{}) {
if action == revel.TEMPLATE_REFRESH_REQUESTED {
engine.templatesByName = map[string]*AceTemplate{}
engine.CaseInsensitive = revel.Config.BoolDefault("ace.template.caseinsensitive", true)
}
}
func init() {
revel.RegisterTemplateLoader(ACE_TEMPLATE, func(loader *revel.TemplateLoader) (revel.TemplateEngine, error) {
return &AceEngine{
loader: loader,
templatesByName: map[string]*AceTemplate{},
Options: &ace.Options{FuncMap: revel.TemplateFuncs},
}, nil
})
}