> ## Documentation Index
> Fetch the complete documentation index at: https://docs.dify.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# Template

> Transform and format data using Jinja2 templating

The Template node transforms and formats data from multiple sources into structured text using Jinja2 templating. Use it to combine variables, format outputs, and prepare data for downstream nodes or end users.

<Frame caption="Template Node Configuration Interface">
  ![Template Node Configuration Interface](https://assets-docs.dify.ai/2025/04/0838bb5c7e1d1a58ed30fcd9fc48920f.png)
</Frame>

## Jinja2 Templating

Template nodes use Jinja2 templating syntax to create dynamic content that adapts based on workflow data. This provides programming-like capabilities including loops, conditionals, and filters for sophisticated text generation.

### Variable Substitution

Reference workflow variables using double curly braces: `{{ variable_name }}`. You can access nested object properties and array elements using dot notation and bracket syntax.

```jinja theme={null}
{{ user.name }}
{{ items[0].title }}
{{ data.metrics.score }}
```

### Conditional Logic

Show different content based on data values using if-else statements:

```jinja theme={null}
{% if user.subscription == 'premium' %}
Welcome back, Premium Member! You have access to all features.
{% else %}
Consider upgrading to Premium for additional capabilities.
{% endif %}
```

### Loops and Iteration

Process arrays and objects with for loops to generate repetitive content:

```jinja theme={null}
{% for item in search_results %}
### Result {{ loop.index }}
**Score**: {{ item.score | round(2) }}
{{ item.content }}
---
{% endfor %}
```

<Frame caption="Template Processing Knowledge Retrieval Results">
  ![Template Processing Knowledge Retrieval Results](https://assets-docs.dify.ai/2025/04/0ae3f13cf725cb2c52c72cc354e592ee.png)
</Frame>

## Data Formatting

### Filters

Jinja2 filters transform data during template rendering:

```jinja theme={null}
{{ name | upper }}
{{ price | round(2) }}
{{ content | replace('\n', '<br>') }}
{{ timestamp | strftime('%B %d, %Y') }}
{{ score | default('No score available') }}
```

### Error Handling

Handle missing or invalid data gracefully using default values and conditional checks:

```jinja theme={null}
{{ user.email | default('No email provided') }}
{{ metrics.accuracy | round(2) if metrics.accuracy else 'Not calculated' }}
```

## Interactive Forms

Templates can generate interactive HTML forms for structured data collection in chat interfaces:

```html theme={null}
<form data-format="json">
  <label for="username">Username:</label>
  <input type="text" name="username" required />
  
  <label for="priority">Priority:</label>
  <input type="select" name="priority" data-options='["low","medium","high"]'/>
  
  <label for="message">Message:</label>
  <textarea name="message" placeholder="Enter your message"></textarea>
  
  <input type="checkbox" name="urgent" data-tip="Mark as urgent"/>
  <button data-variant="primary">Submit</button>
</form>
```

<Frame caption="Interactive Form Rendered in Chat Interface">
  ![Interactive Form Rendered in Chat Interface](https://assets-docs.dify.ai/2025/04/9d24e9cfa3cdde00e4eee15bd4bbea76.png)
</Frame>

When users submit forms, responses become structured JSON data available for immediate processing in downstream workflow nodes.

## Output Limits

Template output is limited to **80,000 characters** (configurable via `TEMPLATE_TRANSFORM_MAX_LENGTH`). This prevents memory issues and ensures reasonable processing times for large template outputs.
