Smarty personalization
In Copernica, personalization is achieved using the scripting language Smarty. This allows you to personalize mailings, web pages, SMS messages, and PDF files based on (sub)profile data. You can recognize Smarty code by its use of curly braces and the dollar sign.
To personalize (sub)profile data, you use the Smarty code profile or subprofile. You combine this code with the name of the field in the relevant database or collection. For example:
- {$profile.Voornaam}
- {$profile.Email}
- {$subprofile.Email}
Smarty is case-sensitive. Personalization only works when the field name you use matches exactly with the field name in the database. For instance, the variable {$profile.firstname} will not yield any result if the field name in the database is 'FirstName'. Instead, you should use {$profile.FirstName}.
When using {$subprofile.FIELDNAME}, personalization variables only work if the subprofile is the recipient. The email will then be sent to an email address within the subprofile. If you want to display data from the subprofile in mailings addressed to the profile itself, you can use the loadsubprofile tag.
Smarty and Security
When using Smarty in your templates, you should always consider data security. This means that every variable must be filtered by escaping it:
Dear {$firstname|escape}
We cannot emphasize enough how important this is. For more information, see our article on Smarty and data security.
Simple Personalization
Suppose your database contains the following fields:
- Title
- Name
The database also includes a profile with the following values:
- Mr.
- Bakker
- frank.bakker@example.com
Using this, you can personalize mailings with the variables {$profile.Title}, {$profile.Name}, and {$profile.Email}.
For example:
Dear {$profile.Title} {$profile.Name},
You are receiving this email because you are registered with the following email address: {$profile.Email}.
Result:
Dear Mr. Bakker,
You are receiving this email because you are registered with the following email address: frank.bakker@example.com.
Advanced Personalization
You can also use Smarty code to display conditional data using if-statements.
With the code below, you can show content only if the field 'FirstName' has the value 'Peter':
{if $profile.FirstName == "Peter"}
Then specify the content to display if this condition is not met:
{else}
Finally, close the condition:
{/if}
Suppose a database contains the fields 'Gender' and 'LastName', but lacks a title field. To use a title, we can determine it based on the gender field:
Dear {if $profile.Gender=="Male"}Mr.{elseif $profile.Gender=="Female"}Ms.{else}Customer{/if},
In this example, we first check if the value of the 'Gender' field is 'Male'. If so, the title 'Dear Mr.' is displayed.
If this is not the case, we check if the value is 'Female'. If it is, the title 'Dear Ms.' is displayed.
If the field contains neither value, the title 'Dear customer' is displayed.
Personalization formatting
Database entries may differ in terms of capitalization. Smarty offers specific functions to handle these differences. Below are some common functions.
lower
This function removes all capital letters. For instance, using the code {$profile.Name|lower} will display the value 'Frank BAKKER' as 'frank bakker'.
ucfirst
This filter changes the first character of a string to a capital letter. If the variable {$profile.Name} contains the value 'frank bakker', using the code {$profile.Name|ucfirst} will display the value as 'Frank bakker'.
You can also combine the above functions. If the variable {$profile.Name} contains the value 'FRANK', you can use the code {$profile.Name|lower|ucfirst} to remove capital letters and capitalize the first character. The value will then be displayed as 'Frank'.
Testing personalization
You can test the display of personalization in Copernica using the 'Preview mode' in your template or document. The preview is based on the default recipient. The default recipient should be in the same database as the recipient to whom you want to send the mailing.