2018.1

Table Of Contents
Replace several placeholders in one script
Suppose there are 20 different placeholders in a postcard (for the address, account and
customer details, a promo code, the due date, discounts, a link to a personalized landing page
etc.). Typically this would require 20 queries. Even after optimizing these scripts by using an ID
as selector for those scripts, there are still 20 scripts, 20 queries to run.
If there was only one query, one single script to do all the work, the output could be generated
much faster. Reducing the number of scripts improves the performance of the template. How to
do this?
First, wrap the content that contains all of the placeholders in one (inline) Box and give that Box
or Span an ID (on the Attributes pane). Next, create a script that uses that ID as selector. Then
replace all placeholders in the script and put the content back in the template.
This is similar to working with snippets, but in this case the element is extracted from the actual
template.
Example
The following script replaces all of the placeholders on a postcard. It takes advantage of the
JavaScript replace() command. Assuming that the ID of the block that requires personalization
is promoblock, the script has to have its selector set 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);
The first line retrieves the HTML of the promo block and stores it in a variable called block. To
make the code more readible, the fields from the record are stored in a variable named data.
After replacing the placeholders by values, the script replaces the HTML of the promoblock with
the personalized string.
Page 705