forked from revel/revel
-
Notifications
You must be signed in to change notification settings - Fork 2
/
template_adapter_go.go
110 lines (98 loc) · 3.29 KB
/
template_adapter_go.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
package revel
import (
"html/template"
"io"
"log"
"strings"
)
const GO_TEMPLATE = "go"
// Adapter for Go Templates.
type GoTemplate struct {
*template.Template
engine *GoEngine
*TemplateView
}
// return a 'revel.Template' from Go's template.
func (gotmpl GoTemplate) Render(wr io.Writer, arg interface{}) error {
return gotmpl.Execute(wr, arg)
}
type GoEngine struct {
loader *TemplateLoader
templateSet *template.Template
// TemplatesBylowerName is a map from lower case template name to the real template.
templatesBylowerName map[string]*GoTemplate
splitDelims []string
CaseInsensitiveMode bool
}
func (i *GoEngine) ConvertPath(path string) string {
if i.CaseInsensitiveMode {
return strings.ToLower(path)
}
return path
}
func (i *GoEngine) Handles(templateView *TemplateView) bool{
return EngineHandles(i, templateView)
}
func (engine *GoEngine) ParseAndAdd(baseTemplate *TemplateView) error {
// If alternate delimiters set for the project, change them for this set
if engine.splitDelims != nil && baseTemplate.Location() == ViewsPath {
engine.templateSet.Delims(engine.splitDelims[0], engine.splitDelims[1])
} else {
// Reset to default otherwise
engine.templateSet.Delims("", "")
}
templateSource := string(baseTemplate.FileBytes)
lowerTemplateName := engine.ConvertPath(baseTemplate.TemplateName)
tpl, err := engine.templateSet.New(baseTemplate.TemplateName).Parse(templateSource)
if nil != err {
_, line, description := ParseTemplateError(err)
return &Error{
Title: "Template Compilation Error",
Path: baseTemplate.TemplateName,
Description: description,
Line: line,
SourceLines: strings.Split(templateSource, "\n"),
}
}
engine.templatesBylowerName[lowerTemplateName] = &GoTemplate{Template: tpl, engine: engine, TemplateView: baseTemplate}
return nil
}
func (engine *GoEngine) Lookup(templateName string) Template {
// Case-insensitive matching of template file name
if tpl, found := engine.templatesBylowerName[engine.ConvertPath(templateName)]; found {
return tpl
}
return nil
}
func (engine *GoEngine) Name() string {
return GO_TEMPLATE
}
func (engine *GoEngine) Event(action int, i interface{}) {
if action == TEMPLATE_REFRESH_REQUESTED {
// At this point all the templates have been passed into the
engine.templatesBylowerName = map[string]*GoTemplate{}
engine.templateSet = template.New("__root__").Funcs(TemplateFuncs)
// Check to see what should be used for case sensitivity
engine.CaseInsensitiveMode = Config.StringDefault("go.template.path", "lower") != "case"
}
}
func init() {
RegisterTemplateLoader(GO_TEMPLATE, func(loader *TemplateLoader) (TemplateEngine, error) {
// Set the template delimiters for the project if present, then split into left
// and right delimiters around a space character
TemplateDelims := Config.StringDefault("template.go.delimiters", "")
var splitDelims []string
if TemplateDelims != "" {
splitDelims = strings.Split(TemplateDelims, " ")
if len(splitDelims) != 2 {
log.Fatalln("app.conf: Incorrect format for template.delimiters")
}
}
return &GoEngine{
loader: loader,
templateSet: template.New("__root__").Funcs(TemplateFuncs),
templatesBylowerName: map[string]*GoTemplate{},
splitDelims: splitDelims,
}, nil
})
}