Skip to content

Commit a8dd97a

Browse files
authored
Merge pull request revel#1170 from notzippy/server-templates
Changes to revel to support a template engine architecture
2 parents fa40869 + 6dd3fbf commit a8dd97a

File tree

7 files changed

+657
-284
lines changed

7 files changed

+657
-284
lines changed

controller.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,8 @@ func (c *Controller) RenderTemplate(templatePath string) Result {
124124
c.setStatusIfNil(http.StatusOK)
125125

126126
// Get the Template.
127-
template, err := MainTemplateLoader.Template(templatePath)
127+
lang, _ := c.ViewArgs[CurrentLocaleViewArg].(string)
128+
template, err := MainTemplateLoader.TemplateLang(templatePath, lang)
128129
if err != nil {
129130
return c.RenderError(err)
130131
}

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.

results.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,11 @@ func (r ErrorResult) Apply(req *Request, resp *Response) {
4444
if contentType == DefaultFileContentType {
4545
contentType = "text/plain"
4646
}
47-
47+
lang, _ := r.ViewArgs[CurrentLocaleViewArg].(string)
4848
// Get the error template.
4949
var err error
5050
templatePath := fmt.Sprintf("errors/%d.%s", status, format)
51-
tmpl, err := MainTemplateLoader.Template(templatePath)
51+
tmpl, err := MainTemplateLoader.TemplateLang(templatePath, lang)
5252

5353
// This func shows a plaintext error message, in case the template rendering
5454
// doesn't work.
@@ -231,12 +231,13 @@ func (r *RenderTemplateResult) render(req *Request, resp *Response, wr io.Writer
231231
}
232232

233233
var templateContent []string
234-
templateName, line, description := parseTemplateError(err)
234+
templateName, line, description := ParseTemplateError(err)
235235
if templateName == "" {
236236
templateName = r.Template.Name()
237237
templateContent = r.Template.Content()
238238
} else {
239-
if tmpl, err := MainTemplateLoader.Template(templateName); err == nil {
239+
lang, _ := r.ViewArgs[CurrentLocaleViewArg].(string)
240+
if tmpl, err := MainTemplateLoader.TemplateLang(templateName, lang); err == nil {
240241
templateContent = tmpl.Content()
241242
}
242243
}

revel.go

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

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

2122
const (
2223
// RevelImportPath Revel framework import path
2324
RevelImportPath = "github.com/revel/revel"
2425
)
26+
const (
27+
// Called when templates are going to be refreshed (receivers are registered template engines added to the template.engine conf option)
28+
TEMPLATE_REFRESH_REQUESTED = iota
29+
// Called when templates are refreshed (receivers are registered template engines added to the template.engine conf option)
30+
TEMPLATE_REFRESH_COMPLETED
2531

32+
)
2633
type revelLogs struct {
2734
c gocolorize.Colorize
2835
w io.Writer
2936
}
3037

38+
type EventHandler func(typeOf int, value interface{}) (responseOf int)
39+
3140
func (r *revelLogs) Write(p []byte) (n int, err error) {
3241
return r.w.Write([]byte(r.c.Paint(string(p))))
3342
}
@@ -111,6 +120,7 @@ var (
111120
// Private
112121
secretKey []byte // Key used to sign cookies. An empty key disables signing.
113122
packaged bool // If true, this is running from a pre-built package.
123+
initEventList = []EventHandler{} // Event handler list for receiving events
114124
)
115125

116126
// Init initializes Revel -- it provides paths for getting around the app.
@@ -230,6 +240,25 @@ func Init(mode, importPath, srcPath string) {
230240
INFO.Printf("Initialized Revel v%s (%s) for %s", Version, BuildDate, MinimumGoVersion)
231241
}
232242

243+
// Fires system events from revel
244+
func fireEvent(key int, value interface{}) (response int) {
245+
for _, handler := range initEventList {
246+
response |= handler(key, value)
247+
}
248+
return
249+
}
250+
251+
// Add event handler to listen for all system events
252+
func AddInitEventHandler(handler EventHandler) {
253+
initEventList = append(initEventList, handler)
254+
return
255+
}
256+
257+
func SetSecretKey(newKey []byte) error {
258+
secretKey = newKey
259+
return nil
260+
}
261+
233262
// Create a logger using log.* directives in app.conf plus the current settings
234263
// on the default logger.
235264
func getLogger(name string) *log.Logger {
@@ -325,7 +354,17 @@ type Module struct {
325354
}
326355

327356
func loadModules() {
357+
keys := []string{}
328358
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 {
329368
moduleImportPath := Config.StringDefault(key, "")
330369
if moduleImportPath == "" {
331370
continue
@@ -335,7 +374,13 @@ func loadModules() {
335374
if err != nil {
336375
log.Fatalln("Failed to load module. Import of", moduleImportPath, "failed:", err)
337376
}
338-
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)
339384
}
340385
}
341386

0 commit comments

Comments
 (0)