aymerick / raymond

Handlebars for golang
MIT License
610 stars 101 forks source link

Set the current context onto the newIterDataFrame. #11

Closed markbates closed 6 years ago

markbates commented 7 years ago

Currently it is not possible to get access to the "parent" context from a helper inside of an each block. This PR solves that problem.

Assumptions:

// assuming this as the context for a template
map[string]interface{}{
  "name": "Mark",
  "items": []string{"item1", "item2"},
}
<!-- assuming this template -->
{{#each item as |items|}}
  {{ some_helper }}
{{/each}}

Current situation:

// this function no longer has access to the "name" value from the original context.
func someHelper(options *raymond.Options) string {
  return options.Data("name").(string)
}

This PR's solution:

// this function can get the "parent" context from the current DataFrame and
// the get access to the information it needs.
func someHelper(options *raymond.Options) string {
  ctx := options.DataFrame().Get("parent").(map[string]interface{})
  return ctx["name"].(string)
}

This PR does not change the public API. Ideally it would be great to have something like options.RootContext() that would return the context that was originally passed in to the parser/renderer.