I want to use one helper function in another helper function. In below code I want to highlight last name if it contains "Finch" word. I have writtern helper class for that. If we use in hbs file then syntax would be {{highlight name}}. But how to use it since I have to use it in another helper class.
Below is my code:
Handlebars.registerHelper('fullName', function(person) {
return person.firstName + " " + person.lastName;
});
Handlebars.registerHelper('highlight', function(person) {
var item = (person.lastName).replace('Finch', '<span style="color: red">'
+ Finch + '</span>');
return new Handlebars.SafeString(item);
});
Here is the working fiddle : http://jsfiddle.net/wC6JT/4/
Here is the fiddle where "highlight" helper is called.: http://jsfiddle.net/wC6JT/3/ . This will not produce any results since we will get console errors for person.lastName not recognized in "highlight" register helper.
I want to use "highlight" helper in fullname helper for person.lastName. How can this be acheived.
Extract the contents of the method you want to use in the first method into its own javascript method. Then call that javascript method in both helpers as necessary.
You can't do it unless you refactor the contents of one of the methods into its own javascript method.
So in your case it should look something like this:
Handlebars.registerHelper('fullName', function(person) {
return person.firstName + " " + highlightJavascript(person);
});
Handlebars.registerHelper('highlight', highlightJavascript);
highlightJavascript : function(person) {
var item = (person.lastName).replace('Finch', '<span style="color: red">'
+ Finch + '</span>');
return new Handlebars.SafeString(item);
}