Table of contents
Functions, modifiers, and examples
array_diff
Computes the difference of arrays.
Syntax: array_diff($array1, $array2)
Type: Function
-
{$array1 = ['green', 'red', 'blue']}
{$array2 = ['green', 'yellow', 'red']}
{$diff = array_diff($array1, $array2)}
<pre>{json_encode($diff, 128)}</pre> -
{ "2": "blue" }
array_flip
Exchanges all keys with their associated values in an array.
Syntax: array_flip($array)
Type: Function
-
{$fruits = ['d'=>'lemon','a'=>'orange','b'=>'banana','c'=>'apple']}
{$tmp = array_flip($fruits)}
<pre>{json_encode($fruits, 128)}</pre> -
{ "lemon": "d", "orange": "a", "banana": "b", "apple": "c" }
array_intersect
Computes the intersection of arrays.
Syntax: array_intersect($array1, $array2)
Type: Function
-
{$array1 = ["green", "red", "blue"]}
{$array2 = ["green", "yellow", "red"]}
{$intersect = array_intersect($array1, $array2)}
<pre>{json_encode($intersect, 128)}</pre> -
[ "green", "red" ]
array_keys
Return all the keys or a subset of the keys of an array.
Syntax: array_keys($array)
Type: Function
-
{$fruits = ["d"=>"lemon","a"=>"orange","b"=>"banana","c"=>"apple"]}
{$keys = array_keys($fruits)}
<pre>{json_encode($keys, 128)}</pre> -
{ "d", "a", "b", "c" }
array_key_exists
Checks if the given key or index exists in the array.
Syntax: array_key_exists('needle', $array)
Type: Function
-
{$fruits = ["d"="lemon","a"="orange","b"="banana","c"="apple"]} {$check = key_exists('a', $fruits)}
<pre>{$check}</pre> -
true
array_merge
Merge one or more arrays.
Syntax: array_merge(array ...$arrays): array
Type: Function
-
{$fruits = ["d" = "lemon", "a" = "orange", "b" = "banana", "c" = "apple"]} {$veggies = ["e" = "carrot", "a" = "broccoli", "b" = "garlic", "potato"]} {$both = array_merge($fruits, $veggies)} {print_r($both)}
-
Array ( [d] = lemon [a] = broccoli [b] = garlic [c] = apple [e] = carrot [0] = potato )
array_pop
Pop the element off the end of array.
Syntax: array_pop($array)
Type: Function
-
{$colors = ['red','blue','green','purple']}
{$color = array_pop($colors)}
<pre>{json_encode($colors, 128)}</pre>
<pre>{$color}</pre> -
[ "red", "blue", "green" ] purple
array_push
Inserts one or more elements to the end of an array.
Syntax: array_push($array,"value1","value2")
Type: Function
-
{$colors = ['red','blue','green','purple']} {$tmp = array_push($colors,"orange","yellow")} <pre>{json_encode($colors, 128)}</pre>
-
[ "red", "blue", "green", "purple", "orange", "yellow" ]
array_reverse
Return an array with elements in reverse order.
Syntax: array_reverse($array)
Type: Function
-
{$fruits = ["d"=>"lemon","a"=>"orange","b"=>"banana","c"=>"apple"]} {$fruits_reversed = array_reverse($fruits)} {$utils->jsonPrettyPrint($fruits_reversed)}
-
{ "c": "apple", "b": "banana", "a": "orange", "d": "lemon" }
array_search
Searches the array for a given value and returns the first corresponding key if successful.
Syntax: array_search('needle', $array)
Type: Function
-
{$colors = ['red','blue','green','purple']}
{$position = array_search('blue', $colors)}
<pre>{json_encode($colors, 128)}</pre>
<pre>{$position}</pre> -
[ "red", "blue", "green", "purple" ] 1
array_shift
Shift an element off the beginning of array.
Syntax: array_shift($array)
Type: Function
-
{$colors = ['red','blue','green','purple']}
{$color = array_shift($colors)}
<pre>{json_encode($colors, 128)}</pre>
<pre>{$color}</pre> -
[ "blue", "green", "purple" ] red
array_splice
Remove a portion of the array and replace it with something else.
Syntax: array_splice($array, offset)
Type: Function
-
{$colors = ['red','blue','green']}
{$color = array_splice($colors, 2)}
<pre>{json_encode($colors, 128)}</pre>
<pre>{$color}</pre> -
[ "blue", "green" ] red
array_slice
Returns selected parts of an array.
Syntax: array_slice($array,start,length,preserve)
Type: Function
-
{$colors = ['red','blue','green','purple']}
{$working_colors = array_slice($colors,1,2)}
<pre>{json_encode($working_colors, 128)}</pre> -
[ "blue", "green" ]
array_unshift
Prepend one or more elements to the beginning of an array.
Syntax: array_unshift($array
Type: Function
-
{$colors = ['red','blue','green','purple']}
{array_unshift($colors, "yellow")}
<pre>{json_encode($colors, 128)}</pre> -
[ "yellow", "red", "blue", "green", "purple" ]
array_unique
Removes duplicate values from an array.
Syntax: array_unique($array)
Type: Function
-
{$colors = ["d"=>"green","a"=>"green","b"=>"yellow","c"=>"blue"]}
{$tmp = array_unique($colors)}
<pre>{json_encode($colors, 128)}</pre> -
{ "d": "green", "b": "yellow", "c": "blue" }
array_values
Return all the values of an array.
Syntax: array_values($array)
Type: Function
-
{$fruits = ["d"=>"lemon","a"=>"orange","b"=>"banana","c"=>"apple"]}
{$fruits = array_values($fruits)}
<pre>{json_encode($fruits, 128)}</pre> -
{ "lemon", "orange", "banana", "apple" }
asort
Sort an array and maintain index association.
Syntax: asort($array)
Type: Function
-
{$colors = ['red','blue','green','purple']}
{$tmp = asort($colors)}
{$colors = array_values($colors)}
<pre>{json_encode($colors, 128)}</pre> -
{ "blue", "green", "purple", "red" }
arsort
Sort an array in reverse order and maintain index association.
Syntax: arsort($array)
Type: Function
-
{$colors = ['red','blue','green','purple']}
{$tmp = arsort($colors)}
{$colors = array_values($colors)}
<pre>{json_encode($colors, 128)}</pre> -
{ "red", "purple", "green", "blue" }
base64_decode
Decodes data encoded with MIME base64.
Syntax: base64_decode(string $string, bool $strict = false): string|false
Type: Function
-
{base64_decode("TG9yZW0gaXBzdW0gZG9sb3Igc2l0IGFtZXQ=")}
Lorem ipsum dolor sit amet
base64_encode
Encodes data with MIME base64.
Type: Modifier
-
{"foo"|base64_encode}
-
Zm9v
count
Counts the number of items in an array.
Syntax: count($variable)
Type: Function or modifier
-
{$colors = ['red','blue','green','purple']}
Function Syntax:<br>
The colors array contains {count($colors)} items.<br><br>
Modifier Syntax:<br>
The colors array contains {$colors|count} items. -
Function Syntax:
The colors array contains 4 items.
Modifier Syntax:
The colors array contains 4 items.
count_characters
Counts the number of characters in a variable or text string. Use boolean parameter "true" to include whitespaces into the total count.
Syntax: {$variableTitle|count_characters:true}
Type: Modifier
-
{$message.subject='Welcome! Here is Your Special Offer!'} {$message.subject|count_characters}<br>
{$message.subject|count_characters:true} -
31
36
current
Return the current element in an array.
Syntax: current($array)
Type: Function
-
{$colors = ['red','blue','green','purple']}
{$working = current($colors)}
<pre>{json_encode($colors, 128)}</pre>
<pre>{$working}</pre> -
[ "red", "blue", "green", "purple" ] red
date_format
Converts the date to a specified format: http://php.net/manual/en/function.date.php
Type: Modifier
-
{$smarty.now|date_format}<br>
{$smarty.now|date_format:'%D'}<br>
{$smarty.now|date_format:'%A, %B %e, %Y'}<br>
{$smarty.now|date_format:'%Y-%m-%dT%H:%M:%S'}<br>
{$smarty.now|date_format:'Y-m-d\TH:i:sO'} -
Oct 9, 2017
10/09/17
Monday, October 9, 2017
2017-10-09T18:55:57
2017-10-09T18:55:57+0000
default
When smarty variable equals false (e.g. is an empty string) or has no value (i.e. null), allows you the opportunity to show default text.
Type: Modifier
-
{$coupon.code|default:'welcome10'}<br>
{$coupon.code='abc'}<br>
{$coupon.code|default:'welcome10'}<br> -
welcome10
abc
emailObfuscate
Used to obfuscate an email address.
Type: Modifier
-
{$contact.channels.email.address|emailObfuscate}
-
j***n@g***m
empty
Checks to see if a value is empty.
Syntax: empty($variable)
Type: Function
-
{$foo = 0}
{if empty($foo)}
Foo is empty
{else}
Value of foo is: {$foo}
{/if} -
Foo is empty
end
Return the end element in an array.
Syntax: end($array)
Type: Function
-
{$colors = ['red','blue','green','purple']} {$working = end($colors)} <pre>{json_encode($colors, 128)}</pre> <pre>{$working}</pre>
-
[ "red", "blue", "green", "purple" ] purple
escape
Encodes or escapes a string or variable. Possible values are html, htmlall,url, urlpathinfo, quotes, hex, hexentity, javascript, mail. Default is html.
Type: Modifier
-
{"http://example.com test"|escape:'url'}
-
http%3A%2F%2Fexample.com%20test
explode
Converts a string to an array based on a specified delimiter.
Type: Modifier
-
{$test = ","|explode:"a,b,c"} {$test|print_r}
-
Array
(
[0] => a
[1] => b
[2] => c
)
1
hash
Encode a string with a hashing algorithm.
Type: Modifier
-
{"test"|hash:"md5"}
-
098f6bcd4621d373cade4e832627b4f6
hash_equals
Checks whether two hash strings are equal.
Syntax: hash_equals(string $known_string, string $user_string): bool
Type: Function
-
{hash_equals('abc', 'abc')}
- true
hash_hmac
Encode a string with a hashing algorithm using the HMAC method and a key.
Syntax: hash_hmac(string $algorithm, string $needle, string $key): string
Type: Function
-
{hash_hmac('sha256', "Lorem ipsum dolor sit amet", "key")}
ba541febc97264bc53bdbc8042b4c7cfd6ac2c388f977868ae8ab566ed071d4b
implode
Join array elements with a specified connection string.
Syntax: implode(string $separator, array $array): string
Type: Function
-
{$test = implode(",", [a,b,c])} {$test}
-
a,b,c
in_array
Checks to see if an item exists in an array.
Syntax: in_array($needle, $haystack)
Type: Function
-
{$colors = ['red','blue','green','purple']}
{if in_array('red', $colors)}
Red is in the colors array.
{else}
Red is not in the colors array.
{/if} -
Red is in the colors array.
intval
Get the integer value of a variable. Note, this will not round a number.
Syntax: intval("value")
Type: Function
-
{intval("102.33")}<br>
{intval("102.63")} -
102
102
is_array
Checks to see if an array exists.
Syntax: is_array($variable)
Type: Function
-
{$colors = ['red','blue','green','purple']}
{if is_array($colors)}
Colors is an array.
{else}
Colors is not an array.
{/if} -
Colors is an array.
isset
Checks to see if a value exists.
Syntax: isset($variable)
Type: Function
-
{if isset($contact.first_name)}
First name exists.
{else}
First name does not exist.
{/if} -
"first_name": "Fred"
-
First name exists.
json_decode
Takes a JSON encoded string and converts it into a PHP variable.
Syntax: json_decode($variable)
Type: Function
-
{$enc = json_encode(["First"=>$contact.first_name,"Last"=>$contact.last_name,"Email"=>$contact.channels.email.address])} enc: {$enc} {$dec = json_decode($enc, true)}<br><br> {foreach $dec as $key => $value} {$key}: {$value}<br> {/foreach}
- enc: {"First":"Fred","Last":"Garvin","Email":"fredgarvin@example.com"}
First: Fred
Last: Garvin
Email: fredgarvin@example.com
json_encode
Returns a string containing the JSON representation of the supplied value.
Syntax: json_encode(["key"=>value,"key"=>value])
Type: Function
-
{$enc = json_encode(["First"=>$contact.first_name,"Last"=>$contact.last_name,"Email"=>$contact.channels.email.address])}
enc: {$enc} -
enc: {"First":"Fred","Last":"Garvin","Email":"fredgarvin@example.com"}
jsonPrettyPrint
Prints data from a data collection in JSON format.
Type: Function
-
<H1>Debug contact</H1> {$utils->jsonPrettyPrint($contact)}
-
Debug contact
{ "_id": "58d2fc99ac0c8117814d4e78", "channels": { "email": { "address": "fredgarvin@example.com", "subscribeStatus": "subscribed", "unsubscribedAt": "" } }, "last_name": "Fred", "lastModified": "2017-10-03T00:11:14+0000", "lists": [], "createdAt": "2017-03-22T22:37:13+0000", "lj": [], "first_name": "Garvin", "id": "58d2fc99ac0c8117814d4e78" }
key_exists
Checks if the given key or index exists in the array.
Syntax: key_exists('needle', $array)
Type: Function
-
{$fruits = ["d"=>"lemon","a"=>"orange","b"=>"banana","c"=>"apple"]}
{$check = key_exists('a', $fruits)}
<pre>{$check}</pre> -
true
krsort
Sort an array by key in descending order.
Syntax: krsort(array &$array, int $flags = SORT_REGULAR): true
Type: Function
-
{$colors = [5 => 'red', 3 => 'blue', 0 => 'green', 1 => 'purple']} {$tmp = krsort($colors)} {$colors = array_values($colors)}
<pre>{print_r($colors)}</pre> -
Array
( [0] = red [1] = blue [2] = purple [3] = green )
ksort
Sort an array by key in ascending order.
Syntax: ksort(array &$array, int $flags = SORT_REGULAR): true
Type: Function
-
{$colors = [5 => 'red', 3 => 'blue', 0 => 'green', 1 => 'purple']} {$tmp = ksort($colors)} {$colors = array_values($colors)}
<pre>{print_r($colors)}</pre> -
Array
( [0] = green [1] = purple [2] = blue [3] = red )
md5
Encode a string with the md5 hashing algorithm.
Syntax: md5(string $value): string
Type: Function
-
{md5("Lorem ipsum dolor sit amet")}
fea80f2db003d4ebc4536023814aa885
next
Return the next element in an array.
Syntax: next($array)
Type: Function
-
{$colors = ['red','blue','green','purple']}
{$working = next($colors)}
<pre>{json_encode($colors, 128)}</pre>
<pre>{$working}</pre> -
[ "red", "blue", "green", "purple" ] blue
nl2br
Converts "\n" line breaks to html <br /> tags in the given variable.
Type: Modifier
-
{$articleTitle = "Sun or rain expected\ntoday, dark tonight"}
{nl2br($articleTitle)}
-
Sun or rain expected<br>
today, dark tonight
number_format
Correctly formats numbers over 999 and optionally decimal digits using the rounding half up rule.
Type: Modifier
-
{'10000000'|number_format:0}
-
10,000,000
prev
Return the prev element in an array.
Syntax: prev($array)
Type: Function
-
{$colors = ['red','blue','green','purple']}
{$working = next($colors)}
{$working = prev($colors)}
<pre>{json_encode($colors, 128)}</pre>
<pre>{$working}</pre> -
[ "red", "blue", "green", "purple" ] red
print_r
Prints the contents of a data feed in a message.
Type: Modifier
-
<pre>
{$contact.cart.cartitems|print_r}
</pre> -
Array
(
[0] => Array
(
[productID] => 1324
[sku] => 51515
[category] => Albums
[name] => David Hasselhoff
[qty] => 1
[itemPrice] => 9.99
[amount] => 9.99
[album] => Night Rocker
))
1
rand
A math equation that generates a random number between two specified numbers.
Type: function
-
{math equation='rand(1,1000)'}
-
127
replace
Replaces a character in a string with a specified character.
Type: Modifier
-
{$test = "hi there"}
{$test|replace:' ':'_'} -
hi_there
round
Rounds a number up or down.
Type: Modifier
-
{23.95|round}<br>
{23.23|round} -
24
23
sha1
Encode a string with the sha1 hashing algorithm.
Type: Modifier
-
{"test"|hash:"sha1"}
-
a94a8fe5ccb19ba61c4c0873d391e987982fbbd3
sizeof
Counts the number of items in an array.
Syntax: sizeof($variable)
Type: Function
-
{$colors = ['red','blue','green','purple']}
The colors array contains {sizeof($colors)} items.<br><br> -
The colors array contains 4 items.
stopMessage
Stops a message from sending.
-
{if 'condition'} {stopMessage reason="Reason for stopping the message."} {/if}
-
Reason for stopping the message.
Adding a reason for why a message stopped will help you locate issues and troubleshoot more quickly.
string_format
Formats a string. Use the syntax for sprintf() for the formatting.
Type: Modifier
-
{$number = 23.5787446} {$number}<br> {$number|string_format:'%.2f'}<br> {$number|string_format:'%d'}
-
23.5787446
23.58
23
str_replace
Replaces a character in a string with a specified character.
Type: Function
-
{$test = 'hi there'}
{str_replace(' ', '_', $test)} -
hi_there
strstr
Find the first occurrence of a string.
Syntax: strstr(string $haystack, string $needle, bool $before_needle = false): string|false
Type: Function
-
{strstr('Find the first occurrence of a string', 'first')}
-
first occurrence of a string
strtotime
Converts a date to a Unix timestamp, which is the number of seconds that have passed since 1970.
Type: Modifier
-
{'Oct 3, 2017'|strtotime}
-
1506988800
-
Rendering content based if today's date is between 2 dates.
{$jan01 = '2017-01-01 00:00:00'|strtotime} {$dec31 = '2017-12-31 23:59:59'|strtotime} {if $smarty.now >= $jan01 && $smarty.now <= $dec31} Today is between Jan 01, 2017 and Dec 31, 2017 {else} Today is NOT between Jan 01, 2017 and Dec 31, 2017 {/if}
substr
Returns a part of a string.
Type: Modifier
-
{'Hello world'|substr:0:1}
<br>
{'Hello world'|substr:6:5}
<br>
{'Hello world'|substr:3} -
H
world
lo world
time
Returns the current time in the number of seconds since the Unix Epoch (January 1 1970 00:00:00 GMT).
Type: Modifier
-
{time()}<br>
{time()|date_format} -
1507139097
Oct 4, 2017
trim
Removes whitespace and other predefined characters from both sides of a string.
Type: Modifier
-
{$foo = "Hello World"}
{$foo|trim:"Hed"} -
llo Worl
truncate
This truncates a variable to a character length, the default is 80. As an optional second parameter, you can specify a string of text to display at the end if the variable was truncated. The characters in the string are included with the original truncation length. By default, truncate will attempt to cut off at a word boundary. If you want to cut off at the exact character length, pass the optional third parameter of TRUE.
Type: Modifier
-
{$articleTitle ='Two Sisters Reunite after Eighteen Years at Checkout Counter'}
{$articleTitle|truncate:30}<br>
{$articleTitle|truncate:30:"...":true}<br>
{$articleTitle|truncate:30:'...':true:true}<br> -
Two Sisters Reunite after...
Two Sisters Reunite after E...
Two Sisters R...ckout Counter
urldecode
Decodes url encoding in the given string. Plus symbols ('+') are decoded to a space character.
Type: Function
-
{$lnk = 'http%3A%2F%2Fwww.cordial.com%2Fproduct'} {urldecode($lnk)}
-
http://www.cordial.com/product
urlencode
Encodes a URL string. Spaces are encoded as plus (+) signs.
Type: Function
-
{$lnk = 'http://www.cordial.com/product'} {urlencode($lnk)}
-
http%3A%2F%2Fwww.cordial.com%2Fproduct
Comments
0 comments
Please sign in to leave a comment.