From ba90949d177bdb6cc7f6bb6d127654d222b09f69 Mon Sep 17 00:00:00 2001 From: Aymerick Date: Sun, 1 May 2016 16:06:14 +0200 Subject: [PATCH] fixes passing of context in helper options (fixes #2) --- helper.go | 2 +- helper_test.go | 28 ++++++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/helper.go b/helper.go index eb766c3..b71782b 100644 --- a/helper.go +++ b/helper.go @@ -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() } // diff --git a/helper_test.go b/helper_test.go index ccfab9b..b867e3f 100644 --- a/helper_test.go +++ b/helper_test.go @@ -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) + } +}