Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom Handlebars block helper

How do I define a custom block helper in Handlebars (for use with Ember.js)? I've tried using the method described on the Handlebars site, but it doesn't seem to work. I get this error from Ember.js:

Assertion failed: registerBoundHelper-generated helpers do not support use with Handlebars blocks. 

Here's the code for my helper. The idea is that the block will only be rendered if the models that I pass in are the same:

Ember.Handlebars.helper 'sameModel', (model1, model2, options) ->
    if model1 is model2
        new Handlebars.SafeString(options.fn(this))
    else
        ''
like image 918
GJK Avatar asked Jul 24 '13 20:07

GJK


People also ask

How do I make my own Handlebars helpers?

To create custom helpers in your theme just create a helpers. js file in the theme directory; here you can add your custom helper functions. Below there is an example of code which creates two super simple custom helpers which accept one argument: /* * Custom theme helpers for Handlebars.

Which function is use for creating custom helpers in Handlebars?

We can create our own helper functions in Handlebars using the registerHelper() method.

What is helper in Handlebars?

A Handlebars helper call is a simple identifier, followed by zero or more parameters (separated by a space). Each parameter is a Handlebars expression that is evaluated exactly the same way as described above in "Basic Usage": template {{firstname}} {{loud lastname}}

How do you escape curly braces with Handlebars?

Declare a template in the HTML file. Handlebars expressions are put into double curly braces {{expr}} for HTML-escaped content; otherwise, use triple curly brackets {{{expr}}} to avoid HTML-escaping.


2 Answers

6 months later, it looks like this is possible now, at least to a certain extent. You can view the discussion here. I agree with the pull request that this should usually be handled using computed properties, but this is still very useful in some cases.

I'm going to accept this answer to keep this post updated. If I've broken any SO etiquette, let me know. :)

like image 89
GJK Avatar answered Sep 21 '22 14:09

GJK


Assertion is correct. You cannot do that, at least not it RC6 and before.

You may want to create a view with a template and bind some properties to it:

some.hbs

{{#if model1}}
    This is model1 {{model1.name}}
{{/if}}

{{#if model2}}
    This is model2 {{model2.name}}
{{/if}}

views/some.js

App.SomeView = Ember.View.Extend({
    templateName: "some"
})

different template

<h3>{{view App.SomeView model1Binding=someModel1 model2Binding=someModel2}}</h3>
like image 35
chrmod Avatar answered Sep 19 '22 14:09

chrmod