Rendering templates
Templates are rendered with payloads triggered by the Event Action Manager. The incoming payload is scoped in a data object, so for an order payload the order data begins at data.order.
Details about the EventActionConfig that triggered the event are available under config.
Using custom queries
In some cases you may need additional data that is not available in the original event payload. For instance when you want to personalize a password reset email or include product images in the order confirmation email.
You can enrich your data with a customQuery. The query is executed against the Propeller GraphQL API (with viewer role on all scopes). The queryVariables are the variables used in the GraphQL query. This field supports Handlebars variables and is the only field that is rendered with only the original event payload. Any other field with Handlebars support is rendered with a payload that also includes the results of the custom query. After rendering, the result of the queryVariables value must be a valid JSON string.
After the customQuery is executed, the results are scoped under custom.
Example
In this example we enrich a password reset email payload to include firstName, middleName, lastName and gender of the contact or customer so we can add a personalized salutation to the email.
The incoming event payload looks like this:
{
"data": {
"passwordReset": {
"email": "jan@acme.nl",
"redirectLink": "https://www.acme.nl/reset-password",
"language": "NL"
}
},
"config": {
"id": "56c30108-17b5-41ce-9841-1e587de30ad2",
"emailTemplateId": "b51aa070-d08c-43da-8fc2-ce13a3cb2d77"
}
}
Create a custom query to fetch the fields from the contact or customer from the GraphQL API based on the email address in the event payload:
query resetPasswordCustom($login: String) {
user(login: $login) {
type: __typename
email
firstName
lastName
middleName
gender
}
}
Then add queryVariables that supply the $login variable from the event payload:
{ "login": "{{data.passwordReset.email}}" }
After the customQuery executes, the complete payload used in all other Handlebars fields looks like this:
{
"data": {
"passwordReset": {
"email": "jan@acme.nl",
"redirectLink": "https://www.acme.nl/reset-password",
"language": "NL"
}
},
"config": {
"id": "56c30108-17b5-41ce-9841-1e587de30ad2",
"emailTemplateId": "b51aa070-d08c-43da-8fc2-ce13a3cb2d77"
},
"custom": {
"user": {
"type": "Contact",
"firstName": "Jan",
"middleName": "de",
"lastName": "Vries",
"gender": "M"
}
}
}
This allows you to add a salutation in your template like this:
<p>Dear {{#if (eq custom.user.gender "M")}}Sir{{else}}Madam{{/if}} {{glue " " custom.user.firstName custom.user.middleName custom.user.lastName}}</p>
Preview and standalone rendering
The API can render and send templates standalone without the event triggering them. This is useful for previewing or rendering and sending a template as a standalone feature.
Since there is no event providing the payload in this case, you need to populate the payload yourself. The custom queries will still execute, so any dynamic variables in the queryVariables still need values that exist.
Three mutations are available:
| Mutation | Description |
|---|---|
templateRenderToHTML | Renders the template content to HTML. Works for both EmailTemplate and DocumentTemplate |
emailTemplateRenderAndSend | Renders the template and sends it as email. Useful for previewing the content in your email client. Uses the email template tos, ccs and bccs fields, so you may want to overwrite those to hardcoded values while testing |
documentTemplateRenderToPDF | Renders the template to PDF and returns it as a Base64File object which includes the fileName, base64 content and contentType (application/pdf) |
The input for these mutations consists of the ID of the template you want to render/send and a TemplateRenderInput which includes the payload as data. The JSON value is also scoped within a data variable, so you will need to create a nested data object in your custom payload. For example, the order payload would be at data.data.order.