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"} |
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} |
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: |
date_format | Change the format of a date |
{$StartDate|date_format:"m/d/Y"} |
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)} |
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} |
markdown2html | Convert markdown to html |
{$Description|markdown2html} |
money_to_words | Convert price to words |
{$Total|money_to_words} |
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} |
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} International format (comma for decimal place) |
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"} |
parse_url | Searches string for a URL |
{$image|parse_url} |
phone_format | Format a phone number |
{$WorkPhone|phone_format:"(%3) %3-%4"} |
replace | Search and replace a string in a merge field |
{$Address|replace:"Street":"St"} |
round | Round a number to number of decimal places provided |
{$Price|round:2} |
spacify | Insert a space (or string) in between each character in merge field |
{$Name|spacifiy} |
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} |
substr | Create a sub-string of the merge field |
{$Description|substr:start:length} |
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} |
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
Comments
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
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!
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
Yes, you can use the replace function
How we do REPLACE(REPLACE(A, W, X), Y, Z)) using Web Merge?
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!
Is it mean that I can direct use PHP syntax in Word document although I will be generated document from Salesforce?
That's correct - we allow basic PHP functionality (like string functions) inside your templates.
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?
Hi Kasper,
You can use the "phone_format" modifier (even though it isn't a phone number), like this: {$ssn|phone_format:"%6-%4"}
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?
Hi Kasper,
Yes, you can use the "date_format" modifier like this: {$StartDate|date_format:"Y"} and that will only print the year.
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?
Hi Todd,
Like this: {($Variable1*($Variable2/Variable3)-(Variable1*Variable4))|number_format}
Can you show all the options for a phone format modifier?
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.
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.
Thanks Alex!
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 ...?
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.
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 :)
@Jeremy, you were right. Can't believe I missed that! Works now. :-)
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?
Hi Daniel,
You just need to place the £ outside your merge field like this:
£{$TotalAmount|number_format:2}
That should do that trick!
Thank you for this, Jeremy!
What would I use to make time display without the leading zero for 1-9?
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
This is amazing! Never used this feature before. Thanks for explaining this! Webmerge is awesome.
Thanks,
Rama.
Looking for how to change the time format for 12h vs. 24h, which appears to be the default. Any hints?
Hi Cory,
You would want to use something like this: {$start_time|date_format:"H:i"}
More formatting options: http://php.net/manual/en/function.date.php
Please sign in to leave a comment.