I’ve been scratching my head on this one for a bit. I’m trying to write a block helper that wraps some content with some additional HTML, but when I call the block it seems to render directly to the view. Here’s a fiddle:
will just render the contents of the helper to the buffer. To wrap the content you can push to the buffer before and after calling options.fn(). Not sure if this is the best solution, but it seems to work. Here’s a working fiddle:
This topic highlights an important point with helpers that needs coverage in the docs:
The preferred way to register helpers in Ember is with Ember.Handlebars.helper as seen in the guides. This delegates down to Ember.Handlebars.registerBoundHelper under the hood, ensuring data bindings and observers are wired up correctly.
However, Ember.Handlebars.helper does not support blocks when used in this form:
This is not strictly equivalent to the previous example, as the additional view will have a tag of its own that can be determined with tagName and other Ember.View options, but it sounds like that might be what you’re after anyway.
Just checked, and both the options.data.buffer.push approach and custom view approach work fine with bindings. They’re both good to use, so I guess it depends on the desired HTML output as to which you go for. Although the former is not officially documented, but perhaps it should be.
@jgwhite wow, that’s very clean! Although ideally I could pull the template from HTML, like…
<script type="text/x-handlebars" data-template-name="wrap">
before {{yield}} after
<script>
App.WrapView = Ember.View.extend({
layout: Ember.TEMPLATES['wrap']
});
But that doesn’t work.
I also neglected to mention that my helper is a conditional helper, so I don’t see how I can just write out the template as a string like in your example. I would need to do something like this:
Which also doesn’t work. @rmbrad your buffer flushing solution works, so thank you! I can definitely move forward. Feels wrong to use an undocumented call, and I would love to get the code into a template/view as @jgwhite was suggesting. But I can’t see how.
Will take another look in the future if I have time.