length

Description

Returns the length of a list or strings.

Copy
 integer length( mixed input )

When passed a list, returns the number of items in that list. When passed an object, returns the number of keys in the object. When passed a string, returns the number of characters. When passed anything else, converts to a string and returns the number of characters.

Example

 {length('string')} = 6 {length([1,2,3])} = 3

Check the Number of Purchases a User Has Ever Made

Use Case: You need to check the amount of purchases a user has ever made. If it’s more than 10, display a “thank you” message at the top of the template.

Zephyr:

{if length(profile.purchases) > 10}
Thank you for being a loyal customer! Take $15 off your next purchase.
{/if}

Explanation: This scripts uses the length() function to check the number of purchases a user has ever made by referencing the “profile” object, specifically, the “purchases” array, which is an array of all user purchases. It is then wrapped in a conditional “if” statement that says to show a discount offer if the length of purchases is greater than 10, i.e. the user has purchased more than ten times.