Course contentsShow
← Cloud and Infrastructure
Lesson 1557 of 2,98931. Helm Package ManagerFree lesson

Advanced Templating with Named Templates

Create reusable named templates and partials to reduce duplication and improve chart maintainability.

Advanced Templating with Named Templates

What You'll Learn

You'll learn how to create reusable named templates (also called partials) in Helm charts to eliminate repetitive code and make your charts easier to maintain.

The Problem Named Templates Solve

Imagine you're building multiple Deployments that all need the same set of labels, or several Services that share identical annotations. Without named templates, you'd copy-paste the same YAML blocks everywhere. When requirements change, you'd hunt down every duplicate to update them—error-prone and tedious.

Named templates act like functions in programming: define once, use everywhere.

How Named Templates Work

You define named templates in files under the templates/ directory (commonly _helpers.tpl—the underscore tells Helm not to render it as a manifest). Each template has a name and can accept parameters.

Defining a named template:

{{- define "mychart.labels" -}}
app.kubernetes.io/name: {{ .Chart.Name }}
app.kubernetes.io/version: {{ .Chart.Version }}
{{- end -}}

Using it in your manifests:

metadata:
  labels:
    {{- include "mychart.labels" . | nindent 4 }}

The include function calls your template, and the dot (.) passes the current scope. The nindent function properly indents the output.

When to Use Named Templates

  • Repeated labels or annotations across resources
  • Selector blocks that must match exactly
  • Container spec patterns used in multiple workloads
  • Common environment variables or volume mounts

Think of named templates as your chart's utility library—any pattern you use more than once is a candidate.

Key Takeaway: Named templates turn repetitive template blocks into reusable components, drastically reducing duplication and making chart-wide changes as simple as updating a single definition.