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}

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}

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

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

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

int_number Convert international number to US format (for calculations)

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

 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

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}

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
    • {$_ = 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

112 Comments

  • Avatar
    Jeremy Clarke

    Hi Richard,

    You'll actually want to just set the font size of the merge field in your template :)

  • Avatar
    Richard Lahaie

    Duh, of course! Thanks Jeremy!

  • Avatar
    David Martin

    Hi Jeremy. I created a fillable pdf in Adobe, and formatted the date fields using adobe's text field properties box to be mm/dd/yyyy, then uploaded that to webmerge as my form. The data is pulling from Process Street via Zapier and shows up as yyyy-mm-ddT11:19:00.000Z. I can't find anywhere in either Process Street or Zapier to use the {$StartDate|date_format:"m/d/Y"} code string and get the format to appear as I want it in the merged document. Thanks

  • Avatar
    Jeremy Clarke

    Hi David,

    Unfortunately, the formatting in a fillable PDF doesn't not "stick" when uploading to WebMerge, but you can use the Field Map (https://support.webmerge.me/hc/en-us/articles/206526086-Field-Map) to re-format the date before going into your PDF.

    If your date field is called "ExpirationDate" you'll enter this in the box under ExpirationDate on your Field Map:

    {$ExpirationDate|date_format:"m/d/Y"}

    Edited by Jeremy Clarke
  • Avatar
    Antoine Bocqueho

    Hi Jeremy,

    Those functions can't work with a word doc ?
    thanks

  • Avatar
    Jeremy Clarke

    Hi Antoine,

    Yes, all those functions will work with a Word doc! If you're having specific issues, please create a support ticket and we can help you from there :)

    Thanks!

  • Avatar
    Julian Swales

    I need to output a date in French in a document.. I see above that I have to use the following item somehow:
    {$_ = setlocale(LC_TIME, "fr_FR")}
    I'm just not sure how to insert it and the date format together...
    This is my current variable field in the English document version.
    {strtotime($Certificate_Date)|date_format:"F j, Y"}
    I need to output the same field input in the French document in French.

  • Avatar
    Jeremy Clarke

    Hi Julian,

    To print the date in another language, you have to use strftime() like this:

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

  • Avatar
    Julian Swales

    Hi Jeremy,
    So, I don't understand how to set the setlocale for the document in the example above. I understand the strftime will use the setlocale, but how do I define it for that document?

  • Avatar
    Jeremy Clarke

    Hi Julian,

    Simply put the setlocale code at the top of your document:

    {$_ = setlocale(LC_TIME, "fr_FR")}

  • Avatar
    Kimberley Sare

    Hi Jeremy

    I am looking to truncate text but rather than by the number of characters, I want everything up to a "(" sign. Can you help please?

  • Avatar
    Jeremy Clarke

    Hi Kimberley,

    You will want to do something like this:

    {substr($Description, 0, strpos("(", $Description))}

  • Avatar
    Tori Bramble

    I have a question: I want to create an option that you select or input the number of people (i.e., 1, 2, 3, 4, 5, etc) and then depending on the number, it has you input a name and a date. For example, if I say {$NbrofPeople} and the input number is 4, how do I get it to say "Person 1 Name", "Person 1 DOB", "Person 2 Name", "Person 2 DOB", "Person 3 Name", "Person 3 DOB", "Person 4 Name", "Person 4 DOB", Thank you

  • Avatar
    Jeremy Clarke

    Hi Tori,

    You will need to create a merge field in your document for each piece of info like this:

    {if $NbrofPeople >= 1}{$Person1Name} {$Person1DOB}{/if}{if $NbrofPeople >= 2}{$Person2Name} {$Person2DOB}{/if}{if $NbrofPeople >= 3}{$Person3Name} {$Person3DOB}{/if}

  • Avatar
    Jeff Schneider

    Hi Jeremy,

    Is there any way to include total page numbers in the document? Looking to add a sentence like "this document, consisting of (X) pages."

    Thanks!

  • Avatar
    Jeremy Clarke

    Hi Jeff,

    It depends on your template type:

    - Document Builder: You can use {nb} in your header/footer for the total pages

    - Word: You can insert a Word "field" that is the total number of pages anywhere in the doc. The easiest way is to do Insert > Page Number, then right click the number and select Edit Field and change it to NumPages.

  • Avatar
    Forrest Chamberlain

    Hi Jeremy,
    How would I convert a CSV to line breaks in an HTML email template? As expected, < br / > is getting stripped.

    Edited by Forrest Chamberlain
  • Avatar
    Jeremy Clarke

    Hi Forrest,

    Is it just a comma separate list of items like this? Red, Green, Blue

    You can try this: {$FieldName|replace:",":"
    "}

  • Avatar
    Forrest Chamberlain

    Hi Jeremy,
    Just like the < br / > in your example right above (and mine until I edited it and added spaces), the email editor is stripping the < br / > (or moving it out of the {}).

    It might be nice to have an option to turn off the email WYSIWYG (no just toggle). Perhaps this is a solution to my woes anyway.

    Edited by Forrest Chamberlain
  • Avatar
    Jeremy Clarke

    Hi Forrest,

    Try adding that code to the Field Map (https://support.webmerge.me/hc/en-us/articles/206526086-Field-Map) instead: {$FieldName|replace:",":"\n"}

    then you can just print the merge field normally in the email and it will automatically have the HTML break line it it.

  • Avatar
    Forrest Chamberlain

    Jeremy, ah, thanks!

  • Avatar
    Andres Cuestas

    any way to format from zapier.

  • Avatar
    Jeremy Clarke

    Hi Andres,

    You can format the data inside your template or using the Field Map: https://support.webmerge.me/hc/en-us/articles/206526086-Field-Map

  • Avatar
    Danielle Walker

    Does web merge limit the number of decimals you can submit? For example, I submit a number 1.123456789012345 and on my document, it only includes the first 13 decimal places 1.1234567890123.

  • Avatar
    Jeremy Clarke

    Hi Danielle,

    Can you try doing something like this: {$field_name|round:13}

  • Avatar
    Danielle Walker

    Jeremy- Sorry to be more clear I want all 15 decimals. But webmerge is only displaying 13.

  • Avatar
    Jeremy Clarke

    Hi Danielle,

    Sorry, try using a "15" instead like this: {$field_name|round:15}

  • Avatar
    Kimberley Sare

    Hi Jeremy

    Can you help please? I want to set the file name of a document from a series of variables in the document. I have added the following to my document and then used the variable $filename as the file name in the webmerge settings but all that I am getting when I test is the name of the document called "filename", rather than the details.

    {$filename=$_date + $client1informal|abbreviation|upper + $client2informal|abbreviation|upper + $client1surname|truncate:3|upper + $reportname}

  • Avatar
    Jeremy Clarke

    Hi Kimberley,

    Unfortunately, you cannot set the value of a merge field inside a document then use that value outside the document in other settings. You'd have to use the Field Map to set the value.

    But with the filename setting, all you need to do is select <> from the dropdown then enter your merge fields like:

    {$_date} {$client1informal|abbreviation|upper} {$client2informal|abbreviation|upper} {$client1surname|truncate:3|upper} {$reportname}

  • Avatar
    Mike Barrette

    I am using a Word Doc template for an application form. The data is coming in using Formstack with a Zap. I am trying to get the Signature image on the Word Doc (it's coming through as a URL so I need some way to convert it to an image. I know with the Build-Your-Own option I can embed the signature field within image html tags and it display nicely in the PDF output. However in the Word Doc, it displays the HTML tags and full URL. Suggestions?

Please sign in to leave a comment.
Powered by Zendesk