Returns a portion of a list.

 list slice( list input, integer offset [, integer length ] )

Given a list, returns a portion of that list.

  • offsetis the index (starting at 0) of the item to start slicing at.
  • lengthis the number of items to return in the slice.
  • If lengthis not passed, the rest of the list will be returned.


 {slice([10, 20, 30, 40, 50], 1, 2)} ---> [20, 30] 
 {slice([10, 20, 30, 40, 50], 2)}    ---> [30, 40, 50]

This function can also be used in a {foreach} loop in the code of a template or campaign. It will limit the number of stories included from a data feed. See details of coding a template for a data feed here.

{foreach slice(content,0,5) as c}

Display Data Feed Content Links with First Article as ‘Featured’

Use Case: In a single feed, you want the first item to be in its own section as a “featured” article. You can isolate that article using the first() function. You can also populate the remaining stories using the slice() function and foreach loop.


{topItem = first(content)}
Here's today's featured story!<br/>
<a href="{topItem.url}">{topItem.title}</a>

<!--Banner Ad Here-->

Other Stories:<br/>
{foreach slice(content,1,2) as c}
<a href="{c.url}">{c.title}</a><br/>


Here’s today’s featured story!
Stephen King’s New Book is a Real Scream

Other Stories:
Get Your First Look at the New iPhone at the Times Square Apple Store
Alexander Harris elected to Los Angeles House of Representatives

Explanation: This script uses the first() function to isolate the first item from a content feed, turning it into its own unique content object, which we’re calling “topItem”. We then use dot notation to pull in the URL and title of that item. For the remaining content, we use the slice() function to start at the second position of the data feed and loop through the remaining items to display in a template.

Insert Ad Between Content Items

Use Case: Parse through an array, most commonly a data feed, to return a sub-section of the array. For instance, if you have ten items in a feed and would like to feed an ad between the second and third slot, you can use the slice function to do so. Let’s assume you’re using a Sailthru Content Feed, which is called “content.”


{*slice the first 2 articles from the content feed and display title and url*}
{foreach slice(content,0,2) as c}
<a href={c.url}>{c.title}
<!--HTML for Your Ad-->

Set a Limit on the Number of Items Shown from a Certain Category

Use Case:

You want to ensure that users aren’t shown content based on the same “author” too many times. Instead, you want to limit it to two items max.


In the Setup:

{max_per_author = 2}
{display_count = 3}

{* Sorts entire content array in interest order, ensuring that most relevant items will be kept for each user *}
{content = personalize({"algorithm": "interest", "content": content, "size": length(content)})}

{* Filter removes any items in excess of max_per_category *}
{author_counts = {}}
{content = filter_content(content, lambda c: ((contains(keys(author_counts), && author_counts[] >= max_per_author) ? false : ((contains(keys(author_counts), ? (author_counts[] = author_counts[] + 1) : (author_counts[] = 1)) || true)))}

{* Slice desired number of items for display. Content already sorted in interest order. *}
{content = slice(content, 0, display_count)}

In the Code:

Picked for You!
{foreach content as c}
<a href={c.url}>{c.title}</a> by {}!


Picked for You!

Alexander Harris elected to Los Angeles House of Representatives by Willow Rosenberg
Get Your First Look at the New iPhone at the Times Square Apple Store by Willow Rosenberg
Spider-Man: Threat or Menace? by J. Jonah Jameson

Show/Hide Code Explanation

This script starts by setting two local variables: a “max_per_author” variable and a “display_count” variable, which are easily editable on a send-by-send basis. In this instance, the “max_per_author” value is set to 2, and the “display_count” is set to 3. It then uses the personalize() algorithm to find all the content the user is interested in. The length() function takes a count of every piece of content in the feed to tell the personalize() function how many items to personalize. By doing the entire length, the content feed is completely reogranized for each user.

Next, an empty object is created, called “author_counts.” In order to fill the object with data, the filter_content() function filters through the content object, using the keys() function in conjunction with the contains() function to check if any “key” (i.e. an author name) is contained within the author_counts object and if the author_count for that particular key is greater than or equal to the author count. Using the Media feed as an example, it’d check if “Willow Rosenberg” is in the object, and if so, if that value is in there more than twice. If so, the script won’t take any further action, but if not, perform another check: If the author_count object already contains that author, then set the value of that author to add “1” to the current value. For instance, if “Willow Rosenberg” were in there once already, the key/value pairing would be “Willow Rosenberg (key) = 1 (value).” Otherwise, if the author_object did not currently contain an instance of that author, the author name is set as the key, with “1” being set as the value. For instance, “Willow Rosenberg = 1”. Since this script iterates through each piece of content, once it gets to “Willow Rosenberg” again as an author, the key/value pair is updated to “Willow Rosenberg = 2”. If it finds that author a third time, it’s removed from the content object for that user.

The content array is then sliced to pull the top items for each user, using the slice() function. The slice starts at position 0 (i.e. the first item in the array) and ends at the value set in the “display_count” variable (in this instance, 3, meaning that content will be the top three items for each user. Finally, a foreach loops iterates through this content array, displaying the title, URL, and author name for each user’s top three pieces of content.

Display Content Recommended Based on Past Purchases

Use Case: You want to check a data feed for any items a user may have already purchased. If they have, call out those specifically in their own “Because you purchased…” section and find non-purchased items for a “We recommend section.”


{content = personalize({
"algorithm" : "popular",
"size" : 100

{nonPurchased = purchased_items(content,false,90)}

We recommend for you:
{foreach slice(nonPurchased,0,3) as c}


We recommend for you:

Item 1

Item 2

Item 3

Explanation: This script uses the personalize() function to create a content object of 100 items based on what’s currently popular in your Content Library. The script then uses the purchased_items() function to check if the user has purchased any of those items in the past 90 days. If they haven’t, the item is added to a “nonPurchased” content object. Next, a foreach loop iterates through this object, and using the slice() function, displays the top three items.