contains
Returns whether a list or string contains an item or substring
boolean contains( mixed haystack , mixed needle )
Returns whether haystack
contains needle
.
- If
haystack
is a string, returnstrue
ifneedle
is a substring. - If
haystack
is a list, returnstrue
ifneedle
is a member of that list. - If
haystack
is an object, returnstrue
ifneedle
is a value (not a key) of that object. - Otherwise, will return false.
contains
only accepts two arguments. If you wish to use more than two arguments, you can try a more explicit statement like:{if contains(profile.vars, "var1) || contains(profile.vars, "var2") || contains(profile.vars "var3")}
Examples
{contains('foobar','ooba') ? 'yes' : 'no'} = yes {contains('foobar','foobari') ? 'yes' : 'no'} = no {contains(['a','b','c'],'b') ? 'yes' : 'no'} = yes {contains({"foo":"bar","baz":"bap"},"bar") ? 'yes' : 'no'} = yes {contains({"foo":"bar","baz":"bap"},"foo") ? 'yes' : 'no'} = no
Check if a subscriber is a member of a list, using the Profile object:
{contains(profile.lists,"Main")}
Check if a user has opted out of a specific template by referencing the Public Name:
{contains(profile.optout_templates,"Notifications")}
Display One-Click Signup Link if User Is Not on Certain List
Use Case:
You want to determine if each user is on your ‘Weekly Notifications’ Natural List, and if they aren’t, show them a one-click signup link using the sailthru_lists query parameter in order to enable them to add themselves.
You’ll notice that the contains() function allows you to check if an array contains a certain value(s). You can also use a bang (“!”) to check the opposite, that is, if it’s notin the array.
Zephyr: (Based on example {profile.lists}
array value
[ "Daily Newsletter", "Sponsored Content", "Breaking News"]
.)
{if !contains(profile.lists,"Weekly Notifications")} Click <a href="http://example.com?sailthru_lists[Weekly Notifications]=1>here</a> to sign up to our Weekly Notifications list! {/if}
Output: Profile Not on Weekly Notifications:
Click here to sign up to our Weekly Notifications list!
Profile on Weekly Notifications:(Nothing shown)
Please note that the “else” statement is optional.
Explanation: This script uses an “if” statement to loop through the “lists” array on each user’s “profile” object, which are all the Natural Lists the user is currently a member of. Using the contains() function, we’re able to see if a particular Natural List is included in their lists array, i.e. they’re currently subscribed to that list. Using a bang (“!”) before contains(), the inverse is checked, i.e. if the user is not on that list. If they are not, a prompt is shown to encourage sign up. Using the “sailthru_lists” query parameter, the user is signed up with one click.