Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

prevent parent context changes #76

Merged
merged 2 commits into from
Nov 17, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions partial_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ func partialHelper(name string, data map[string]interface{}, help HelperContext)
if help.Context == nil || help.Context.data == nil {
return "", errors.New("invalid context. abort")
}

help.Context = help.New()
for k, v := range data {
help.Set(k, v)
}
Expand Down
43 changes: 42 additions & 1 deletion partial_helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,47 @@ func Test_PartialHelper_With_Data(t *testing.T) {
r.Equal(`<div class="test">Hello Yonghwan</div>`, string(html))
}

func Test_PartialHelper_With_InternalChange(t *testing.T) {
r := require.New(t)

name := "index"
data := map[string]interface{}{}
help := HelperContext{Context: NewContextWith(map[string]interface{}{
"number": 3,
})}
help.Set("partialFeeder", func(string) (string, error) {
return `<% let number = number - 1
%><div class="test">Hello <%= number %></div>`, nil
})

html, err := partialHelper(name, data, help)
r.NoError(err)
r.Equal(`<div class="test">Hello 2</div>`, string(html))
r.Equal(3, help.Value("number"))
}

func Test_PartialHelper_With_Recursion(t *testing.T) {
r := require.New(t)

name := "index"
data := map[string]interface{}{}
help := HelperContext{Context: NewContextWith(map[string]interface{}{
"number": 3,
})}
help.Set("partialFeeder", func(string) (string, error) {
return `<%=
if (number > 0) { %><%
let number = number - 1 %><%=
partial("index") %><%= number %>, <%
} %>`, nil
})

html, err := partialHelper(name, data, help)
r.NoError(err)
r.Equal(`0, 1, 2, `, string(html))
r.Equal(3, help.Value("number"))
}

func Test_PartialHelper_Render_Error(t *testing.T) {
r := require.New(t)

Expand Down Expand Up @@ -187,4 +228,4 @@ func Test_PartialHelper_Javascript_With_HTML(t *testing.T) {
html, err := partialHelper(name, data, help)
r.NoError(err)
r.Equal(`alert(\'\\\'Hello\\\'\');`, string(html))
}
}