# Functions

> Built-in functions, from range to attrs.

Call a function by name, with its arguments in brackets. Your own functions sit alongside these. See [Functions, filters and tests](https://kilnlang.com/docs/api/extending/).

## `range`

Returns an array of numbers from a start to an end, including both. The optional third argument is the step. A fractional step, such as 0.1, still reaches the end.

page.kiln:

```kiln
{{ range(1, 5)|join(", ") }}
{{ range(0, 10, 5)|join(", ") }}
{{ range(3, 1)|join(", ") }}
{{ range(0, 0.3, 0.1)|join(", ") }}
```

Output:

```html
1, 2, 3, 4, 5
0, 5, 10
3, 2, 1
0, 0.1, 0.2, 0.3
```

For a step of 1, `1..5` is shorter.

## `max` and `min`

Return the largest or smallest argument, the largest or smallest item of one array, or the largest or smallest value of one hash. Null can't be compared, so it is an error.

page.kiln:

```kiln
{{ max(3, 8, 1) }} {{ min([4, 2, 9]) }}
```

Output:

```html
8 2
```

## `cycle`

Returns the item at a position, starting again from the beginning when it runs out. Useful for alternating classes.

page.kiln:

```kiln
{% for item in ["a", "b", "c"] %}
	<li class="{{ cycle(["odd", "even"], loop.index0) }}">{{ item }}</li>
{% endfor %}
```

Output:

```html
<li class="odd">a</li>
<li class="even">b</li>
<li class="odd">c</li>
```

## `date`

Returns a `Date` from a date, an ISO 8601 string or milliseconds. With no argument, it returns the current time. With null, it returns null, so a missing date never becomes today.

page.kiln:

```kiln
{% if date(post.publishedAt) < date("2026-06-01") %}Older post{% endif %}
```

Output:

```html
Older post
```

## `attrs`

Builds HTML attributes from a hash. It handles the fiddly cases for you:

- null and `false` leave the attribute out;
- `true` writes the name on its own;
- arrays are joined with spaces, skipping empty values;
- everything else is escaped.

page.kiln:

```kiln
<a{{ attrs({
	href: post.url,
	class: ["card", featured ? "card--featured"],
	hidden: false,
	"aria-current": current ? "page",
}) }}>{{ post.title }}</a>
```

Output:

```html
<a href="/blog/hello/" class="card card--featured">Hello</a>
```

Each attribute starts with a space, so write `attrs` straight after the tag name.

## `parent` and `block`

`parent()` outputs the layout's version of the current block. `block("name")` outputs a block of the current template. See [extends and block](https://kilnlang.com/docs/tags/extends/#reusing-blocks).

## `dump`

Outputs a readable view of its arguments, for debugging. It only works when the environment has `debug: true`. Otherwise it outputs nothing, so a forgotten `dump` never leaks data.

```kiln
{{ dump(page, route) }}
```
