Skip to content

Commit 97ebc2b

Browse files
committed
As requested
1 parent 87374f9 commit 97ebc2b

File tree

5 files changed

+85
-66
lines changed

5 files changed

+85
-66
lines changed

i18n.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ import (
1616
)
1717

1818
const (
19-
// CurrentLocaleRenderArg the key for the current locale render arg value
20-
CurrentLocaleRenderArg = "currentLocale"
19+
// CurrentLocaleViewArg the key for the current locale view arg value
20+
CurrentLocaleViewArg = "currentLocale"
2121

2222
messageFilesDirectory = "messages"
2323
messageFilePattern = `^\w+\.[a-zA-Z]{2}$`
@@ -202,7 +202,7 @@ func I18nFilter(c *Controller, fc []Filter) {
202202
// Set the current locale controller argument (CurrentLocaleControllerArg) with the given locale.
203203
func setCurrentLocaleControllerArguments(c *Controller, locale string) {
204204
c.Request.Locale = locale
205-
c.ViewArgs[CurrentLocaleRenderArg] = locale
205+
c.ViewArgs[CurrentLocaleViewArg] = locale
206206
}
207207

208208
// Determine whether the given request has valid Accept-Language value.

revel.go

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616

1717
"github.com/agtorre/gocolorize"
1818
"github.com/revel/config"
19+
"sort"
1920
)
2021

2122
const (
@@ -353,7 +354,17 @@ type Module struct {
353354
}
354355

355356
func loadModules() {
357+
keys := []string{}
356358
for _, key := range Config.Options("module.") {
359+
keys = append(keys, key)
360+
}
361+
// Reorder module order by key name, a poor mans sort but at least it is consistent
362+
sort.Strings(keys)
363+
for _, key := range keys {
364+
println("Sorted keys", key)
365+
366+
}
367+
for _, key := range keys {
357368
moduleImportPath := Config.StringDefault(key, "")
358369
if moduleImportPath == "" {
359370
continue
@@ -363,7 +374,13 @@ func loadModules() {
363374
if err != nil {
364375
log.Fatalln("Failed to load module. Import of", moduleImportPath, "failed:", err)
365376
}
366-
addModule(key[len("module."):], moduleImportPath, modulePath)
377+
// Drop anything between module.???.<name of module>
378+
subKey := key[len("module."):]
379+
if index := strings.Index(subKey, "."); index > -1 {
380+
subKey = subKey[index+1:]
381+
}
382+
383+
addModule(subKey, moduleImportPath, modulePath)
367384
}
368385
}
369386

template.go

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -33,18 +33,18 @@ type TemplateLoader struct {
3333
paths []string
3434
// Map from template name to the path from whence it was loaded.
3535
TemplatePaths map[string]string
36+
// A map of looked up template results
37+
TemplateMap map[string]Template
3638
}
3739

3840
type Template interface {
39-
// #Name: The name of the template.
41+
// The name of the template.
4042
Name() string // Name of template
41-
// #Content: The content of the template as a string (Used in error handling).
43+
// The content of the template as a string (Used in error handling).
4244
Content() []string // Content
43-
// #Render: Called by the server to render the template out the io.Writer, args contains the arguements to be passed to the template.
44-
// arg: wr io.Writer
45-
// arg: arg interface{}
46-
Render(wr io.Writer, arg interface{}) error
47-
// #Location: The full path to the file on the disk.
45+
// Called by the server to render the template out the io.Writer, context contains the view args to be passed to the template.
46+
Render(wr io.Writer, context interface{}) error
47+
// The full path to the file on the disk.
4848
Location() string // Disk location
4949
}
5050

@@ -80,6 +80,9 @@ func (loader *TemplateLoader) Refresh() (err *Error) {
8080
engine.Event(TEMPLATE_REFRESH_COMPLETE, nil)
8181
}
8282
fireEvent(TEMPLATE_REFRESH_COMPLETE, nil)
83+
// Reset the TemplateMap, we don't prepopulate the map because
84+
loader.TemplateMap = map[string]Template{}
85+
8386
}()
8487
// Resort the paths, make sure the revel path is the last path,
8588
// so anything can override it
@@ -237,7 +240,7 @@ func (loader *TemplateLoader) findAndAddTemplate(path, fullSrcDir, basePath stri
237240
return
238241
}
239242

240-
func (loader *TemplateLoader) loadIntoEngine(engine TemplateEngine, baseTemplate *BaseTemplate) (loaded bool, err error) {
243+
func (loader *TemplateLoader) loadIntoEngine(engine TemplateEngine, baseTemplate *TemplateView) (loaded bool, err error) {
241244
if loadedTemplate := engine.Lookup(baseTemplate.TemplateName); loadedTemplate != nil {
242245
// Duplicate template found for engine
243246
TRACE.Println("template already exists: ", baseTemplate.TemplateName, " in engine ", engine.Name(), "\r\n\told file:",
@@ -303,12 +306,17 @@ func (loader *TemplateLoader) Template(name string) (tmpl Template, err error) {
303306
return nil, loader.compileError
304307
}
305308

306-
// Look up and return the template.
307-
for _, engine := range loader.templatesAndEngineList {
308-
if tmpl = engine.Lookup(name); tmpl != nil {
309-
break
309+
if t,found := loader.TemplateMap[name];!found && t != nil {
310+
tmpl = t
311+
} else {
312+
// Look up and return the template.
313+
for _, engine := range loader.templatesAndEngineList {
314+
if tmpl = engine.Lookup(name); tmpl != nil {
315+
loader.TemplateMap[name] = tmpl
316+
break
317+
}
310318
}
311-
}
319+
}
312320

313321
if tmpl == nil && err == nil {
314322
err = fmt.Errorf("Template %s not found.", name)
@@ -317,18 +325,10 @@ func (loader *TemplateLoader) Template(name string) (tmpl Template, err error) {
317325
return
318326
}
319327

320-
type BaseTemplate struct {
321-
TemplateName string
322-
FilePath string
323-
BasePath string
324-
FileBytes []byte
325-
EngineType string
326-
}
327-
328-
func (i *BaseTemplate) Location() string {
328+
func (i *TemplateView) Location() string {
329329
return i.FilePath
330330
}
331-
func (i *BaseTemplate) Content() (content []string) {
331+
func (i *TemplateView) Content() (content []string) {
332332
if i.FileBytes != nil {
333333
// Parse the bytes
334334
buffer := bytes.NewBuffer(i.FileBytes)
@@ -339,8 +339,8 @@ func (i *BaseTemplate) Content() (content []string) {
339339
}
340340
return nil
341341
}
342-
func NewBaseTemplate(templateName, filePath, basePath string, fileBytes []byte) *BaseTemplate {
343-
return &BaseTemplate{TemplateName: templateName, FilePath: filePath, FileBytes: fileBytes, BasePath: basePath}
342+
func NewBaseTemplate(templateName, filePath, basePath string, fileBytes []byte) *TemplateView {
343+
return &TemplateView{TemplateName: templateName, FilePath: filePath, FileBytes: fileBytes, BasePath: basePath}
344344
}
345345

346346
/////////////////////

template_adapter_go.go

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,15 @@ var (
1717
// The functions available for use in the templates.
1818
TemplateFuncs = map[string]interface{}{
1919
"url": ReverseURL,
20-
"set": func(renderArgs map[string]interface{}, key string, value interface{}) template.JS {
21-
renderArgs[key] = value
20+
"set": func(viewArgs map[string]interface{}, key string, value interface{}) template.JS {
21+
viewArgs[key] = value
2222
return template.JS("")
2323
},
24-
"append": func(renderArgs map[string]interface{}, key string, value interface{}) template.JS {
25-
if renderArgs[key] == nil {
26-
renderArgs[key] = []interface{}{value}
24+
"append": func(viewArgs map[string]interface{}, key string, value interface{}) template.JS {
25+
if viewArgs[key] == nil {
26+
viewArgs[key] = []interface{}{value}
2727
} else {
28-
renderArgs[key] = append(renderArgs[key].([]interface{}), value)
28+
viewArgs[key] = append(viewArgs[key].([]interface{}), value)
2929
}
3030
return template.JS("")
3131
},
@@ -79,10 +79,10 @@ var (
7979
return template.HTML(html.EscapeString(str) + strings.Repeat("&nbsp;", width-len(str)))
8080
},
8181

82-
"errorClass": func(name string, renderArgs map[string]interface{}) template.HTML {
83-
errorMap, ok := renderArgs["errors"].(map[string]*ValidationError)
82+
"errorClass": func(name string, viewArgs map[string]interface{}) template.HTML {
83+
errorMap, ok := viewArgs["errors"].(map[string]*ValidationError)
8484
if !ok || errorMap == nil {
85-
WARN.Println("Called 'errorClass' without 'errors' in the render args.")
85+
WARN.Println("Called 'errorClass' without 'errors' in the view args.")
8686
return template.HTML("")
8787
}
8888
valError, ok := errorMap[name]
@@ -92,8 +92,8 @@ var (
9292
return template.HTML(ErrorCSSClass)
9393
},
9494

95-
"msg": func(renderArgs map[string]interface{}, message string, args ...interface{}) template.HTML {
96-
str, ok := renderArgs[CurrentLocaleRenderArg].(string)
95+
"msg": func(viewArgs map[string]interface{}, message string, args ...interface{}) template.HTML {
96+
str, ok := viewArgs[CurrentLocaleViewArg].(string)
9797
if !ok {
9898
return ""
9999
}
@@ -154,7 +154,7 @@ var (
154154
type GoTemplate struct {
155155
*template.Template
156156
engine *GoEngine
157-
*BaseTemplate
157+
*TemplateView
158158
}
159159

160160
// return a 'revel.Template' from Go's template.
@@ -168,10 +168,10 @@ type GoEngine struct {
168168
// TemplatesBylowerName is a map from lower case template name to the real template.
169169
templatesBylowerName map[string]*GoTemplate
170170
splitDelims []string
171-
BaseTemplateEngine
171+
TemplateEngineHelper
172172
}
173173

174-
func (engine *GoEngine) ParseAndAdd(baseTemplate *BaseTemplate) error {
174+
func (engine *GoEngine) ParseAndAdd(baseTemplate *TemplateView) error {
175175
// If alternate delimiters set for the project, change them for this set
176176
if engine.splitDelims != nil && baseTemplate.Location() == ViewsPath {
177177
engine.templateSet.Delims(engine.splitDelims[0], engine.splitDelims[1])
@@ -180,7 +180,7 @@ func (engine *GoEngine) ParseAndAdd(baseTemplate *BaseTemplate) error {
180180
engine.templateSet.Delims("", "")
181181
}
182182
templateSource := string(baseTemplate.FileBytes)
183-
baseTemplate.TemplateName = engine.ConvertPath(baseTemplate.TemplateName)
183+
lowerTemplateName := engine.ConvertPath(baseTemplate.TemplateName)
184184
tpl, err := engine.templateSet.New(baseTemplate.TemplateName).Parse(templateSource)
185185
if nil != err {
186186
_, line, description := ParseTemplateError(err)
@@ -192,13 +192,12 @@ func (engine *GoEngine) ParseAndAdd(baseTemplate *BaseTemplate) error {
192192
SourceLines: strings.Split(templateSource, "\n"),
193193
}
194194
}
195-
engine.templatesBylowerName[baseTemplate.TemplateName] = &GoTemplate{Template: tpl, engine: engine, BaseTemplate: baseTemplate}
195+
engine.templatesBylowerName[lowerTemplateName] = &GoTemplate{Template: tpl, engine: engine, TemplateView: baseTemplate}
196196
return nil
197197
}
198198

199199
func (engine *GoEngine) Lookup(templateName string) Template {
200200
// Case-insensitive matching of template file name
201-
202201
if tpl, found := engine.templatesBylowerName[engine.ConvertPath(templateName)]; found {
203202
return tpl
204203
}

template_engine.go

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10,28 +10,36 @@ import (
1010
)
1111

1212
type TemplateEngine interface {
13-
// #ParseAndAdd: prase template string and add template to the set.
14-
// arg: basePath *BaseTemplate
15-
ParseAndAdd(basePath *BaseTemplate) error
13+
// prase template string and add template to the set.
14+
ParseAndAdd(basePath *TemplateView) error
1615

17-
// #Lookup: returns Template corresponding to the given templateName
18-
// arg: templateName string
16+
// returns Template corresponding to the given templateName, or nil
1917
Lookup(templateName string) Template
2018

21-
// #Event: Fired by the template loader when events occur
22-
// arg: event string
23-
// arg: arg interface{}
19+
// Fired by the template loader when events occur
2420
Event(event int, arg interface{})
2521

26-
// #IsEngineFor: returns true if this engine should be used to parse the file specified in baseTemplate
27-
// arg: engine The calling engine
28-
// arg: baseTemplate The base template
29-
IsEngineFor(engine TemplateEngine, baseTemplate *BaseTemplate) bool
22+
// returns true if this engine should be used to parse the file specified in baseTemplate
23+
IsEngineFor(engine TemplateEngine, templateView *TemplateView) bool
3024

31-
// #Name: Returns the name of the engine
25+
// returns the name of the engine
3226
Name() string
3327
}
3428

29+
// The template view information
30+
type TemplateView struct {
31+
TemplateName string // The name of the view
32+
FilePath string // The file path (view relative)
33+
BasePath string // The file system base path
34+
FileBytes []byte // The file loaded
35+
EngineType string // The name of the engine used to render the view
36+
}
37+
38+
// Template engine helper
39+
type TemplateEngineHelper struct {
40+
CaseInsensitiveMode bool
41+
}
42+
3543
var templateLoaderMap = map[string]func(loader *TemplateLoader) (TemplateEngine, error){}
3644

3745
// Allow for templates to be registered during init but not initialized until application has been started
@@ -88,12 +96,7 @@ func (loader *TemplateLoader) InitializeEngines(templateEngineNameList string) (
8896
return
8997
}
9098

91-
// BaseTemplateEngine allows new engines to use the default
92-
type BaseTemplateEngine struct {
93-
CaseInsensitiveMode bool
94-
}
95-
96-
func (i *BaseTemplateEngine) IsEngineFor(engine TemplateEngine, description *BaseTemplate) bool {
99+
func (i *TemplateEngineHelper) IsEngineFor(engine TemplateEngine, description *TemplateView) bool {
97100
if line, _, e := bufio.NewReader(bytes.NewBuffer(description.FileBytes)).ReadLine(); e == nil && string(line[:3]) == "#! " {
98101
// Extract the shebang and look at the rest of the line
99102
// #! pong2
@@ -117,7 +120,7 @@ func (i *BaseTemplateEngine) IsEngineFor(engine TemplateEngine, description *Bas
117120
}
118121
return false
119122
}
120-
func (i *BaseTemplateEngine) ConvertPath(path string) string {
123+
func (i *TemplateEngineHelper) ConvertPath(path string) string {
121124
if i.CaseInsensitiveMode {
122125
return strings.ToLower(path)
123126
}

0 commit comments

Comments
 (0)