Twig template variables: Variable types
Each Twig template variable shows a type of value (for example, a text, a date or a list). The type of value tells you how to use the variable in a template.
Twig code in a template
- Between
{{and}}, enter a variable to show a value in the document (for example,{{ contact.FirstName }}) - Between
{%and%}, enter a Twig tag that controls the document (for example, anifstatement or aforloop)
The name of a variable has two or more parts, with a dot between each part. The first part is the record (for example, contact). The next part is the data of the record (for example, FirstName).
Variable types
| Type of value | How to use the variable | Example |
|---|---|---|
| Text or number | Enter the variable between {{ and }}. | {{ contact.FirstName }} |
| Price or total | Use the moneyformat Twig filter. Some variables show the value in currency format. For these variables, you do not use a filter. | {{ invoice.Total|moneyformat }} |
| Date | Use the dateformat Twig filter. To show the date in a different format, use the date Twig filter. | {{ consult.ConsultDate|dateformat }}
|
| Condition (true or not true) | Put the variable in a Twig if statement. When you use the variable without an if statement, the variable shows 1 or no value. | {% if animal.IsDead %} … {% endif %} |
| Record | Add one more variable after the variable. After the dot, you can use all variables of that type of record. | {{ animal.Breed.Name }} |
| List of records or values | Put the variable in a Twig for loop. To show a list of text values with a comma between each value, use the join Twig filter. | {% for pet in contact.Animals %}{{ pet.Name }}{% endfor %}
|
| Formatted content | Use the raw Twig filter. If you do not use the raw filter, the document shows the HTML tags as text. | {{ contact.PostalAddress.AddressHtml|raw }} |
Find the type of a variable
- "True if" at the start of the description: the variable shows if a condition is true
- "Shows a list": the variable shows a list of records or values
- "Shows the … record", with a tip to add one more variable: the variable shows a record
- A note about the
moneyformat,dateformatorrawTwig filter: the variable shows a price, a date or formatted content
Each page of Twig template variables also has help sections for the types of value in that page (for example, "Variables that check a condition").