JS templates

Use templating to support dynamic task and runbook behavior
JS templates allow you to set various task and runbook fields dynamically at runtime. The "JS" stands for "JavaScript" since the template syntax supports executing arbitrary JavaScript code; however, the templates themselves are independent of the JavaScript task type, and detailed knowledge of the JavaScript language isn't required for using them.

What are JS templates?

JS templates in Airplane work a lot like templates in other tools. A common place you might have seen these is in email marketing tools, where you can write something like Hello {{first_name}} {{last_name}}! and the latter part will get filled in with the person's first name and last name when the email is sent.
Airplane's JS templates work the same way. However, in addition to simple string substitution templates like the one above, JS templates in Airplane also allow you to execute JavaScript code.

Anatomy of a JS template

A JS template is composed of expressions, wrapped in {{}}'s, interwoven with raw text:
When a JS template is evaluated, the contents of each expression (including the {{}}'s) are replaced with their result. The example above would produce a string such as:
Copied
1
Added Wilbur Wright to team Rocketship
In other words, the expression results are casted to strings to match the interwoven raw text.
If a template consists of a singular expression, with no other text, then the output of the expression will not be casted to a string. This allows you to generate non-string values, such as integers:

Using JavaScript in JS templates

Within each JS expression, you can access global metadata such as the task or runbook parameters:
handlebars
Copied
1
{{params.email}}
The particular globals available depend on whether the template is being executed for a task or a runbook. See Task globals and Runbook globals for more details in each case.
Most JS expressions tend to be references, like params.email. However, you can use the full power of JavaScript to perform data transformations inline. For example, you can introduce fallback values:
handlebars
Copied
1
Hello, {{block1.output.name ?? "friend"}}!
If block1 did not produce a name, this will default to the string "friend" instead. Therefore the whole expression might say Hello, [your name]! or it might just say Hello, friend!
You can also use JavaScript to transform data, such as splitting a comma-delimited list of IDs into a list of strings:
handlebars
Copied
1
{{ params.ids.split(",").map(s => s.trim()) }}
For example, an input of "1, 3, 6, 10, 15" would be transformed into ['1', '3', '6', '10', '15'].
Another use case is transforming data into markdown. For example, this will convert a list of rows from a DB query into a markdown list that can be rendered in the Slack integration:
handlebars
Copied
1
{{
2
block2.output.user_list.map(function(row) {
3
return "* " + row.name;
4
}).join("\n")
5
}}

How to use JS templates in Airplane

In the UI

JS templates can be used in any input field that has the template icon:
Certain input types, such as dates or integers, have a template toggle which allows you to toggle between the native input (e.g., a datepicker or numeric input) and an input that supports templates. Click the text that says "templating: off" to toggle:

In task definitions

Templates can also be used when setting template-enabled fields in a YAML task config. For example, to set a URL parameter from a task parameter in a REST task definition, you can write something like:
yaml
Copied
1
rest:
2
urlParams:
3
userID: "{{params.user_id}}"