Skip to content

Commit

Permalink
render-args = viewArgs
Browse files Browse the repository at this point in the history
  • Loading branch information
pedromorgan committed Mar 12, 2017
1 parent 0e454f2 commit 5e0825a
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 25 deletions.
18 changes: 9 additions & 9 deletions controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func (c *Controller) setStatusIfNil(status int) {
}

// Render a template corresponding to the calling Controller method.
// Arguments will be added to c.RenderArgs prior to rendering the template.
// Arguments will be added to c.ViewArgs prior to rendering the template.
// They are keyed on their local identifier.
//
// For example:
Expand All @@ -91,7 +91,7 @@ func (c *Controller) setStatusIfNil(status int) {
//
// This action will render views/Users/ShowUser.html, passing in an extra
// key-value "user": (User).
func (c *Controller) Render(extraRenderArgs ...interface{}) Result {
func (c *Controller) Render(extraViewArgs ...interface{}) Result {
c.setStatusIfNil(http.StatusOK)

// Get the calling function name.
Expand All @@ -100,15 +100,15 @@ func (c *Controller) Render(extraRenderArgs ...interface{}) Result {
ERROR.Println("Failed to get Caller information")
}

// Get the extra RenderArgs passed in.
// Get the extra ViewArgs passed in.
if renderArgNames, ok := c.MethodType.RenderArgNames[line]; ok {
if len(renderArgNames) == len(extraRenderArgs) {
for i, extraRenderArg := range extraRenderArgs {
c.RenderArgs[renderArgNames[i]] = extraRenderArg
if len(renderArgNames) == len(extraViewArgs) {
for i, extraRenderArg := range extraViewArgs {
c.ViewArgs[renderArgNames[i]] = extraRenderArg
}
} else {
ERROR.Println(len(renderArgNames), "RenderArg names found for",
len(extraRenderArgs), "extra RenderArgs")
len(extraViewArgs), "extra ViewArgs")
}
} else {
ERROR.Println("No RenderArg names found for Render call on line", line,
Expand All @@ -119,7 +119,7 @@ func (c *Controller) Render(extraRenderArgs ...interface{}) Result {
}

// RenderTemplate method does less magical way to render a template.
// Renders the given template, using the current RenderArgs.
// Renders the given template, using the current ViewArgs.
func (c *Controller) RenderTemplate(templatePath string) Result {
c.setStatusIfNil(http.StatusOK)

Expand All @@ -131,7 +131,7 @@ func (c *Controller) RenderTemplate(templatePath string) Result {

return &RenderTemplateResult{
Template: template,
RenderArgs: c.RenderArgs,
ViewArgs: c.ViewArgs,
}
}

Expand Down
2 changes: 1 addition & 1 deletion flash.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func (f Flash) Success(msg string, args ...interface{}) {
// The name of the Flash cookie is set as CookiePrefix + "_FLASH".
func FlashFilter(c *Controller, fc []Filter) {
c.Flash = restoreFlash(c.Request.Request)
c.RenderArgs["flash"] = c.Flash.Data
c.ViewArgs["flash"] = c.Flash.Data

fc[0](c, fc[1:])

Expand Down
2 changes: 1 addition & 1 deletion i18n.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ func I18nFilter(c *Controller, fc []Filter) {
// Set the current locale controller argument (CurrentLocaleControllerArg) with the given locale.
func setCurrentLocaleControllerArguments(c *Controller, locale string) {
c.Request.Locale = locale
c.RenderArgs[CurrentLocaleRenderArg] = locale
c.ViewArgs[CurrentLocaleRenderArg] = locale
}

// Determine whether the given request has valid Accept-Language value.
Expand Down
4 changes: 2 additions & 2 deletions invoker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ func BenchmarkSetAction(b *testing.B) {
RegisterController((*Mixin2)(nil), []*MethodType{{Name: "Method"}})
RegisterController((*Benchmark)(nil), []*MethodType{{Name: "Method"}})
c := Controller{
RenderArgs: make(map[string]interface{}),
ViewArgs: make(map[string]interface{}),
}

for i := 0; i < b.N; i++ {
Expand All @@ -118,7 +118,7 @@ func BenchmarkSetAction(b *testing.B) {
func BenchmarkInvoker(b *testing.B) {
startFakeBookingApp()
c := Controller{
RenderArgs: make(map[string]interface{}),
ViewArgs: make(map[string]interface{}),
}
if err := c.SetAction("Hotels", "Show"); err != nil {
b.Errorf("Failed to set action: %s", err)
Expand Down
20 changes: 10 additions & 10 deletions results.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ type Result interface {
// It renders the relevant error page (errors/CODE.format, e.g. errors/500.json).
// If RunMode is "dev", this results in a friendly error page.
type ErrorResult struct {
RenderArgs map[string]interface{}
ViewArgs map[string]interface{}
Error error
}

Expand Down Expand Up @@ -82,16 +82,16 @@ func (r ErrorResult) Apply(req *Request, resp *Response) {
panic("no error provided")
}

if r.RenderArgs == nil {
r.RenderArgs = make(map[string]interface{})
if r.ViewArgs == nil {
r.ViewArgs = make(map[string]interface{})
}
r.RenderArgs["RunMode"] = RunMode
r.RenderArgs["Error"] = revelError
r.RenderArgs["Router"] = MainRouter
r.ViewArgs["RunMode"] = RunMode
r.ViewArgs["Error"] = revelError
r.ViewArgs["Router"] = MainRouter

// Render it.
var b bytes.Buffer
err = tmpl.Render(&b, r.RenderArgs)
err = tmpl.Render(&b, r.ViewArgs)

// If there was an error, print it in plain text.
if err != nil {
Expand Down Expand Up @@ -130,7 +130,7 @@ func (r PlaintextErrorResult) Apply(req *Request, resp *Response) {
// a template be rendered.
type RenderTemplateResult struct {
Template Template
RenderArgs map[string]interface{}
ViewArgs map[string]interface{}
}

func (r *RenderTemplateResult) Apply(req *Request, resp *Response) {
Expand Down Expand Up @@ -225,7 +225,7 @@ func (r *RenderTemplateResult) Apply(req *Request, resp *Response) {
}

func (r *RenderTemplateResult) render(req *Request, resp *Response, wr io.Writer) {
err := r.Template.Render(wr, r.RenderArgs)
err := r.Template.Render(wr, r.ViewArgs)
if err == nil {
return
}
Expand All @@ -249,7 +249,7 @@ func (r *RenderTemplateResult) render(req *Request, resp *Response, wr io.Writer
}
resp.Status = 500
ERROR.Printf("Template Execution Error (in %s): %s", templateName, description)
ErrorResult{r.RenderArgs, compileError}.Apply(req, resp)
ErrorResult{r.ViewArgs, compileError}.Apply(req, resp)
}

type RenderHTMLResult struct {
Expand Down
2 changes: 1 addition & 1 deletion session.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ func SessionFilter(c *Controller, fc []Filter) {
sessionWasEmpty := len(c.Session) == 0

// Make session vars available in templates as {{.session.xyz}}
c.RenderArgs["session"] = c.Session
c.ViewArgs["session"] = c.Session

fc[0](c, fc[1:])

Expand Down
2 changes: 1 addition & 1 deletion validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ func ValidationFilter(c *Controller, fc []Filter) {

fc[0](c, fc[1:])

// Add Validation errors to RenderArgs.
// Add Validation errors to ViewArgs.
c.ViewArgs["errors"] = c.Validation.ErrorMap()

// Store the Validation errors
Expand Down

0 comments on commit 5e0825a

Please sign in to comment.