docs / articles / Helm Template Range Function Explained

Helm Template Range Function Explained

Noah Aug 16, 2026 2026-08-16 04:58:21

The Helm Template's range function is a powerful tool that helps automate and repeat tasks, making it an essential aspect of templating in Kubernetes. This function allows you to process and manipulate data based on ranges of numbers or strings, enabling you to efficiently create multiple resources with minimal code.

Templates — SKS PROPS
Templates — SKS PROPS

In this comprehensive guide, we'll delve into the intricacies of the range function, providing you with a solid understanding of its usage, syntax, and best practices. By the end of this article, you'll be equipped to harness the full potential of this function in your helm templates.

Helmet Template – Large
Helmet Template – Large

Understanding the range Function

The range function in Helm templates iterates over a given list and assigns each element to a placeholder. This placeholder can then be used within the template to dynamically generate content. The function is defined as follows:

an image of a helmet with measurements and instructions for the parts to be used in this project
an image of a helmet with measurements and instructions for the parts to be used in this project

{{ range $index := .Items }}{{ $index }} - {{ . }} {{ end }}

The above example demonstrates a simple usage of the range function. In this case, it iterates over a list of items (".Items") and prints out the index and value of each item.

the free helmet template clip art is available for all ages to print and use on t - shirts
the free helmet template clip art is available for all ages to print and use on t - shirts

Syntax and Placeholders

The syntax of the range function involves three key components:

    range: The keyword that triggers the function. $index: An optional placeholder that captures the current index of the iteration. .Items: The list being iterated over. This could be any valid list or slice in Go.
three views of the front, back and side of a motorcycle helmet on a white background
three views of the front, back and side of a motorcycle helmet on a white background

Using the $index placeholder is optional, but it can be helpful for tracking the current position within the loop.

Proteins

The end keyword is crucial for signaling the end of the range block. Without it, the loop will result in a fatal error.

the heinet base build template is shown in three different sizes, including one for each
the heinet base build template is shown in three different sizes, including one for each

It's essential to include {{ end }} to close the range loop and prevent errors in your templates.

Common Use-Cases for the range Function

Headgear Alignment Chart
Headgear Alignment Chart
Drawing of a knight's helmet
Drawing of a knight's helmet
the helmet and visor are shown in three different positions
the helmet and visor are shown in three different positions
FAST XP SETUP LNR07
FAST XP SETUP LNR07
three different views of a motorcycle helmet
three different views of a motorcycle helmet
the diagram shows how to cut and sew with scissors, which are also used for sewing
the diagram shows how to cut and sew with scissors, which are also used for sewing
an image of a computer screen showing the design process for a helmet and its components
an image of a computer screen showing the design process for a helmet and its components
Halo 5  / Early Spartan helmet interior concept & HUD wire language exploration / Spartan IV / 343 / Halo Franchise, Eric Will
Halo 5 / Early Spartan helmet interior concept & HUD wire language exploration / Spartan IV / 343 / Halo Franchise, Eric Will
haas miami helmet
haas miami helmet

The range function is particularly useful when you need to create multiple instances of a resource with varying parameters. Here are some common use-cases:

Creating Multiple Pods

Suppose you want to create multiple pods based on a list of container images. You can use the range function to iterate over the list and generate the necessary YAML for each pod:

{{ range $index := .PodImages }}

apiVersion: v1

kind: Pod

metadata:

name: "pod-{{ $index }}"

spec:

containers:

- name: "container-{{ $index }}"

image: "{{ .PodImages.$index }}"

{{ end }}

In this example, the range function iterates over the .PodImages list and creates a new pod for each item in the list.

Generating Configuration Files

The range function can also be used to generate configuration files based on a given template. For instance, you might want to create multiple environment configuration files for different environments (dev, staging, production):

{{ range $env := .Environments }}

version: "{{ $env }}"

services:

- name: "my-app"

env: "{{ $env }}"

{{ end }}

In this case, the range function generates a new configuration file for each environment listed in the .Environments list.

Mastering the Helm Template's range function opens up a world of possibilities for automating and simplifying your templating workflow. By understanding its syntax, placeholders, and best practices, you'll be well-equipped to create powerful and efficient templates for your Kubernetes resources.

Embrace the power of automation, and happy templating!