Using Twig 2 in Email Notifications

Using Twig2 in SSA Email Notifications

Twig 2 is a PHP templating framework that is used to generate and display content in Simply Schedule Appointments’ email notifications.

With Twig, you can customize almost every single part of your email content. To learn more about Twig 2 and all of its functions, please see this link: https://twig.symfony.com/doc/2.x/

Let’s look at some common examples where we can use Twig to modify your email notification text:

Display a custom field in an email

To display a custom field in a notification, you can copy and paste the code below into the message area of the notification.

Replace “customfieldnamehere” with the name of the custom field you wish to use.

{{ attribute( Appointment.customer_information, "customfieldnamehere" ) }}

For example, if you create a custom field called “location”, the following code will display the value that the user entered in the location field when they made their booking:

{{ attribute( Appointment.customer_information, "location" ) }}

Change the appointment date format

If an appointment for May 1, 2020 at 12:00am is booked, by default, the “Appointment Details” section displays the date like this:

Starting at 1 May 2020 12:00 am (timezone here)

Here is the relevant code in the notification area:

{{ Appointment.start_date | date("F d, Y g:ia (T)", Appointment.date_timezone) }}

If we dig deeper into this line of code, the expression date(“F d, Y g:ia (T)” in particular is what formats the date. Here is a quick legend of what the letters inside the date expression represent:

F: The full name of the month (ex. January, February, March)
d: Day of the month, two digits, leading zeroes (01 to 31)
Y: Year, in 4 digits
g: 12 hour time format, no leading zeroes
i: minutes (00 to 59)
a: (am or pm)
T: Abbreviated time zone

Here are some alternative letters we can use:

D: the day, in three letters (Mon, Tue, Wed)
G: 24 hour time format, no leading zeroes
j: the day, no leading zeroes (1 – 31)
M: the month, three letters (Jan-Dec)

To change the date format to display as Fri May 1 2020 at 15:00 (timezone), we can use the code below:

{{ Appointment.start_date | date("D M j Y \\a\\t G:ia (T)", Appointment.date_timezone) }}

To add the text “at”, we need to use “\\” in front of each character in order for the text to display correctly.

To view a full list of all the different date variables you can use, please see this link:

https://www.php.net/manual/en/function.date.php

Convert all values to uppercase

If you would like to convert a user’s field so that the result is always uppercase, you can add “|upper” to the end of the variable string. For example:

{{ attribute( Appointment.customer_information, "location" )|upper }}

Will display always display the field “location” in Upper Case, no matter how the user actually types it.

Advanced: Using If Statements

If statements can be used to display certain fields only if certain conditions are met.

For example, you may have an optional custom field on your booking form, and you may want to only display the label for this field and its value if the user fills this field out in the form.

To achieve this, we can use the following code:

{% if attribute(Appointment.customer_information, ‘testfield’ %}
Test Field: {{ attribute(Appointment.customer_information, ‘testfield’ }}
{% endif %}

Some more information about using if statements and boolean operators can be found here: https://twig.symfony.com/doc/2.x/tags/if.html