Skip to main content

Using templates in a package

The most common way to use templates is from a package script. The script prepares data in global variables, then calls a template function to generate the output.

Setup

  1. Create a template in the Templates tab with a name, render mode, and content (subject, text, and/or HTML).
  2. In the package script, assign data to global variables (starting with $) that the template references.
  3. Call one of the template functions to generate the output.

Some examples of template functions offered by the automator are:

FunctionReturns
createTemplateHTMLMail(name){ subject, body: { html } }
createTemplateTextMail(name){ subject, body: { text } }
createTemplateHTML(name)HTML string
createTemplateText(name)text string

See Template functions for the full reference, including additional functions like bodyFromTemplate and subjectFromTemplate.

Example: Sending an email

This example assumes that your profile has a working Outbound email configuration that has been marked as default.

Suppose you have a template called workflow-with-notes with the following HTML body (using Comment style):

<h2>Workflow <!--#$workflow.id#--> - <!--#$workflow.subject#--></h2>
<!--BEGIN-LIST-ALL#$workflow.notes#-->
<ul>
<!--BEGIN-ITEM-->
<li><!--#ITEM.person.name#-->: <!--#ITEM.text#--></li>
<!--END-ITEM-->
</ul>
<!--END-LIST-->

The script below fetches a workflow from Xurrent, assigns it to a global variable, generates the mail using the workflow-with-notes template and then sends it to a recipient:

const $workflow = fetch('workflows', 1674);
let mailData = createTemplateHTMLMail('workflow-with-notes');
mailData.to = 'recipient@example.org';
sendMail(mailData);

Example: Generating a note

Suppose you have a template called request-summary with the following text body (using Curly style):

Request #{{$request.id}}: {{$request.subject}}
Status: {{$request.status}}
Category: {{$request.category}}

The script below fetches a request, generates text from the template and adds it as a note to the request:

const $request = fetch('requests', source.object_id);
let text = createTemplateText('request-summary');
addNote('requests', $request.id, text);