keys

Welcome to our newly overhauled Zephyr pages! See our new Overview, Examples, and Index.
Feedback is welcome at bottom of each page.

Description

Returns list of property names of an object.

list keys( object input )

Returns a list of property names that exist on the input object.

Example

{obj={"a":"Hello","b":"World"}}
{keys(obj)}   ---> ["a","b"]

Show the Temperature from Various Cities from a Data Feed

Use Case: You have a data feed of various areas and current temperature. The location is being stored as a key, and the temperature is being stored as a value. In order to populate it in a template to be reader-friendly, you’ll need to use both keys() and values() to properly display them.

Zephyr:

Feed content:

{"weather": [
{"url": "http://example.com/newyork","vars": {"city": {"New York": "57"}}},
{"url": "http://example.com/losangeles","vars": {"city": {"Los Angeles": "68"}}},
{"url": "http://example.com/london","vars": {"city": {"London": "36"}}}]}

In the Setup:

 

{areas = []}
{degrees = []}
{foreach weather as c}
{areas = areas + keys(c.vars.city)}
{degrees = degrees + values(c.vars.city)}
{/foreach}

 

In the template:

 

{foreach areas as i,k}
The weather in {k} is {degrees[i]} degrees.<br/>
{/foreach}

Output:

The weather in New York, NY is 57 degrees.
The weather in Los Angeles, CA is 68 degrees.
The weather in London, GB is 36 degrees.
The weather in Austin, MA is 76 degrees.

Explanation:

This script is referencing a unique data feed with a content array “weather.” Within this array, there is a subset of content variables called “city”, whose value is the key/value pairing of a city name and the current weather in degrees Fahrenheit — for instance, “New York, NY” (key) and “57” (value). Within the script, two empty arrays are created: an “areas” array and a “degrees” array. Using a foreach loop, the “weather” content array is looped through, and, using the keys() function, the value for “areas” becomes a list of all of the “keys” of the “city” variable. In the example of this feed, the areas array equates to [‘New York’, ‘Los Angeles’,’London’]. Similarly, the same loop also uses the values() function to find each value of the “city” variable. Each value then gets added to the degrees array. In this instance, “degrees” becomes [’57’,’68’,’36’].

Finally, in the template, another loop is done on the “areas” array, finding the index (i) and the key (k). The loop iterates through each item in the “areas” array and outputs the city name. Using the index (i.e., its numerical position in the array), the “degrees” array is referenced to pull in whatever value is in that position. For instance, in the first iteration of the loop, i = 0 and k = New York, which dynamically populates as “The weather in New York is {degrees[0]} degrees.” Since “57” is in the first position of the “degrees” array (i.e. degrees[0]), the final output is “The weather in New York is 57 degrees.” This loop then continues for the rest of the array.

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.

Zephyr:

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), c.author) && author_counts[c.author] >= max_per_author) ? false : ((contains(keys(author_counts), c.author) ? (author_counts[c.author] = author_counts[c.author] + 1) : (author_counts[c.author] = 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 {c.author}!
<br/>
{/foreach}

Output:

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.

Top