Skip to content

Commit

Permalink
fixes passing of context in helper options (fixes aymerick#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
aymerick committed May 1, 2016
1 parent 4785153 commit ba90949
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
2 changes: 1 addition & 1 deletion helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ func (options *Options) ValueStr(name string) string {

// Ctx returns current evaluation context.
func (options *Options) Ctx() interface{} {
return options.eval.curCtx()
return options.eval.curCtx().Interface()
}

//
Expand Down
28 changes: 28 additions & 0 deletions helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,3 +163,31 @@ func TestHelper(t *testing.T) {

launchTests(t, helperTests)
}

//
// Fixes: https://github.com/aymerick/raymond/issues/2
//

type Author struct {
FirstName string
LastName string
}

func TestHelperCtx(t *testing.T) {
RegisterHelper("template", func(name string, options *Options) SafeString {
context := options.Ctx()

template := name + " - {{ firstName }} {{ lastName }}"
result, _ := Render(template, context)

return SafeString(result)
})

template := `By {{ template "namefile" }}`
context := Author{"Alan", "Johnson"}

result, _ := Render(template, context)
if result != "By namefile - Alan Johnson" {
t.Errorf("Failed to render template in helper: %q", result)
}
}

0 comments on commit ba90949

Please sign in to comment.