split
Description
Given a string, returns a list separated by a delimiter”]
list split( string input , string delimiter )
Returns a list separated by
delimiter
.
Example
{str=split("red,blue,yellow",",")} ---> ["red","blue","yellow"]
Split a List of Values Stored as a String
Use Case: You have a value on a user’s profile, for example authors they’re following, but it’s saved as a string. It needs to be converted to an array to parse and match against a feed, so we need to use “split” for that conversion.
Zephyr:
{authorArray = split(my_authors,",")}
{if length(authorArray) > 0}
You're currently following:
{foreach authorArray as c}
{c}<br/>
{/foreach}
Click <a href="http://example.com/subscriptions">here</a> to update your preferences!
{else}
It doesn't look like you're following anyone yet! Click <a href="http://example.com/subscriptions">here</a> to update start!
{/if}
Output:
You’re currently following:
Jessie Jones
Mike Murdock
Carl Lucas
Click here to update your preferences!
Explanation: The script uses the split() function to create an array from a string value, based on a specific delimiter, in this case, a comma. Using split() converts the “my_authors” custom field so that each item because a single item in an array as opposed to one individual string. Next, using the length() function, the script checks to see if there’s more than one item in this array. If there is, a foreach loop displays each author along with a prompt to update their preferences. Otherwise (i.e. the user is not following anyone), the user is shown a prompt to start following users.