Using Handlerbars helpers in backbone.marionette.handlebars

I have a web application using backbone.marionette with handlebars templating provided via the extension backbone.marionette.handlebars. This is working well but I want to now add some templateHelpers to help with formatting and I can't seem to get it to work.

I have included template helpers in what I believe is the correct way (see below) but I get and error from require.js saying it "GET .../templates/helpers/foo.js 404 (NotFound)" as soon as the backbone.marionette application starts.

My template looks like this (UPDATED: Thanks Billy Chan)

<div>{{{foo myData}}}</div>

And I have tried two things. Firstly defining a simple helper inside my Marionette.ItemView as so:

...
return Marionette.ItemView.extend({
    template: template,
    templateHelpers: {
        foo: function (someData) { return 'foo and ' + someData; }
    },
...

This didn't work, i.e. I still got the require.js error given in the first paragraph. (Note: I did try without a function argument in case that was the problem but didn't fix it).

The second thing I did was add a simple file .../templates/helpers/foo.js with a require.js define returning an object with a function foo inside it. This gave the same error as before, i.e. missing file. That might be because I need to define this file as a dependency. I would prefer the first method which does not need an external file, but happy to use this method if that is the correct way.

Finally, I noticed this issue on the backbone.marionette.handlebars github. This suggest that templateHelpers work, but might have a problem. Any suggestions on using helpers in this environment would be very much appreciated.

UPDATE

I bit more experimenting shows that if I have {{foo}} or {{{foo}}} then it calls the foo function in Marionette.ItemView.extend but doesn't insert the return of foo. However if I have {{foo myData}} or {{{foo myData}}} then it gives the 404 Not found error listed above. Strange.

Just for completeness I am using

  • backbone 1.0.0
  • backbone.marionette 1.0.4
  • backbone.marionette.handlebars 0.2.0.

Answers


Ok, I took @BillyChan's suggestion and started digging into the code. Following the rabbit trail it turns out that another plugin was handling the helpers. The single page application, which I didn't design, uses a plugin called require-handlebars-plugin to integrate handlebars into require.

This plugin knows nothing about Backbone.Marionette so it implements its own registerHelper's convention using require, which, you guessed it, expects the helper to be in .../templates/helpers/xxx.js in a specific format. This explains why I was getting my file 404, or even if the file was there it wasn't in the right format.

So thanks again for the comments, but my mistake as I was looking at the wrong documentation. I hadn't used helpers before so I thought I had set it up incorrectly, which I had, but not in the way that the Handlebars and Backbone.Marionette documentation I was looking at was suggesting. I am wiser now.


Need Your Help

Can't install or update redhat-lsb package for RHEL 5

linux redhat rhel rhel5 lsb

I moved a daemon shell-script from a Devian server to a RHEL 5 server. The script now throws the error :

Wordpress wp_query custom post type query not working on second page

wordpress conflict custom-post-type

Using below code i'm listing all posts under custom post type people