Create and use templates

Templates in Studio are Jinja2 or TextFSM files that filter and reshape data for use in workflow tasks. You build and test them under Studio > Templates.

When an Apply Template task runs in a workflow, it combines data from a preceding task or job variable with a named template and passes the result to subsequent tasks.

Prerequisites

Template Builder uses Jinja2 or TextFSM syntax. Knowledge of the relevant language is required before proceeding.

Create a template

1

Open the Create dialog

Click Create at the top of the Studio side nav.

2

Select template type

Select Template from the dropdown.

3

Enter template details

Enter a name and group, then select a language from the Type dropdown.

4

Create

Click Create. The template opens in Template Builder.

Create template dialog

To open an existing template, select it from the Templates section of the side nav, or browse via the collection view by clicking Search.

Template Builder UI

Template Builder has three components:

LabelComponentFunction
1Data fieldEnter example data to be parsed by the template.
2Template fieldDefine the template logic.
3Output fieldPreview the result of combining the data and template. Updates in real time.
Template Builder with Data, Template, and Output fields labeled

Define a template

Enter template logic in the Template field. The syntax depends on the language selected during creation.

Enter example data in the Data field. This should match the structure your template will receive at runtime — a JSON object for Jinja2, semi-formatted text for TextFSM.

The Output field shows the combined result in real time. Use this to draft and troubleshoot your template before using it in a workflow.

Jinja2 template with data, template, and output fields

Use a template in a workflow

In this example, a workflow retrieves interface data from a network switch via API. The response contains dozens of entries, but only interfaces with a role of voice are needed. A Jinja2 template filters the response, and the result is passed to a View Data task for manual confirmation.

In this example, the API response is simulated by a New Variable task that creates a deviceResponse job variable:

1{
2 "interfaces": {
3 "ethernet10": { "role": "desktop", "vlan": 1 },
4 "ethernet11": { "role": "desktop", "vlan": 1 },
5 "ethernet15": { "role": "printer", "vlan": 5 },
6 "ethernet22": { "role": "voice", "vlan": 10 },
7 "ethernet25": { "role": "voice", "vlan": 10 }
8 }
9}
1

Add the Apply Template task

Add Apply Template to the workflow and set:

  • text — the deviceResponse job variable
  • templateDocsDemo
2

Add the Query task

Add a Query task and draw a success transition from Apply Template. Set:

  • object — the parsed output variable from Apply Template
  • queryresults
3

Add the View Data task

Add a View Data task and draw a success transition from Query. Set:

  • headerVoice Interfaces
  • messageEnsure that the expected interfaces are designated as 'voice'
  • body — the return_data output variable from Query
  • btn_successAccept, btn_failureReject
4

Complete the workflow

Draw a success transition from View Data to the workflow end.

Workflow using Apply Template, Query, and View Data tasks
View Data modal showing filtered voice interfaces

Apply Templates (plural) works the same way but accepts arrays. The text and templates fields each take an array, and items are paired by index: text[0] with templates[0], text[1] with templates[1], and so on.