2022.2

Table Of Contents
ExamplesofcustomHelpers
Coloring a negative value
Valuesinyourdatacouldbepositiveornegative.Let'ssayyouwantaHelperthatcolorsanynegative
valuesred.Here'sthecodeforsuchaHelper.
Handlebars.registerHelper('highlightNegative', function ( value ) {
let result = value
if( -1 === Math.sign(value) ) {
result = '<span style="color:red">' + value + '</span>';
}
return new Handlebars.SafeString(result);
})
ThenameoftheHelperishighlightNegative.TheHelpercheckswhetherthevaluethatitgetspassed
byanexpressionisnegative,usingaJavaScriptfunction:Math.sign()(seethedocumentationofMoz-
illa).Ifitis,itwrapsthevalueinaSpanwithastyleattributethathassomeCSStocolorthevaluered.
Onceyou'veaddedthisHelper,itcanbeusedinanyexpressionthatreturnsanumbervalue.
Example: {{highlightNegativeTotal}}
ThisexpressioncallstheHelperwiththenameofadatafield,Total,thathasanumber.Ifthevalueof
thedatafieldis-15,theoutputofthisexpressionwillbe-15.
Inserting a hyperlink
Let'sassumeyouhavehyperlinksinyourdataaswellastextstouseaslabelforthehyperlinks.For
example:
[
{
"url": "http://www.nu.nl",
"label": "nu.nl"
}
]
ThefollowingcoderegistersaHelpercalledmakeHyperlinkthatwillturnthesevaluesintoahyperlink
inthetext.
Handlebars.registerHelper('makeHyperlink', function () {
let result = `<a href="${this.url}">${this.label}</a>`;
return new Handlebars.SafeString( result );
})
Inthetemplateyouwouldcallthefunctioninanexpression.
Page 338