Skip to content

Commit

Permalink
added check and nice error message for missing block (#56)
Browse files Browse the repository at this point in the history
  • Loading branch information
matryer authored and markbates committed Jun 11, 2018
1 parent 00bff23 commit 6cd5bf0
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
5 changes: 4 additions & 1 deletion content_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ func contentForHelper(name string, help HelperContext) {
<%= contentOf("buttons", {"label": "Click me"}) %>
*/
func contentOfHelper(name string, data map[string]interface{}, help HelperContext) (template.HTML, error) {
fn := help.Value("contentFor:" + name).(func(data map[string]interface{}) (template.HTML, error))
fn, ok := help.Value("contentFor:" + name).(func(data map[string]interface{}) (template.HTML, error))
if !ok {
return template.HTML(""), errors.WithStack(errors.New("missing contentOf block: " + name))
}
return fn(data)
}
10 changes: 10 additions & 0 deletions content_helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,13 @@ func Test_ContentForOfWithData(t *testing.T) {
r.Contains(s, "<b2><button>Button Two</button></b2>")
r.Contains(s, "<b3>Outer label</b3>", "the outer label shouldn't be affected by the map passed in")
}

func Test_ContentForOf_MissingBlock(t *testing.T) {
r := require.New(t)
input := `
<b1><%= contentOf("buttons") %></b1>
<b2><%= contentOf("buttons") %></b2>
`
_, err := Render(input, NewContext())
r.EqualError(err, "line 2: missing contentOf block: buttons")
}

0 comments on commit 6cd5bf0

Please sign in to comment.