Skip to main content
In this lesson, you’ll learn how to use named templates to eliminate repetitive code in your Helm charts. When creating Kubernetes manifests, you might notice that labels or other blocks often repeat across multiple objects. For instance, consider the following YAML snippets for a Service and a Deployment where identical label definitions appear in several sections:
As your codebase expands, duplicating these definitions increases the risk of errors and inconsistencies during updates.
Keep your Helm charts DRY (Don’t Repeat Yourself) by consolidating common code blocks in a helper file.

Moving Common Labels to a Helper File

To address this redundancy, you can transfer the shared lines to a helper file (commonly named _helpers.tpl). The underscore in the filename tells Helm to ignore this file when generating Kubernetes manifests. For example, if you start with a Service template like this:
You can move the repeated labels into a named template using the define directive. In your _helpers.tpl, add:
Next, update your Service manifest to include the named template:
Notice that appending a dot (.) to the template call passes the current context into the helper file. Without it, the helper template wouldn’t have access to critical values like .Release.Name.

Applying Named Templates in Deployment Manifests

The same approach works for Deployment manifests where the labels are used in multiple locations. Initially, you might attempt to reuse the helper template like this:
However, this method may result in unexpected indentation issues. When the helper template is inserted, it must respect the surrounding indentation. Although the first instance might work correctly if the helper template is properly formatted, additional instances may not be aligned as expected.

Using the include Function for Proper Indentation

To resolve the indentation problem, use the include function instead of template. The include function allows the output to be piped to other functions like indent. Ensure your helper template in _helpers.tpl is defined as follows:
Then, update the Deployment manifest to properly indent the inserted template output:
This practice ensures that the output from the named template “labels” remains correctly indented, preserving readability and consistency in your generated manifest.

Final Example

Below is an example of the final output with actual values replacing the template directives:
This approach of moving repetitive code into named templates and using functions like include with proper indentation not only enhances code maintainability but also minimizes the risk of errors and inconsistencies in your Helm charts.
For more details on managing Helm charts and Kubernetes manifests, visit the Helm Documentation and Kubernetes Concepts.

Watch Video