2022.2

Table Of Contents
results.after(labelStr);
Tip: Thereplace()methodasusedintheaboveexamplereplacesonlythefirstoccurrenceof
thesearchstring.Toreplace every occurrenceofasearchstringinagivenstring,useareg-
ular expression.Inthefollowinglineofcode,theregularexpression/@product@/gmakes
replace()searchforalloccurrencesofthestring@product@inthelabelstring:
label = label.replace(/@product@/g, record.tables.detail[i].fields
['product']);
Inthisexample,@product@isapattern(tobeusedinasearch)andgisamodifier(tofindall
matchesratherthanstoppingafterthefirstmatch).Formoreinformationaboutpossibleregular
expressions,seehttps://www.w3schools.com/js/js_regexp.asp.
Replaceseveralplaceholdersinonescript
Supposethereare20differentplaceholdersinapostcard(fortheaddress,accountandcustomer
details,apromocode,theduedate,discounts,alinktoapersonalizedlandingpageetc.).Typicallythis
wouldrequire20queries.EvenafteroptimizingthesescriptsbyusinganIDasselectorforthose
scripts,therearestill20scripts,20queriestorun.
Iftherewasonlyonequery,onesinglescripttodoallthework,theoutputcouldbegeneratedmuch
faster.Reducingthenumberofscriptsimprovestheperformanceofthetemplate.Howtodothis?
First,wrapthecontentthatcontainsalloftheplaceholdersinone(inline)BoxandgivethatBoxor
SpananID(ontheAttributespane).Next,createascriptthatusesthatIDasselector.Thenreplaceall
placeholdersinthescriptandputthecontentbackinthetemplate.
Thisissimilartoworkingwithsnippets,butinthiscasetheelementisextractedfromtheactualtem-
plate.
Example: Thefollowingscriptreplacesalloftheplaceholdersonapostcard.Ittakesadvantage
oftheJavaScriptreplace()command.AssumingthattheIDoftheblockthatrequiresper-
sonalizationispromoblock,thescripthastohaveitsselectorsetto#promoblock.
var block = results.html();
var data = record.fields;
block = block.replace('@name@',data.first + ' ' + data.last);
block = block.replace('@address@',data.address);
block = block.replace('@zip@',data.zip);
block = block.replace('@city@',data.city);
block = block.replace('@country@',data.country);
block = block.replace('@saldo@',data.saldo);
block = block.replace('@promo@',data.promo);
block = block.replace('@customercode@', data.customercode);
results.html(block);
Page 823