How to call one user helper function in another custom helper

I want to use one helper function in another helper function. In the code below, I want to highlight the last name if it contains the word "Finch". For this, I have a helper class. If we use the hbs file, then the syntax will 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 a working fiddle: http://jsfiddle.net/wC6JT/4/

Here is the fiddle where the helper helper is called: http://jsfiddle.net/wC6JT/3/ . This will not give any results, since we will get console errors for person.lastName that are not recognized in the auxiliary element "highlight".

I want to use the "highlight" helper in fullname helper for person.lastName. How can this be achieved.

+7
javascript
source share
3 answers

Extract the contents of the method that you want to use in the first method into your own javascript method. Then call this javascript method in both helpers as needed.

You cannot do this unless you reformat the contents of one of the methods into your 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); } 
+4
source share

To call the Handlebars helper from another function, you can use Handlebars.helpers :

 Handlebars.registerHelper('fullName', function(person) { var lastName = Handlebars.helpers.highlight.apply(this, [person.lastName]); var firstName = Handlebars.Utils.escapeExpression(person.firstName); return new Handlebars.SafeString(firstName + " " + lastName); }); Handlebars.registerHelper('highlight', function(str) { var safeStr = Handlebars.Utils.escapeExpression(str); var item = safeStr.replace("Finch", "<em>Finch</em>"); return new Handlebars.SafeString(item); }); 

Here is a working fiddle: http://jsfiddle.net/acLcsL6h/1/

Read this blog post for another example.

+2
source share

you can use this method: http://goo.gl/oY4IIO no need to concatenate the string.

 <script id="tmp" type="text/x-handlebars-template"> <p>test: {{test "2.3333333"}}</p> <p>format: {{format "2.3333333"}}</p> </script> 

 Handlebars.registerHelper('format', function (value) { return parseFloat(value).toFixed(2); }); Handlebars.registerHelper('test', function (value) { var source = '{{format x}}'; var context = {x:value}; var html = Handlebars.compile(source)(context); return new Handlebars.SafeString(html); }); $(document).ready(function () { var source = $('#tmp').html(); var template = Handlebars.compile(source); var html = template(); $('#main').html(html); }); 

Output: Test: 2.33 Format: 2.33

+1
source share

All Articles