# Templating

## Templating Language

We use [Jinja templating](https://jinja.palletsprojects.com/en/stable/) to construct dynamic queries, allowing flexible adjustments tailored to your data model.

Jinja allows:

* **Loops (`for`)** → Automate query generation for multiple signals.
* **Conditions (`if`)** → Add optional calculations dynamically.

For example, for retrieving cursor stats in ADX, we could use the following query:

```
{% for name, series in time_series -%}
{{series}}
| summarize
    min = min(toreal(value)),
    max = max(toreal(value)),
    mean = avg(toreal(value))
{% if extended %},
    median = percentile(toreal(value), 50),
    std = stdev(toreal(value))
{% endif %}
| extend name = '{{ name }}';
```

## Template Constants

Marple allows you to substitute variables in query templates using Jinja syntax.&#x20;

These variables fall into three categories: **constants**, **dataset filters**, and **query-specific variables**.

* **Constants** are predefined values that can be referenced in queries as `{{CONSTANT_NAME}}`. You can define these constants within the configuration.
* **Dataset filters** are determined by the filter signals in your dataset definition. For example, if `session` is part of the filter list, it can be used in queries as `{{dataset.session}}`.
* **Query-specific variables** are defined per query and vary based on its purpose. For instance, a time series query might use `{{timestamp_start}}` and `{{timestamp_stop}}` to define the time range.

This approach ensures dynamic and flexible queries that adapt to different datasets while maintaining clarity and efficiency.

<figure><img src="https://3590686807-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FwEBNWlmdcxXBXd7oyqyR%2Fuploads%2Fj1NK08eFGmIQYbvxbTV9%2FScreenshot%202025-01-30%20at%2009.27.45.png?alt=media&#x26;token=fb644964-6537-4350-9236-6eeb12298ba7" alt=""><figcaption></figcaption></figure>
