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

Named Templates and Helpers

Create reusable template snippets with define and include to reduce duplication and improve maintainability.

Named Templates and Helpers

You'll learn how to create reusable template snippets in Helm charts that eliminate duplication and make your templates easier to maintain.

What Are Named Templates?

When writing Helm charts, you often repeat the same template patterns—like generating labels, defining resource names, or constructing common configurations. Named templates (also called partials or sub-templates) let you define these patterns once and reuse them everywhere, just like functions in programming.

Think of named templates as cookie cutters: you design the shape once, then stamp it out wherever you need it instead of hand-carving each cookie individually.

Defining Named Templates

Use the define action to create a named template:

{{- define "mychart.labels" -}}
app: {{ .Chart.Name }}
version: {{ .Chart.Version }}
{{- end -}}

This creates a reusable template called mychart.labels that generates standard labels.

Using Named Templates

Invoke your named template with include:

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

The dot (.) passes the current scope to the template. The nindent function properly indents the output.

Why Named Templates Matter

Without named templates, you'd copy-paste the same label definitions across Deployment, Service, ConfigMap, and other resources. When you need to update labels, you'd change them in multiple places—error-prone and tedious. Named templates centralize this logic: change once, affect everywhere.

Naming Convention

Prefix your templates with the chart name (like mychart.labels) to avoid collisions when charts are used as dependencies.

Key Takeaway: Named templates with define and include act as reusable template functions, reducing duplication and centralizing common configuration patterns for cleaner, more maintainable Helm charts.