Mikechaos
Mikechaos

Reputation: 361

Injecting a global helper into all underscore templates with Marionette

This was originally the second part of an other question which I was able to isolate into it's own question for better search indexing. It might be helpful to refer to it for more specific context.

My question is : How to elegantly inject a global view helper into all underscore templates

This questions finds it's inspiration directly from i18next documentation. It is too late in the project to switch to handlebars templates but I, by far, prefer the way it is handled with the registerHelper style. Searching on that side, I came across Derick Bailey's View Helpers article, which, as usual, provide a very neat solution to an inelegant problem.

In our architecture, we decided to re-implement serializeData for all our views and not rely on the default implementation (exporting all the attributes from the model the view is bound with). My take on this one is if I wanted to get anything close to the registerHelper style, I would have to extend the base Marionette.View class and provide a basic helper that is injected into all the views, by inheritance.
This doesn't seem even close to the cleanliness of handlebars helpers, giving it working at all, which at this point seems nothing but certain.

Providing any examples or any hints on which path to follow to tackle this issue would be greatly appreciated.

Thank you.

[edit]

I found a solution to this exact problem, detailed in my previous question. Simon also posted an answer that is also likely to work nicely.

Upvotes: 2

Views: 910

Answers (1)

Simon
Simon

Reputation: 1754

Why don't you just use Handlebars with Marionette? You can use any template library you like. Check out this or write your own integration based on the Marionette docs. I've done this myself to integrate DustJS with Marionette.

EDIT

Something that could work nicely is creating a templateHelpers property on the underscore object. Then you could do something like:

<span><%= _.templateHelpers.someHelper(data) %></span>

Upvotes: 3

Related Questions