first
Description
Returns the first element or word of a list or string.
mixed first( mixed input [, string prior])
If passed a list, returns the first element of that list. If passed a string, returns the first word in that string (using spaces as the separator). This can be useful for extracting a user’s first name from the
name
property. Optionally, you can pass a second string parameter to return the string value before the first instance of prior
.
Examples
{first([1, 2, 3])} ---> 1
{first('Joe Smith')} ---> Joe
{first('Mrs. Meyer','.')} ---> Mrs
{first('Mrs. Meyer','eyer')} ---> Mrs. M
If the variable name
is set on your subscribers and it includes both their first and last names, you could use this in a template to call just the user’s first name:
Dear {first(name)},
Display User’s First Name from Full-Name Field
Use Case: If you’re collecting a full_name variable on the user and would like a friendlier salutation, first() will return only the first item in the string. For example, if a user’s full_name variable is “John Smith,” the first() function will output “John.”
Zephyr:<b>"</b><span style="font-weight: 400;">Hello, {first(full_name)}!"</span>
Output:
"Hello, John!"
Explanation: This script uses the first() function to check the first element in an array or string, in this instance a “full_name” user variable, to return only the first element, i.e. the user’s first name.
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.