Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Handlebars : What is the best way to pass multiple parameters to a registerHelper function?

I am using Handlebars to render data in a table.

One of the data items needs processing, which takes a few parameters into account in order to provide outcome.

The templated text example :

{{getOutputByParameters param1=DataFieldName1 param2=DataFieldName2}}

And corresponding registerHelper would be written as:

var __this = this;
Handlebars.registerHelper('getOutputByParameters', function(params){ __this.getOutputByParameters(params.hash.param1, params.hash.param2)})

I figured that handlebars would pass an array of these parameters, which I can access using hash property.

But is that the only best way?

like image 391
dhruvpatel Avatar asked Jul 25 '13 23:07

dhruvpatel


People also ask

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.

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}}

Which syntax is used by handlebars to indicate that it should incorporate other handlebars file when rendering a page?

Any comments that must contain }} or other handlebars tokens should use the {{! -- --}} syntax.

Which mapping uses handlebars expressions?

Handlebars expressions may be used for: Mapping Export and Import application fields; Performing dynamic arithmetic calculations on the values being exported; Performing dynamic encoding and decoding of data during integration.


1 Answers

Nope. As per their document you can do this in many ways. I would do this way.

{{getOutputByParameters param1 param2}}

Handlebars.registerHelper('getOutputByParameters', function(param1, param2){ 
//do your stuff here.
})

jsFiddle

like image 187
user10 Avatar answered Sep 21 '22 07:09

user10