2. Template Definition
We’ll design the message for the booking creator to include a subject line and a body section.
Creating Subject Template
Let’s create a template for the subject of the message that will be sent to the creator of a booking. This template will dynamically include the booking title, making the subject line personalized and relevant.
Navigate to the Message Templates view in your application. Click the Create button to start creating a new template.
After clicking Create, the template designer will open. Here’s how to configure the template:
-
Name: Enter a human-readable name for the template, such as
Booking Email Subject
. This name helps you easily identify the template in the list. -
Code: Set the template code to
booking-email-subject
. This code will be used later to reference the template programmatically. -
Type: Select Plain text as the template type, since the subject line does not require HTML formatting.
-
Content: Define the content of the template. For the subject line, use the following format:
Booking confirmation for ${booking.title}
Here,
${booking.title}
is an Apache FreeMarker placeholder that will be replaced with the actual title of the booking when the message is generated.

Once the template is configured, save it.
Creating Body Template
Next, we’ll proceed to create the template that will form the body of our message. Click the Create button once more, and then provide the following values for the template’s attributes:
-
Name: Enter a human-readable name for the template, such as
Booking Email Body
. -
Code: Set the template code to
booking-email-body
. This code will be used later to reference the template programmatically. -
Type: Select HTML as the template type. This indicates that the template will contain HTML markup.
-
Content: Define the content of the template. To do this, simply drag and drop the required elements from the palette located on the right side of the designer directly onto the design canvas.
Create the template by arranging elements from the palette to match the following content:
Hello ${booking.creator.firstName}! Thank you for booking your date and time for ${booking.title}. Your time starts at ${booking.startDate?datetime} and ends at ${booking.endDate?datetime} The booking took place ${today?date}. Have a nice day, Developer team. Developer www.jmix.io
This template is formatted using the principles of the Apache FreeMarker Template Engine.
For example, let’s break down the
${booking.startDate?datetime}
expression in Apache FreeMarker Template Engine:-
${…}
: This is the interpolation syntax in FreeMarker. It tells FreeMarker to evaluate the expression inside the curly braces and insert the result into the output. Think of it as a placeholder that will be replaced with a dynamic value. -
booking.startDate
: This is a variable reference. In this case, it’s assumed that you have a variable namedbooking
that is an object (JPA-entity) and that object has a property calledstartDate
. FreeMarker uses dot notation to access properties of objects. -
?datetime
: This is a built-in function in FreeMarker. It’s used to format a value as a date and time. The?
symbol is used to apply a built-in to the value to its left.For more detailed information, please refer to the FreeMarker documentation.
-
Save the template.
Summary
By completing this section, you have learned how to:
-
Navigate the Message templates view.
-
Create new templates using the template editor.
-
Define separate templates for the subject and body.