Template transformation
Definition
Enables flexible data transformation and text processing using Jinja2, a Python template language.
Prerequisites
Jinja
Jinja is a fast, expressive, and extensible Python template engine for flexible data transformation and text processing.
Use cases
The template node uses Jinja2, a powerful Python template language, to perform lightweight and flexible data transformation within a workflow. It is ideal for scenarios such as text processing and JSON transformation. For example, you can format and merge variables from previous steps into a single text output, which helps prepare information from multiple data sources for subsequent steps.
Example 1: Concatenate multiple inputs
In the input variable section of the template transformation node, add three variables: intro, body, and title. Configure each variable to reference a string-type field from the start node. Then, in the Code editor, enter the following template to combine the variables into a single text output: {{ title }}{{ intro }}{{ body }}.
Example 2: Format information into Markdown
{% for item in chunks %}
### Chunk {{ loop.index }}.
### Similarity: {{ item.metadata.score | default('N/A') }}
#### {{ item.title }}
##### Content
{{ item.content | replace('\n', '\n\n') }}
---
{% endfor %}
Example 3: Render an HTML form
<form data-format="json"> // Default to text
<label for="username">Username:</label>
<input type="text" name="username" />
<label for="password">Password:</label>
<input type="password" name="password" />
<label for="content">Content:</label>
<textarea name="content"></textarea>
<label for="date">Date:</label>
<input type="date" name="date" />
<label for="time">Time:</label>
<input type="time" name="time" />
<label for="datetime">Datetime:</label>
<input type="datetime" name="datetime" />
<label for="select">Select:</label>
<input type="select" name="select" data-options='["hello","world"]'/>
<input type="checkbox" name="check" data-tip="By checking this means you agreed"/>
<button data-size="small" data-variant="primary">Login</button>
</form>