---
title: "Alerting template language | Grafana documentation"
description: "Use Go template language to create your notification and alert rule templates"
---

# Alerting template language

Notification templates and alert rule templates, such as annotations and labels, both use the Go template language, [text/template](https://pkg.go.dev/text/template).

Both types of templates can use the same keywords, functions, and comparison operators of the Go template language, such as `range`, `if`, `and`, `index`, `eq`, and more.

However, it’s important to note that because notifications and alert rules operate in distinct contexts, some additional variables and functions are only available for either notification or alert rule templates. Refer to:

- [Annotation and label template reference](/docs/grafana/latest/alerting/alerting-rules/templates/reference/)
- [Notification template reference](/docs/grafana/latest/alerting/configure-notifications/template-notifications/reference/)

This documentation provides an overview of the functions and operators of the Go template language that are available for both notification and alert rule templates.

## Print

To print the value of something, use `{{` and `}}`. You can print the value of a [variable](#variables), a field of a variable, the result of a function, or the value of dot.

Go ![Copy code to clipboard](/media/images/icons/icon-copy-small-2.svg) Copy

```go
{{ $values }}
{{ $values.A.Value }}
{{ humanize 1000.0 }}
{{ .Alerts }}
```

## Dot

In `text/template`, there is a special cursor called dot, written as `.`. You can think of this cursor as a variable whose value changes depending on where in the template it is used.

At the start of notification templates, dot (`.`) refers to [Notification Data](/docs/grafana/latest/alerting/configure-notifications/template-notifications/reference/#notification-data).

Go ![Copy code to clipboard](/media/images/icons/icon-copy-small-2.svg) Copy

```go
{{ .Alerts }}
```

In annotation and label templates, dot (`.`) is initialized with all alert data. It’s recommended to use the [`$labels` and `$values` variables](/docs/grafana/latest/alerting/alerting-rules/templates/reference/#variables) instead to directly access the alert labels and query values.

> Note
> 
> Dot (`.`) might refer to something else when used in a [range](#range), a [with](#with), or when writing [templates](#templates) used in other templates.

## If

You can use `if` statements in templates. For example, you can print `Variable empty` when a variable is empty:

Go ![Copy code to clipboard](/media/images/icons/icon-copy-small-2.svg) Copy

```go
{{ if $element }}
Element value: {{$element}}
{{ else }}
Element is empty
{{ end }}
```

## With

`with` is similar to `if` statements, but unlike `if`, it updates dot(`.`) to refer to the value of the expression in `with`:

Go ![Copy code to clipboard](/media/images/icons/icon-copy-small-2.svg) Copy

```go
{{ with $array }}
There are {{ len . }} item(s)
{{ else }}
There are no alerts
{{ end }}
```

## Range

`range` iterates over an array or map, and dot (`.`) is set to the current element of the array:

Go ![Copy code to clipboard](/media/images/icons/icon-copy-small-2.svg) Copy

```go
{{ range $array }}
{{ .itemPropertyName }}
{{ end }}
```

Optionally, you can handle empty objects using `else`:

Go ![Copy code to clipboard](/media/images/icons/icon-copy-small-2.svg) Copy

```go
{{ range $array }}
  {{ .itemPropertyName }}
{{ else }}
  Empty array
{{ end }}
```

You can also get the index of each item within a range by defining index and value variables at the start of the range:

Go ![Copy code to clipboard](/media/images/icons/icon-copy-small-2.svg) Copy

```go
{{ $num_items := len $array }}
{{ range $index, $item := $array }}
This is item {{ $index }} out of {{ $num_items }}
{{ end }}
```

Additionally, you can use `{{break}}` to stop the remaining iterations or `{{continue}}` to stop the current iteration and continue with the next one.

## Functions

The global functions available in `text/template` are:

Expand table

| Function   | Description                                                                                                                                                                    |
|------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `and`      | Returns the boolean AND of its arguments by returning the first empty argument or the last argument.                                                                           |
| `call`     | Returns the result of calling the first argument, which must be a function, with the remaining arguments as parameters.                                                        |
| `html`     | Returns the escaped HTML equivalent of the textual representation of its arguments.                                                                                            |
| `index`    | Returns the result of indexing its first argument by the following arguments, e.g., `{{ index $labels "instance" }}` returns the `instance` key in the `$labels` map variable. |
| `slice`    | Returns the result of slicing its first argument by the remaining arguments.                                                                                                   |
| `js`       | Returns the escaped JavaScript equivalent of the textual representation of its arguments.                                                                                      |
| `len`      | Returns the integer length of its argument, e.g., `{{ len $array }}`                                                                                                           |
| `not`      | Returns the boolean negation of its single argument.                                                                                                                           |
| `or`       | Returns the boolean OR of its arguments by returning the first non-empty argument or the last argument.                                                                        |
| `print`    | An alias for fmt.Sprint                                                                                                                                                        |
| `printf`   | An alias for fmt.Sprintf                                                                                                                                                       |
| `println`  | An alias for fmt.Sprintln                                                                                                                                                      |
| `urlquery` | Returns the escaped value of the textual representation of its arguments in a form suitable for embedding in a URL query                                                       |

For more details, refer to the official documentation on [functions in `text/template`](https://pkg.go.dev/text/template#hdr-Functions).

## Comparison operators

Boolean comparison operators are also available in `text/template`:

Expand table

| Function | Description                                  |
|----------|----------------------------------------------|
| `eq`     | Returns the boolean truth of arg1 == arg2    |
| `ne`     | Returns the boolean truth of arg1 != arg2    |
| `lt`     | Returns the boolean truth of arg1 &lt; arg2  |
| `le`     | Returns the boolean truth of arg1 &lt;= arg2 |
| `gt`     | Returns the boolean truth of arg1 &gt; arg2  |
| `ge`     | Returns the boolean truth of arg1 &gt;= arg2 |

## Variables

Variables in `text/template` must be created within the template. For example, you can create a variable with the current value of dot (`.`) and assign a string or another object to the variable like this:

Go ![Copy code to clipboard](/media/images/icons/icon-copy-small-2.svg) Copy

```go
{{ $variable := . }}
{{ $variable := "This is a test" }}
{{ $variable }}
```

This template outputs:

![Copy code to clipboard](/media/images/icons/icon-copy-small-2.svg) Copy

```none
This is a test
```

## Templates

You can create reusable templates that can be executed from other templates or within the same template.

Define templates using `define` and the name of the template in double quotes:

Go ![Copy code to clipboard](/media/images/icons/icon-copy-small-2.svg) Copy

```go
{{ define "print_labels" }}
{{ end }}
```

You should not define templates with the same name as other templates, including default templates such as `__subject`, `__text_values_list`, `__text_alert_list`, `default.title` and `default.message`. Where a template has been created with the same name as a default template, or a template in another notification template, Grafana might use either template. Grafana does not prevent, or show an error message, when there are two or more templates with the same name.

### Execute templates

You can execute defined templates using `template`, the name of the template in double quotes, and the cursor that should be passed to the template:

Go ![Copy code to clipboard](/media/images/icons/icon-copy-small-2.svg) Copy

```go
{{ template "print_labels" . }}
```

Within a template dot refers to the value that is passed to the template.

For example, if a template is passed a list of firing alerts then dot refers to that list of firing alerts:

Go ![Copy code to clipboard](/media/images/icons/icon-copy-small-2.svg) Copy

```go
{{ template "print_alerts" .Alerts }}
```

If the template is passed the sorted labels for an alert then dot refers to the list of sorted labels:

Go ![Copy code to clipboard](/media/images/icons/icon-copy-small-2.svg) Copy

```go
{{ template "print_labels" .SortedLabels }}
```

This is useful when writing reusable templates. For example, to print all alerts you might write the following:

Go ![Copy code to clipboard](/media/images/icons/icon-copy-small-2.svg) Copy

```go
{{ template "print_alerts" .Alerts }}
```

Then to print just the firing alerts you could write this:

Go ![Copy code to clipboard](/media/images/icons/icon-copy-small-2.svg) Copy

```go
{{ template "print_alerts" .Alerts.Firing }}
```

This works because both `.Alerts` and `.Alerts.Firing` is a list of alerts.

Go ![Copy code to clipboard](/media/images/icons/icon-copy-small-2.svg) Copy

```go
{{ define "print_alerts" }}
{{ range . }}
{{ template "print_labels" .SortedLabels }}
{{ end }}
{{ end }}
```

> Note
> 
> You cannot create independent, reusable templates for labels and annotations as you can with notification templates. In alert rule templates, you need to write each template inline within the label or annotation field.

## Comments

You can add comments with `{{/*` and `*/}}`:

Go ![Copy code to clipboard](/media/images/icons/icon-copy-small-2.svg) Copy

```go
{{/* This is a comment */}}
```

To avoid adding line breaks, use:

Go ![Copy code to clipboard](/media/images/icons/icon-copy-small-2.svg) Copy

```go
{{- /* This is a comment with no leading or trailing line breaks */ -}}
```

## Indentation

You can use indentation, both tabs and spaces, and line breaks, to make templates more readable:

Go ![Copy code to clipboard](/media/images/icons/icon-copy-small-2.svg) Copy

```go
{{ range .Alerts }}
  {{ range .Labels.SortedPairs }}
    {{ .Name }} = {{ .Value }}
  {{ end }}
{{ end }}
```

However, indentation in the template is also present in the text.

### Remove spaces and line breaks

In text/template use `{{-` and `-}}` to remove leading and trailing spaces and line breaks.

For example, when using indentation and line breaks to make a template more readable:

Go ![Copy code to clipboard](/media/images/icons/icon-copy-small-2.svg) Copy

```go
{{ range .Alerts }}
  {{ range .Labels.SortedPairs }}
    {{ .Name }} = {{ .Value }}
  {{ end }}
{{ end }}
```

The indentation and line breaks are also present in the text:

![Copy code to clipboard](/media/images/icons/icon-copy-small-2.svg) Copy

```none
    alertname = "Test"

    grafana_folder = "Test alerts"
```

You can remove the indentation and line breaks from the text changing `}}` to `-}}` at the start of each range:

Go ![Copy code to clipboard](/media/images/icons/icon-copy-small-2.svg) Copy

```go
{{ range .Alerts -}}
  {{ range .Labels.SortedPairs -}}
    {{ .Name }} = {{ .Value }}
  {{ end }}
{{ end }}
```

The indentation and line breaks in the template are now absent from the text:

![Copy code to clipboard](/media/images/icons/icon-copy-small-2.svg) Copy

```none
alertname = "Test"
grafana_folder = "Test alerts"
```
