How can we help?

Smarty variable modifiers

Overview

Sometimes data is not stored in a format that makes sense to print in your message body. Variable modifiers allow you to reformat the output of the data so it's more friendly to read. There are many modifiers available documented in Smarty's official documentation: http://www.smarty.net/docs/en/language.modifiers.tpl

Due to Cordial's custom implementation of Smarty, not all modifiers listed in the official Smarty docs are supported. If there are specific unsupported modifiers needed per your business needs, please submit a support request.

Let's take a look at a few commonly used examples:

Date format modifiers

For use with date_format variable modifier.

If for some reason your variable passes an invalid date string, the output will default to today's date using the specified date format, or default to %b %e, %Y if no format is specified. Empty strings and null values will not return a default date value.

Smarty

{$contact.mydate|date_format}
{$contact.mydate|date_format:"%D"}
{$contact.mydate|date_format:"%A, %B %e, %Y"}

HTML output

Jan 1, 2014
01/01/14
Monday, January 1, 2014

Convert UNIX timestamp

Some data sources will provide time as a UNIX timestamp. You can use a Smarty date modifier to convert the date to an more presentable output.

For example, if a date variable had the following UNIX timestamp value:

"date":1493164800

You could write the Smarty variable with the added date modifier.

{$date|date_format}

HTML output

April 25, 2017

Available date_format conversion specifiers

Specifier Result
%a Abbreviated weekday name according to the current locale.
%A Full weekday name according to the current locale.
%b Abbreviated month name according to the current locale.
%B Full month name according to the current locale.
%c Preferred date and time representation for the current locale.
%C Century number (the year divided by 100 and truncated to an integer, range 00 to 99).
%d Day of the month as a decimal number (range 01 to 31).
%D Same as %m/%d/%y.
%e Day of the month as a decimal number, a single digit is preceded by a space (range 1 to 31).
%g Week-based year within century [00,99].
%G Week-based year, including the century [0000,9999].
%h Same as %b.
%H Hour as a decimal number using a 24-hour clock (range 00 to 23).
%I Hour as a decimal number using a 12-hour clock (range 01 to 12).
%j Day of the year as a decimal number (range 001 to 366).
%k Hour (24-hour clock) single digits are preceded by a blank (range 0 to 23).
%l Hour as a decimal number using a 12-hour clock, single digits preceded by a space (range 1 to 12).
%m Month as a decimal number (range 01 to 12).
%M Minute as a decimal number.
%n Newline character.
%p Either `am' or `pm' according to the given time value, or the corresponding strings for the current locale.
%r Time in a.m. and p.m. notation.
%R Time in 24 hour notation.
%S Second as a decimal number.
%t Tab character.
%T Current time, equal to %H:%M:%S.
%u Weekday as a decimal number [1,7], with 1 representing Monday.
%U Week number of the current year as a decimal number, starting with the first Sunday as the first day of the first week.
%V The ISO 8601:1988 week number of the current year as a decimal number, range 01 to 53, where week 1 is the first week that has at least 4 days in the current year, and with Monday as the first day of the week.
%w Day of the week as a decimal, Sunday being 0.
%W Week number of the current year as a decimal number, starting with the first Monday as the first day of the first week.
%x Preferred date representation for the current locale without the time.
%X Preferred time representation for the current locale without the date.
%y Year as a decimal number without a century (range 00 to 99).
%Y Year as a decimal number including the century.
%Z Time zone or name or abbreviation.

Capitalize, lower, and upper modifiers

Smarty

{$contact.name}
{$contact.name|capitalize}
{$contact.name|lower}
{$contact.name|upper}

HTML output (where the contact's name value = "Michael smith")

Michael smith
Michael Smith
michael smith
MICHAEL SMITH

For instances where the source data is in all caps and you need to convert to title case (capitalize first letter of each word), you first need to convert to lower case.

Smarty

{$contact.name = $contact.name|lower}
{$contact.name|capitalize}

HTML Output (where the contact's name value = "MICHAEL SMITH")

Michael Smith

Number modifiers

Smarty

{$number}
{$number|string_format:"%.2f"}
{$number|string_format:"%d"}

HTML output (where the number value value = "23.5787446")

23.5787446
23.58
23

Default modifiers

Using the default modifier, you can render a default value if no value exists for the variable.

Smarty

{$contact.first_name|default:'Friend'}

HTML Output (where the contact's first name value = "John")

John

HTML Output (where the contact's first name value is empty)

Friend

 

In the next article, you can learn about Hashing algorithms.

Comments

2 comments

  • Comment author
    James Anderson

    Went to Smarty Modifiers page linked at top and found nl2br modifier. Integrated it into Sculpt Block only to find out it is disabled due to Security Restrictions. It'd be nice if that were listed here along with any other disabled modifiers.

    0
  • Comment author
    Chad Farran
    Thanks for your feedback James! I added a call-out in the article mentioning that due to our custom implementation of Smarty, not all modifiers and functions will be supported.  If you have a specific need for an unsupported modifier, please submit a support ticket and we'll bring it up with our Dev team about adding support.
    0

Please sign in to leave a comment.