Formatting Your Data

There are many different ways to format your data. In this post, when we talk about formatting, we don’t mean styling (font, color, etc) – we are talking about altering the format of your data. For example, you have created a document that includes numbers, but the data comes through without pretty formatted numbers that include commas to separate the thousands. Or you may want to convert a word to all lowercase. Much of this formatting can be done pretty easily.

To format the merge field data, we're going to use what is called a "modifier".  This is a little piece of code that is added after the merge field name, separated with a pipe "|".

Here is a list of available modifiers:

Name Description Example
abbreviation Convert text to abbreviation (grab first letter of each word)

{$Field|abbreviation:"suffix":"prefix"}
{$FullName|abbreviation}
John Smith = JS

{$FullName|abbreviation:"."}
John Smith = J.S.

add_days Add days to date

{$Date|add_days:10}

age Convert date into age (years)

{$Birthdate|age}

age_to_words Convert date into age in words

{$Birthdate|age_to_words} = 2 years 3 days 4 hours

bitly Convert URL to Bitly link

{$Website|bitly:"username":"password"}

calendar Make calendar from array of dates

{json_encode($dates)|calendar}

$dates = [{"name":"Make Offer", "date":"3/5/2017"}, {"name":"Submit Escrow  Check", "date":"3/21/2017"}, {"name":"Close Deal", "date":"4/15/2017"}]

capitalize Capitalize the first letter of a merge field

{$FirstName|capitalize}

capitalize_sentences Capitalize the first word in each sentence

{$Description|capitalize_sentences}

cat Append text to the end of a merge field

{$Name|cat:" is ready"}

csv_to_array Convert CSV string (or URL) to an array

{$csv = $file_url|csv_to_array}

currency_format Format a currency value

{$TotalAmount|currency_format:"$"} = $1,298.99

International Format:
{$TotalPrice|currency_format:'€':2:',':'.'}

date_format Change the format of a date

{$StartDate|date_format:"m/d/Y"}
{strtotime("+ 5 days", strtotime($_date))|date_format:"m/d/Y"}
For more options, check out www.php.net/date

decimal_to_words Convert a decimal/number to words

{$Ratio|decimal_to_words} = three and 7 tenths

default Set a default value for a merge field

{$FavoriteColor|default:"None"}

expand_url Get real URL (follow redirects) from a shortened URL

{$short_url|expand_url}

get_file Download file from URL

{$text = $url|get_file}

html_safe Convert characters to HTML safe characters

{$word|html_safe}

html2office Convert HTML to Office doc formatting

{$Description|html2office}

html2text Convert HTML to plain text

{$Description|html2text}

implode Combine list of array values into single string with a separator

{implode(", ", $Colors)}
comma separated list of the $Colors

int_number Convert international number to US format (for calculations) 

 {$Total = $Subtotal|int_number + $Tax|int_number}

list List array values in a commas separated list

{$Colors|list:",":"and"} = Red, Green, and Blue

lower Convert all characters to lowercase

{$Name|lower}
WebMerge = webmerge

markdown2html Convert markdown to html

{$Description|markdown2html}

money_to_words Convert price to words

{$Total|money_to_words}
10.50 = ten dollars and 50 cents

$Total|money_to_words:"euro"}
11 = eleven euros

nl2 Convert new-line characters to given string

{$Description|nl2:", "}

nl2br Convert new-line characters to HTML line breaks <br>

{$Description|nl2br}

nl2p Convert new-line characters to HTML paragraph

{$Description|nl2p}

number Convert merge field to a number (for calculations)

{$Total|number}
10,231 = 10231

number_eu_to_us Converts a number from EU format to US

{$eu_number|number_eu_to_us}

number_us_to_eu Converts a number from US format to EU

{$us_number|number_us_to_eu}

number_format Change the format of a number (commas and decimals)

{$TotalPrice|number_format:2}
5234.35 = 5,234.35

International format (comma for decimal place)
{$TotalPrice|number_format:2:',':'.'}

number_to_words Convert number to words

{$Total|number_to_words}

pad Pad merge field with the given string, so it is the given length

{$AccountNumber|pad:12:"0"}
123456 = 00000012345

parse_url Searches string for a URL

{$image|parse_url}

phone_format Format a phone number

{$WorkPhone|phone_format:"(%3) %3-%4"}
123-555-7890 = (123) 555-7890

{$phone|phone_format:"%3.%3.%4"} = 123.456.7890

replace Search and replace a string in a merge field

{$Address|replace:"Street":"St"}
123 S Main Street = 123 S Main St

round Round a number to number of decimal places provided

{$Price|round:2}
{$Price|round}

spacify Insert a space (or string) in between each character in merge field

{$Name|spacifiy}
Thomas = T h o m a s

{$AccountNumber|spacify:"-"}
123456 = 1-2-3-4-5-6

state_abbreviation Convert US State name to abbreviation

{$State|state_abbreviation}

strip Replace all new lines, spaces, and tabs with a single space

{$Description|strip}

strip_tags Remove all HTML tags from merge field

{$Description|strip_tags}
{$Description|strip_tags:"<br>"} (allow <br>)

substr Create a sub-string of the merge field

{$Description|substr:start:length}
{$Description|substr:0:40}

text2html Convert text to HTML

{$Description|text2html}

time_to_words Creates a text representation of the number of seconds

{$time_in_seconds|time_to_words}

translate Translate text into another language

{$Description|translate:"de"}

via Google Translate - Languages

truncate Truncate/shorten the merge field to the given length

{$Description|truncate:140}    (140 characters)

ucfirst Capitalize the first letter in the first word

{$FirstName|ucfirst}

ucwords Capitalize the first letter in each word

{$Name|ucwords}

upper Convert all characters to uppercase

{$Name|upper}
WebMerge = WEBMERGE

xml_safe Encode merge field data for XML (Office) docs

{strftime("%d %B %Y", strtotime($_date))|ucwords|xml_safe}

 

  • International Date/Translation
    • You can print a date like this: {strftime("%d %B %Y", strtotime($_date))|ucwords}
    • More formatting options for strftime: http://php.net/strftime

 

For more information on using variable modifiers, please visit http://www.smarty.net/docs/en/language.modifiers.tpl

Have more questions? Submit a request

123 Comments

  • Avatar
    Jurgen Peters (Handicart)

    Wanna format your date and translate? E.g to Dutch?
    Webmerge supports multiple modifiers, use {$_date|date_format:"d F Y"|translate:"nl"}
    Works great!

  • Avatar
    Markwood Fields

    I am using a platform that has both date fields and date+time fields. It seems the date fields are stored as date+time behind the scenes, and clocked at 0:00 UTC.

    This is creating an issue where WebMerge is adjusting the timezone, outputting the wrong date. So on our side, we'll see "1/02/19", but the merge says "1/01/19". Is there a way for us to format around this?

  • Avatar
    Jeremy Clarke

    Hi Markwood,

    You'll need to tell the system that the date is GMT like this:

    {$DateTimeField|cat:" GMT"|date_format:"d/m/Y"}

    Make sure to set your timezone under your Account settings or Document settings

Please sign in to leave a comment.
Powered by Zendesk