Common Zephyr Examples



On this page, you'll find basic examples of Zephyr code that you can use or modify within your templates to achieve some of our most common use cases. When you're ready, we also have a larger Zephyr Examples Library containing many more advanced examples. If you're just getting started with Zephyr, be sure to see our Overview which contains links to all introductory information and tutorials.

 

Format Date from a Content Feed

Use Case: You want to pass and display the publish date and time of an article from a Sailthru Content Feed. Since Sailthru automatically adds this parameter as a timestamp to your content, you can format simply with the data() function. Zephyr:
Your top story for the day:<br/>
<a href="{content[0].url}">{content[0].title}</a> published on {date("MMMM dd, yyyy", content[0].date)} at {date("hh:mm:ss aa", content[0].date)}
Output: Your top story for the day: Stephen King’s New Book is a Real Scream! published on March 13, 2017 at 12:42:39 PM Description: This script uses the date() function to format the UNIX timestamp on a piece of content from a Sailthru Content Feed (note that Sailthru automatically adds the “date” parameter and value upon spider). It converts it to a human-readable format in two steps: First to format the month, day, and year; and then to format the hour, minute, second, and produce the AM/PM marker.
 

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.

Sort and Display Content by Date

Use Case: If you have items in a feed and are passing a “date” parameter and wish to sort content from oldest to newest.

Zephyr:

{content = sort(content, 'date')}  {foreach content as c}
<a href={c.url}>{c.title}</a><br/>
{/foreach}

Output: Oldest Item Second Oldest Item Newest Item Using our feed example: From the Fashion Blog: The Most Comfortable Boots You’ll Ever Own Spider-Man: Threat or Menace? Alexander Harris elected to Los Angeles House of Representatives Get Your First Look at the New iPhone at the Times Square Apple Store Stephen King’s New Book is a Real Scream!

Explanation: This script uses the sort() function to rearrange objects by a particular method. In this instance, it’s rearranging content in a data feed by date, printing out the content in order from oldest to newest based on a “date” parameter.


 

Sort and Display Content by Price

Use Case: If you have items in a feed and are passing a “price” parameter and wish to sort content from highest priced to lowest priced. Note the “-” sign is used to reverse the order (i.e. go from highest to lowest as opposed to lowest to highest).
Zephyr:

{content = sort(content, '-price')}
{foreach content as c}
<a href={c.url}>{c.title}</a>
<br/>
{/foreach}

 

Output: Most Expensive Item Second Most Expensive Item Least Expensive Item Using our feed example: Women’s Leather Jacket Men’s Chelsea Boots To Kill a Mockingbird Salem’s Lot Invisible Monsters: A Novel From the Fashion Blog: The Most Comfortable Socks You’ll Ever Own

 

Explanation: This script uses the sort() function to rearrange objects by a particular method. In this instance, it’s rearranging content in a data feed by “reverse” weight (note the “-” before “weight), printing out the content in order from highest priced to lowest priced based on a “price” parameter.


 

Create a Reader-Friendly Price

Use Case: You’re passing a “price” parameter on your comment, which Sailthru requires to be in cents. In order to make it reader-friendly, you need to use “number” to convert it to show dollars and cents.

Zephyr:

Picked for You:
{foreach content as c}
{c.title} for ${number(c.price/100,2)}!<br/>
{/foreach}

Output: Picked for You: To Kill a Mockingbird for $15.00! Salem’s Lot for $9.99! Women’s Leather Jacket for $450.00! Show/hide sample e-commerce feed used for this example

{
"feed": {
    "name": "Ecommerce Feed",
		"url": "http://feed.sailthru.com/ws/feed?id=ecomm"
},
	"content": [
		{
		"title": "To Kill a Mockingbird",
		"description":"The unforgettable novel of a childhood in a sleepy Southern town and the crisis of conscience that rocked it.",
		"date": 1489437759,
		"image": "http://example.com/fiction/tokillamockingbird-full.jpg",
		"weight": 75,
		"tags": [
			"site-store",
			"genre-mystery-thriller",
			"author-harper-lee",
			"classics",
			"fiction",
			"price-11-20"
				],
		"author": "Harper Lee",
		"price": 1500,
		"inventory": 200,
		"vars": {
			"sailthru_genre":"mysteries-and-thrillers",
			"sailthru_category":"books",
			"membership_price":"12.99"
			},
		"url": "http://example.com/fiction/tokillamockingbird/?utm_medium=site",
		"images": {
			"full": {
				"url": "http://example.com/fiction/tokillamockingbird-full.jpg"
					},
			"thumb": {
				"url": "http://example.com/fiction/tokillamockingbird-thumb.jpg"
					}
				}
	},
	{
		"title": "Salem's Lot",
		"description":"Ben Mears has returned to Jerusalem's Lot -- but things are more sinister than he remembers.",
		"date": 1474547794,
		"image": "http://example.com/books/fiction/salemslot-full.jpg",
		"weight": 30,
		"tags": [
			"site-store",
			"genre-horror",
			"author-stephen-king",
			"vampires",
			"fiction",
			"price-5-10"
				],
		"author": "Stephen King",
		"price": 999,
		"inventory": 1567,
		"vars": {
			"sailthru_genre":"horror-and-suspense",
			"sailthru_category":"books",
			"membership_price":"8.15"
			},
		"url": "http://example.com/fiction/salemslot/",
		"images": {
			"full": {
				"url": "http://example.com/books/fiction/salemslot-full.jpg"
					},
			"thumb": {
				"url": "http://example.com/books//fiction/salemslot-thumb.jpg"
					}
				}
	},
	{
		"title": "Women's Leather Jacket",
		"description":"A classic look that never goes out of style with an exposed metal closure that zips at the cuff.",
		"date": 1474547794,
		"image": "http://example.com/fashion/womens/outerwear/leatherjacket-full.jpg",
		"weight": 55,
			"tags": [
			"site-store",
			"womens-clothing",
			"outerwear",
			"material-leather",
			"color-brown",
			"price-201-300"
				],
		"price": 45000,
		"inventory": 1720,
		"vars": {
			"sailthru_color": "brown",
			"sailthru_material":"leather",
			"membership_price":"325.75"
				},
		"url": "http://example.com/fashion/womens/outerwear/leatherjacket/",
		"images": {
			"full": {
				"url": "http://example.com/fashion/womens/outerwear/leatherjacket-full.jpg"
					},
			"thumb": {
				"url": "http://example.com/fashion/womens/outerwear/leatherjacket-thumb.jpg"
					}
				}
	},
	{
		"title": "Invisible Monsters: A Novel",
		"description":"Love, betrayal, petty larceny, and high fashion fuel this deliciously comic novel from the author of <i>Fight Club</i>.",
		"date": 1474547794,
		"image": "http://example.com/books/fiction/invisiblemonsters-full.jpg",
		"weight": 30,
		"tags": [
			"site-store",
			"genre-mystery-thriller",
			"author-chuck-palahniuk",
			"post-modern",
			"fiction",
			"price-5-10"
				],
		"author": "Chuck Palahniuk",
		"price": 899,
		"inventory": 678,
		"vars": {
			"sailthru_genre":"mysteries-and-thrillers",
			"sailthru_category":"books"
			},
		"url": "http://example.com/fiction/invisiblemonsters/",
		"images": {
			"full": {
				"url": "http://example.com/books/fiction/invisiblemonsters-full.jpg"
					},
			"thumb": {
				"url": "http://example.com/books//fiction/invisiblemonsters-thumb.jpg"
					}
				}
	},
	{
		"title": "Men's Chelsea Boots",
		"description": "Look great with jeans or a suit with these slick, black boots.",
		"date": 1474547794,
		"image": "http://example.com/fashion/mens/shoes/chelsea-boots-full.jpg",
		"weight": 40,
			"tags": [
			"site-store",
			"mens-clothing",
			"shoes",
			"material-suede",
			"color-black",
			"price-101-150"
				],
		"price": 14000,
		"inventory": 15,
		"vars": {
			"sailthru_color": "suede",
			"sailthru_material":"leather",
			"membership_price":"115.50"
				},
		"url": "http://example.com/fashion/mens/shoes/chelsea-boots/",
		"images": {
			"full": {
				"url": "http://example.com/fashion/mens/shoes/chelsea-boots-full.jpg"
					},
			"thumb": {
				"url": "http://example.com/fashion/mens/shoes/chelsea-boots-thumb.jpg"
					}
				}
	},
	{
		"title": "From the Fashion Blog: The Most Comfortable Socks You'll Ever Own",
		"description":"They'll literally change your life. Find out where and how to get them!",
		"date": 1489092159,
		"expire_date": 1520628159,
		"image": "http://example.com/fashion/most-comfortable-socks-full.jpg",
		"weight": 25,
		"tags": [
			"site-store",
			"vertical-fashion",
			"type-footwear",
			"author-richard-wilkins",
			"socks",
			"unisex-clothing"
			],
		"author": "Richard Wilkins",
		"vars": {
			"sailthru_category": "fashion",
			"sailthru_storyid": 87422
				},
		"url": "http://example.com/fashion/most-comfortable-socks/",
		"images": {
			"full": {
				"url": "http://example.com/fashion/most-comfortable-socks-full.jpg"
					},
			"thumb": {
				"url": "http://example.com/fashion/most-comfortable-socks-thumb.jpg"
					}
				}
	}
	]
}>

Explanation: This script uses a foreach loop to iterate through each item in a data feed, dividing the value of the “price” parameter by “100” (in Sailthru, “price” must be passed in cents). The script then uses tthe number() function to take this value and format it with comma separators to a certain decimal place, in this case, two decimal places.


 

Show Today’s Date in Reader-Friendly Format

Use Case: Show the current date in an email body/subject line in any desired format. Note that this also uses the time() function.

Zephyr:

Today's date is {date('MMMM dd, yyyy',time("now"))}

Output: Today’s date is August 01, 2016.

Explanation: This script uses the time() function to return the UNIX timestamp for the current date and time, then uses date() to convert that timestamp to the common format of Month Day, Year.


 

Display Name from User Profile or Alternate Noun

Use Case: In an email subject line/body or onsite, greet the user using the first name stored on their user profile. If no first name is stored, use an alternate noun instead. Zephyr:
Hi {first_name ? first_name : 'Valued Reader'}!
Output: Example if first_name exists: “Hi Stefanie!” Example if first_name is empty: “Hi Valued Reader!” Explanation: Uses the ternary operator to determine if first_name exists before displaying it; if it does not exist, uses the alternate variable value specified in quotes.  
   

Insert a Unique Promotion Code

Once you have created your Promotion and uploaded codes, your template can retrieve it using the promotion() function. To include the promotion code Zephyr:
Use code {promotion().code} to redeem your discount.
Output:
Use code UA5-E2C to redeem your discount.