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.

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"}

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}

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

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}

html2offoce Convert HTML to Office doc formatting

{$Description|html2office}

html2text Convert HTML to plain text

{$Description|html2text}

lower Convert all characters to lowercase

{$Name|lower}
WebMerge = webmerge

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_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

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

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}

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

 

  • International Date/Translation
    • {$_ = setlocale(LC_TIME, "spanish")}
    • Then 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

27 Comments

  • Avatar
    Einar v/Nøkkelhus AS

    The numbers are formattet wrong. It does not take into account that in Norway we format differently. one million is 1 000 000,- or 1.000.000,- The decimal punktation is a comma , and thousand seperator is SPACE or DOT

  • Avatar
    Jeremy Clarke

    Hi Einar,

    We have added an international number example above that uses a comma for the decimal and a dot for the thousands separator.  You can actually customize all of the punctuation using {$VariableName|number_format:2:'DECIMAL POINT':'THOUSAND SEPARATOR'}

    Thanks!

  • Avatar
    Nur Husnidha Hussain

    For replace function,I just check in smarty it has replace http://www.smarty.net/docs/en/language.modifier.replace.tpl ,so is supported by Webmerge

  • Avatar
    Jeremy Clarke

    Yes, you can use the replace function

  • Avatar
    Nur Husnidha Hussain

    How we do REPLACE(REPLACE(A, W, X), Y, Z)) using Web Merge?

  • Avatar
    Jeremy Clarke

    It will actually be easier to use the php str_replace function like this:

    {str_replace(str_replace(A, W, X), Y, Z))}

    If you have more questions, please create a support ticket and we can help you directly!

  • Avatar
    Nur Husnidha Hussain

    Is it mean that I can direct use PHP syntax in Word document although I will be generated document from Salesforce?

  • Avatar
    Jeremy Clarke

    That's correct - we allow basic PHP functionality (like string functions) inside your templates.

  • Avatar
    Kasper Dam

    I have a danish social security numbers for input.

    By default webmerge makes it look like this: 1,234,561,234
    I need the following format: 123456-1234

    How do i go around that?

  • Avatar
    Jeremy Clarke

    Hi Kasper,

    You can use the "phone_format" modifier (even though it isn't a phone number), like this: {$ssn|phone_format:"%6-%4"}

  • Avatar
    Kasper Dam

    In webmerge there is no field that records the year of creation for the field map.

    I have only full dates.

    Is it possible to use a merge only part of the date, so i only pull the year?

  • Avatar
    Jeremy Clarke

    Hi Kasper,

    Yes, you can use the "date_format" modifier like this: {$StartDate|date_format:"Y"} and that will only print the year.

  • Avatar
    Todd Heine

    How do I format the results of a calculation?

    For example:

    If {$Variable1*($Variable2/Variable3)-(Variable1*Variable4)} results in 1000000, how do I format the result to to look like 1,000,000?

  • Avatar
    Jeremy Clarke

    Hi Todd,

    Like this: {($Variable1*($Variable2/Variable3)-(Variable1*Variable4))|number_format}

  • Avatar
    Albert Trevino

    Can you show all the options for a phone format modifier?

  • Avatar
    Jeremy Clarke

    Hi Albert,

    We've updated the article above to show an example, but here it is:

    {$WorkPhone|phone_format:"(%3) %3-%4"}

    The %[Number] indicates how many digits from the number to show.

  • Avatar
    Alex Sirota

    Turns out you can do a lot more than just format numbers. You can use conditionals to create arrays which can be used in the data routing rules to create batch copies of documents with an incoming stream of data. I'll write something up for you Jeremy if you want and you can post as a tip here.

     

    Edited by Alex Sirota
  • Avatar
    Jeremy Clarke

    Thanks Alex!

  • Avatar
    Matthew Souther

    Date field {$INTAKE_DATE} coming out of Salesforce looks like this:

    2017-07-03 00:00:00

    Attempting to format it like this:

    7/3/17

    Based on the PHP article, it looks like I should do this:

    {$INTAKE_DATE|date_format:"n/j/y"}

    Error upon file upload:

    Error merging document: Syntax Error in template "string:" "{$INTAKE_DATE |date_format:"n/j/y"}" - Unexpected "|"

    It apparently doesn't like the pipe character ...?

  • Avatar
    Max Joy

    My experience is that you have to specify the format using the correct syntax for date, month, and year. Previously, I've had success with {$INTAKE_DATE|date_format:"d/m/Y"} or similar. The PHP thread they've put up designates the numeric representation of the day as a capital 'N' etc.


    They've got a list of variable modifiers at the top of the thread, as well.

    Edited by Max Joy
  • Avatar
    Jeremy Clarke

    Hi Matthew,

    It looks like there's a space after your merge field and before the pipe. Please remove that space and you should be good to go :)

  • Avatar
    Matthew Souther

    @Jeremy, you were right. Can't believe I missed that! Works now. :-)

  • Avatar
    Daniel Norris

    Hi,
    I am using Smarty {$fields} to populate a word presentation. Is it possible to format a field into £ sterling using a modifier? so if someone enters '300' in a freetype box it will appear as £300.00? Or should this be done using Word fieldmerge?

  • Avatar
    Jeremy Clarke

    Hi Daniel,

    You just need to place the £ outside your merge field like this:

    £{$TotalAmount|number_format:2}

    That should do that trick!

  • Avatar
    Alliance Theatre Staff

    Thank you for this, Jeremy!

  • Avatar
    Alliance Theatre Staff

    What would I use to make time display without the leading zero for 1-9?

  • Avatar
    Jeremy Clarke

    You would want to use something like this: {$start_time|date_format:"g:ia"}

    More formatting options: http://php.net/manual/en/function.date.php

Please sign in to leave a comment.
Powered by Zendesk