2022.2

Table Of Contents
4. WritealineofcodethatcallsthefunctionHandlebars.render(template, data).
l
Thetemplatecanbe:
a. thenameofaHandlebarstemplate(.hbs)inthetemplate
b. thenameofaHandlebarstemplate(.hbs)ondisk(file:///)
c. thenameofaremoteHandlebarstemplate(.hbs)(http://orhttps://)
d. astringthatcontainsHTMLandHandlebarsexpressions.
Withasnippetondisk,thecompletesyntaxis:file://<host>/<path>.Ifthehost
is"localhost",itcanbeomitted,resultinginfile:///<path>-notethethreeforwardslashes
afterfile:.
Intheremainderofthepathyoucaneitheruseescapedbackwardslashes:
"file:///C:\\Users\\Administrator\\Desktop\\Handlebars_LoadFile.hbs"
orforwardslashes:
"file:///C:/Users/Administrator/Desktop/Handlebars_LoadFile.hbs"
l
Thedatacanbethecurrentrecordorpartofit,oraJavaScriptobject(whichmayinclude
therecordorpartofit-see"HandlebarsAPI"onpage780foranexample).
Ifnodataispassed,thecurrent recordwillbeused.
Example:
var html = Handlebars.render( "snippets/policy-info.hbs", record
);
5. WritealineofcodethataddstherenderedHTMLtothecontentofthesection.
Example: results.replaceWith( html )
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.
Page 776