Looping Blocks

A Looping Block is a special type of Content Block that allows you to create sophisticated Campaign content that contains complex personalization, calculations, and formatting.

Looping Blocks are typically used in the following scenarios:

  • Building dynamic content consisting of one or more values. For example, you could create an order confirmation email message that's personalized with all of the items that the customer purchased.

  • Building dynamic content containing data gathered from tables other than the Campaign source table, regardless of the relationship between those tables. For example, you could create an order confirmation email message that contains the order number and date (stored on the Order table), the customer's name and address (stored on the Customer table), and product descriptions for each item purchased (stored on the Product table).

  • Transforming or reformatting data. For example, you could reformat a date field in your Campaign content from the way it's stored in the database (2015-15-05 02:31:11) into a more reader-friendly format (May 5, 2015).

  • Performing calculations on data. For example, you could create an email Campaign that references the consumer's Last Purchase Date by identifying all purchase records associated with a recipient, calculating which of those purchases was the last one, and then displaying that date within the email content.

  • Ranking and ordering data. For example, you could populate a Campaign message with a list of recommended products, sorted by cost.

Like Content Blocks, a Looping Block is created independently of a Campaign, and is reusable across Campaigns.

The platform also supports Simple Looping, which allows you define a simple data looping structure within a Content Block. For more details on Simple Looping, please see Content Blocks.

The platform supports two different data sources for building Looping Blocks -- a Cheetah Digital table or the Engagement Data Platform (EDP).

The platform also supports two different scripting languages -- XSLT and Handlebars. Both configurations are described below.

 

Access

The Looping Blocks screen is accessible via the Main menu; select Assets > Blocks > Looping Blocks.

 

Build a Looping Block -- XSLT

This section describes how to build a Looping Block using the XSLT scripting language. This option is available for Looping Blocks built off a Cheetah Digital table, or off the EDP database. 

Data model

Creating an effective Looping Block relies on a strong understanding of the your database tables and how they're joined together. A join refers to the relationship between tables. Joins have a direction:

  • One-to-many: This join describes a table where one record can map to multiple records in a joined table.   

  • Many-to-one: This join describes a table where many records map back to a single record in a joined table.

Looping Blocks allow you to create complex, sophisticated personalization in your message content, drawing values from tables in either the one-to-many direction, or in the many-to-one direction. Looping Blocks offer more functionality than Personalization Fields, which are limited to only accessing fields in joined tables in the many-to-one direction.

Components of a Looping Block

A Looping Block consists of two components: an XML document and an XSL style sheet coded in the XLST language.

Note: The terminology for these various documents and languages can be confusing. Technically, the language is XLST and the code is stored in an XLS document. However, in general usage, the terms XLST and XLS are often used interchangeably.

XML is a mark-up language (conceptually similar to HTML) that's used to describe data -- including its attributes, and its relationship to other data -- in a manner that's readable by both machines and humans. An XML document consists of a tree with one or more nodes. Each node can represent a table or a join, or a property of that table or join.

As a simple example, the following XML document describes a single individual on the Customer table. The XML describes three properties of this individual (his first name, last name, and address), as well as the values for those properties (John," "Smith," and "80 Main Street" respectively). This customer has made three purchases, which are linked via a join to the Order table. The XML describes two properties for each of these purchases (the Order ID and the Order Date) and their values.

Copy
<customer_table cust_id=326>
                 <prop name="name_first" val="John" />
                 <prop name="name_last" val="Smith" />
                 <prop name="address" val="80 Main Street" />
                 <order_to_customer>
                 <prop prop_name="order_id" val="138" />
                 <prop prop_name="order_date" val="8-5-2014" />
                 </ order_to_customer>
                 <order_to_customer>
                 <prop prop_name="order_id" val="965" />
                 <prop prop_name="order_date" val="10-1-2014" />
                 </ order_to_customer>
                 <order_to_customer>
                 <prop prop_name="order_id" val="419" />
                 <prop prop_name="order_date" val="5-5-2015" />
                 </ order_to_customer>
            </ customer_table>

Within Cheetah Digital, the platform creates the XML document for you once you've defined what data you want to use in your message content. You never work directly with the XML document.

XSLT is a style sheet language for XML documents, similar to how a Cascading Style Sheet (CSS) is a style sheet for HTML documents. XSLT is a flexible programming language that can look through the tables of a database, extract the desired values, and output them in a particular format or ranking based on the user’s requirements. In short, XSLT transforms and presents the data that XML has defined.

The XSLT programming language consists of a series of tags called elements. For the purposes of a Looping Block, you'll mostly use two specific elements:

  • for-each:This element will loop through each node of a specified name.

  • value-of: This element will output the selected data in a browser.

Continuing the above example, let's say you want to create Campaign content that's personalized with the Order Dates for each order that the consumer has placed. Your XSLT code would use a for-each element that references the Customer-to-Order table join. The system would loop through the XML document, looking for nodes with this join. When found, the value-of element tells the platform which properties to output. In this case, you want the order_date property.

Copy
<xsl:for-each select="/customer_table/order_to_customer">
                 <xsl:value-of select="Prop[@prop_name = 'order_date']/@val" />
            </xsl:for-each>

Optionally, you could then apply additional XSLT elements that would reformat this date value, or rank the date values from oldest to newest.

Within Cheetah Digital, the platform will build sample XSLT code for you based on the options you select from the user interface. However, a strong knowledge of XSLT is highly recommended in order to make the best use of the Looping Block feature.

 

Build a Looping Block -- Handlebars

This section describes how to build a Looping Block using the Handlebars scripting language. This option is available only for Looping Blocks built off the EDP database. 

When creating a Looping Block, you can include Handlebars expressions, built-in helpers, and a selection of global helpers.

Note: For more information on Handlebars, please see the official Handlebars guide.

Expressions

All standard Handlebars expressions are supported, including:

  • Path expressions: If the selected EDP attribute has JSON in it, a user can traverse the JSON as long as they know the schema. You can use path expressions, using dot-separated paths. For example, if the Person attribute contains the variables First Name and Last Name, a user can reference them as follows:

Copy
{{person.firstname}} {{person.lastname}}
  • HTML-escaping: In Handlebars, the values returned by the {{expression}} are HTML-escaped. For example, "&" will render as "&amp". If you don't want Handlebars to escape a value, use the "triple-stash"-- "{{{".

Built-in Helpers

Looping Blocks support built-in helpers, including the following: 

  • #if: This helper will conditionally render a block. If its argument returns false, undefined, null, "", 0, or [], Handlebars will not render the block. In the below example, when passed a "true" value for "author," the Looping Block will display firstName lastName. If "author" is null, undefined, or false, nothing will display.

Copy
{{#if author}}
                {{firstName}} {{lastName}}
            {{/if}}

Optionally, you can add an else section to determine what to display if the value is false. The below example will display firstName lastName if the value for author is true, or Unknown Author if the value for author is false.

Copy
{{#if author}}
                {{firstName}} {{lastName}}
                {{else}} 
                Unknown Author
            {{/if}}
  • #unless: This helper can be used as the inverse of an #if helper. The below example will display the warning message if the value for author is false.

Copy
{{#unless author}}
                WARNING: This entry does not have an author!
            {{/unless}}
  • #each: This helper will allow you to iterate over an array. Inside the block, you can use this to reference the element being iterated over. In the below example, when passed multiple values for people, the block will display a bulleted list of each value. You can optionally provide an else section which will display only when the list is empty.

Copy
{{#each people}}
                  <li>{{this}}</li>
            {{/each}}
  • #with: This helper allows you to specify the data context of the block inside it, without needing to specify the nested syntax. This helper can also be used with block parameters to define known references in the current block. Consider the following example: 

Copy
{{#with city as | city |}}
                  {{#with city.location as | loc |}}
                    {{city.name}}: {{loc.north}} {{loc.east}}
                  {{/with}}
            {{/with}}

If the above example is passed the following:

Copy
{
                  city: {
                    name: "San Francisco",
                    summary: "San Francisco is the <b>cultural center</b> of <b>Northern California</b>",
                    location: {
                      north: "37.73,",
                      east: -122.44,
                    },
                    population: 883305,
                  },
                }
            

The block will output "San Francisco: 37.73, -122.44."

Global Helpers

Looping Blocks support a wide range of Global Helpers. For more details on the supported Global Helpers and how to use them, please see Global Helpers

Custom Helpers

Looping Blocks support custom Helpers that are C# compatible. After coding the custom Helper, please speak to your Client Services team, who will add the custom Helper to your account. Please provide the Helper code, the desired name for the Helper, and a description. This description will be shown in the Helper section of the Looping Blocks screen, so be sure to include all relevant information. Once your Helper has been loaded to your account, it will be accessible from the Looping Blocks screen, beneath the list of Global Helpers. 

 

 Create a new Looping Block -- XSLT

This topic describes how to create a new Looping Block through the Looping Block screen. In addition, Looping Blocks can be created through the New Item screen (see Create a New Item: Looping Block).  

To create a new Looping Block using the XSLT scripting language:

 Create a new Looping Block -- Handlebars

To create a new Looping Block using the Handlebars scripting language:

 Copy a Looping Block

To copy an existing item to use as the basis for a new item:

 View or edit a Looping Block

To view or edit an existing Looping Block:

 Delete a Looping Block

To delete an item:

 Assign a Looping Block to a campaign

Once you've created your Looping Block, you can use it within one or more Campaigns.

To insert a Looping Block into the Campaign message: