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

# Prompt templating

> Substitute variables, loop over data, and apply conditionals and filters inside prompt messages using Mustache or Nunjucks syntax.

Templates let you inject variables into prompts at runtime. Braintrust supports [Mustache](https://mustache.github.io/) and [Nunjucks](https://mozilla.github.io/nunjucks/templating.html) templating:

* **Mustache** (default): Simple variable substitution and basic logic.
* **Nunjucks**: Advanced templating with loops, conditionals, and filters.

## Mustache

Mustache is the default templating language. For the complete syntax, see the [Mustache documentation](https://mustache.github.io/mustache.5.html).

### Basic variable substitution

Use `{{variable}}` to insert values:

```
Hello {{name}}! Your account balance is ${{balance}}.
```

### Nested properties

Access nested object properties with dot notation:

```
User: {{user.name}}
Email: {{user.profile.email}}
City: {{user.profile.address.city}}
```

### Sections and iteration

Use sections to iterate over arrays or conditionally show content:

```
{{#items}}
- {{name}}: ${{price}}
{{/items}}

{{#user}}
Welcome back, {{name}}!
{{/user}}
```

### Inverted sections

Use `^` to show content when a value is falsy or empty:

```
{{^items}}
No items found.
{{/items}}
```

### Comments

Use `{{! comment }}` for comments that won't appear in output:

```
{{! This is a comment explaining the template }}
Hello {{name}}!
```

### Preserve special characters

If you want to preserve double curly brackets `{{` and `}}` as plain text when using Mustache, change the delimiter tags:

```
{{=<% %>=}}
Return the number in the following format: {{ number }}

<% input.formula %>
```

### Strict mode

Mustache supports strict mode, which throws an error when required template variables are missing:

<CodeGroup dropdown>
  ```typescript theme={"theme":{"light":"github-light","dark":"github-dark-dimmed"}}
  const result = prompt.build(
    { name: "Alice" },
    {
      strict: true, // Throws if any required variables are missing
    },
  );
  ```

  ```python theme={"theme":{"light":"github-light","dark":"github-dark-dimmed"}}
  result = prompt.build(
      {"name": "Alice"},
      strict=True,  # Throws if any required variables are missing
  )
  ```
</CodeGroup>

## Nunjucks

For more complex templating needs, use Nunjucks, which implements Jinja2 syntax in JavaScript. For the full list of syntax and features, see the [Nunjucks templating documentation](https://mozilla.github.io/nunjucks/templating.html).

### Loops

Process arrays and iterate over data:

```
{% for item in items %}
- {{ item.name }}: {{ item.description }}
{% endfor %}
```

Loop variables provide useful metadata:

```
{% for product in products %}
{{ loop.index }}. {{ product.name }}{% if not loop.last %}, {% endif %}
{% endfor %}
```

Available loop variables are `loop.index` (1-indexed), `loop.index0` (0-indexed), `loop.first`, `loop.last`, and `loop.length`.

### Conditionals

Add logic to your prompts:

```
{% if user.age >= 18 %}
You are eligible to vote.
{% elif user.age >= 16 %}
You can get a driver's license.
{% else %}
You are a minor.
{% endif %}
```

Combine conditionals with loops:

```
{% for product in products %}
  {% if product.inStock %}
Available: {{ product.name }} - ${{ product.price }}
  {% endif %}
{% endfor %}
```

### Filters

Transform data with built-in filters:

```
Hello {{ name | upper }}!
Your email is {{ email | lower }}.
Items: {{ items | join(", ") }}
```

Common filters:

* `upper`, `lower`: Change case.
* `title`, `capitalize`: Capitalize text.
* `join(separator)`: Join array elements.
* `length`: Get array or string length.
* `default(value)`: Provide default value.
* `replace(old, new)`: Replace text.

### String operations

Concatenate strings with `~`:

```
{{ greeting ~ " " ~ name }}!
Full name: {{ firstName ~ " " ~ lastName }}
```

### Nested data access

Access nested properties and array elements:

```
{{ user.profile.address.city }}
{{ items[0].name }}
{{ data.results[2].score }}
```

## Next steps

* [Create prompts](/docs/evaluate/prompts/create) that use these templates.
* [Use prompts in code](/docs/evaluate/prompts/use-in-code) and pass input values for your template variables.
