bucket_list

Description

Moves elements of a list into separate ‘buckets’ based on criteria.

Copy
 object bucket_list( list source , mixed bucketmethod )

Take a list and turn it into an object. Each field of the resulting object will contain a list of items within that “bucket”. This is especially useful if you have a feed of content and you want to break it up into different sections based on some criterion. If bucketmethodis a string, it treats that as a field name to use as the bucket key. If bucketmethodis a lambda, it evaluates that lambda and uses the result as a bucket key. If the lambda returns null, the item will not be included.

Examples

Supposing a data feed that looks like this:

{"content":[   
   {"url":"http://www.example.com/item-1",
    "section":"sidebar","tags":["furniture","wood"]},
   {"url":"http://www.example.com/item-2",
    "section":"sidebar","tags":["furniture","metal"]},
   {"url":"http://www.example.com/item-3",
    "section":"main","tags":["art","wood"]},
   {"url":"http://www.example.com/item-4",
    "section":"main","tags":["art","metal"]}]
}

Break into buckets based on section:

{sections = bucket_list(content, 'section')} 
{foreach sections.main as content}...item-3 & item-4 appear here{/foreach} 
{foreach sections.sidebar as content}...item-1 & item-2 appear here{/foreach}

Break into buckets based on tag membership:

{sections = bucket_list(content, lambda x: contains(x.tags,'metal') ? 'metal' : 'nonmetal')} 
{foreach sections.metal as content} ... item-2 and item-4 appear here 
{/foreach} 
{foreach sections.nonmetal as content} ... item-1 and item-3 appear here 
{/foreach}

Build Content Arrays Based on Feed Items’ Content Var Values

Use Case: You have a feed of varied content, which you want to split into generalized “news” content and all other content, such as tech, media, etc. in your template. Everything that’s news has a custom content var called “sailthru.category” with the value set to “news.” Everything else has this value set to “specialized”.

Zephyr:

{content = bucket_list(content, lambda c: c.vars.sailthru_category == "news" ? news : specialized)}
News Stories:
{foreach content.news as c}
<a href={c.url}>{c.title}</a>
<br/>
{/foreach}
Specialized Stories:
{foreach content.specialized as c}
<a href={c.url}>{c.title}</a>
<br/>
{/foreach}

Output: News Stories: Alexander Harris elected to Los Angeles House of Representatives Spider-Man: Threat or Menace? Specialized Stories: Stephen King’s New Book is a Real Scream! Get Your First Look at the New iPhone at the Times Square Apple Store From the Fashion Blog: The Most Comfortable Socks You’ll Ever Own

Explanation:

This script uses a lambda, which creates an anonymous function, in conjunction with the bucket_list() function to evaluate custom meta tags on each piece of content from a Sailthru Content Feed. If the value for the sailthru.category content variable equals “news”, the content is placed in a bucket of content called “content.news”. Otherwise, the content by default is bucketed into a content object called “content.specialized”. Each object is then looped through and displayed in the appropriate section.