slice

Description

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.

Example

 {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.

Zephyr:

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

<!--Banner Ad Here-->

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

Output:

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.”

Zephyr:
{*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}
<br/>
{/foreach}
<!--HTML for Your Ad-->

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.”

Zephyr:

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

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

We recommend for you:
{foreach slice(nonPurchased,0,3) as c}
{c.title}<br/>
{/foreach}

Output: 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.

Contact us

Top