Overview
Using Smarty variables, you are able to dynamically render personalized content in a message. This content is pulled from data objects stored either within Cordial or external sources.
When using Smarty to render message content, you will have access to Cordial specific data objects, values of which can be exposed using variables. Available data objects include:
- Contact - contains data related to a contact.
- Message - contains data related to a message.
- Account - contains data related to the account.
When writing Smarty, the object names are appended with variable keys in order to render the correct value from the data object.
For example, if you created a key called "first" within the contact object; it would be referenced as:
{$contact.first}
{$contact = 'moose'}Then all subsequent variables referencing that object in the system would fail to render in the message.
Reserved and Custom Variables
Variables are related to data values based on their key name within their respective object. Some key names are reserved by the system and some are created by the user.
Examples of reserved variables in Cordial that you can render in a message:
Examples of variables created by the user that you can render in a message:
- Contact Attributes (including lists)
- Event Variables
- Supplement Variables
- External Variables (used for API sending)
Smarty variables that are used in a message will be replaced by the stored data values and shown when a message is rendered either at send time or in message preview.
An example of Smarty syntax for a simple variable replacement:
Smarty in Message | Data In Cordial | Rendered Output |
---|---|---|
Hi {$contact.first}! | Steve | Hi Steve! |
Notice the name of the variable (first) is prepended with the object name (contact).
Variable Scope
Variable scope determines where in the message template passed variables can be accessed. In Smarty, you can use the scope=''
attribute to limit the scope of variables. The principles of variable scope are mainly applicable to application development, but in some cases may be relevant to the use of Smarty variables for message personalization.
We dedicated this section to call out the potential pitfalls of global variables, and to highlight parent
scope as an effective option for the majority of message personalization use cases.
root
, global
, and montyPython
.Aside from user defined variables, those reserved by the system can also have a global scope. Add to that the complexities of advanced Smarty utility use cases, it becomes all too easy to unintentionally override variable names and values. And with such a wide application of variables, the global scope can become polluted, leading to unintentional name collisions that could cause message render issues.
As a rule of thumb, we recommend declaring variables at the parent
scope. Looking at our HTML content include overview and examples, we see that assigning variables found within the include to the parent
scope of the including template, is sufficient for making those variables accessible at the message level.
Comments
0 comments
Please sign in to leave a comment.