2019.2

Table Of Contents
Inserting content in the template
To insert the content after the selected element, use results.after(). To replace the element
with the new content, use results.html() or results.replaceWith().
Example: recent posts
The following script loads five posts from Mozilla's blog and inserts their titles as links in a
template. Mozilla's blog is a WordPress website. Since the WordPress REST API uses JSON
as the response format, the loadjson() function has to be used and the received content has to
be wrapped in HTML.
If the script's selector was h1 (a level one heading), the retrieved content would be inserted after
each level one heading.
var postsObj = loadjson('https://blog.mozilla.org/wp-
json/wp/v2/posts?per_page=5');
var html = '';
html = '<ul>';
for (var idx in postsObj) {
html += '<li><a href="' + postsObj[idx].link + '">' + postsObj
[idx].title.rendered + '</a></li>';
}
html += '</ul>';
results.after(html);
See WordPress REST API developer endpoint reference.
Tip
More examples of how to use an API to load external content are given in these How-to's:
l Using the Google Maps API
l Using the OpenWeatherMap API
The script flow: when scripts run
When Connect generates the actual output letters, web pages or emails -, it opens a record
set and merges it with the template. It takes each record, one by one, and runs all scripts for it,
in a specific order, as explained below.
Page 877