Comma Separate String using handlebars.js -
these days working working handlerbars.js , seems pretty interesting. in there need small guys.
i have following json input
"alerts": [ { "alert_description": "alcohol", }, { "alert_description": "diagnosis", } ]
and need write template create following comma separated string.
alcohol, diagnosis
i able print these values using line line using following template.
<div> <div> {{#each this}} <span>{{alert_description}}</span> {{/each}} </div> </div>
can guys please me solve issue? appreciate help
thanks keth
for kind of scenario, create helper.
here implementation
handlebars.registerhelper('commalist', function(items, options) { var out = ''; for(var i=0, l=items.length; i<l; i++) { out = out + options.fn(items[i]) + (i!==(l-1) ? ",":""); } return out; });
and template
<div> <div> {{#commalist this}} <span>{{alert_description}}</span> {{/commalist}} </div> </div>
in fact, helper here comma. can still put html inside helper (span, div, ...)
Comments
Post a Comment