2022.2

Table Of Contents
6. Savethescript.Makesurethatthereisatleastoneelementinthesectionthatmatchesthe
selectorofthescript.
Alternative: compile and call the template
Therender()functionactuallydoestwothings:
1. Compile the Handlebars template into a function
WhenaHandlebarstemplateiscompiled,itisactuallycompiledintoaJavaScriptfunction.
2. Call the function to replace expressions with data
Thesecondstepistocalltheresultingfunction,passinginsomedata.Thefunctionwillreplace
theexpressionswithvaluesfromthedataandwillthenreturnHTML.
InsteadofcallingHandlebars.render()youcouldcallHandlebars.compile()andthencallthe
resultingfunction.
Example: var myFunction = Handlebars.compile( "snippets/policy-
info.hbs", record);
let html = myFunction( record );
results.replaceWith( html )
HandlebarssamplecodefromanonlinesourcewillusethetwoseparatestepssinceHandle-
bars.render()isonlyavailableinOLConnect.
Seealso:"HandlebarsAPI"onpage780.
UsingHandlebarstemplates:examples
Rendering a Handlebars template repeatedly
Thisscriptloopsoveradetailtable,eachtimepassingdifferentdatatothesameHandlebarstemplate.
let policies = record.tables.Policies;
let html = '';
for( var i = 0; i < policies.length; i++) {
{html += Handlebars.render( 'snippets/policy.hbs', policies[i] );
}
results.replaceWith( html );
Selecting a Handlebars template based on a data field
AnapproachthatisalsousedwithHTMLsnippetsistousethevalueofadatafieldinthecurrent
recordtodeterminethenameoftheHandlebarstemplatetouse.
Thisisanexample:
Page 668