Assigning Template Variables
Capturing values to reuse across a block.
Assigning Template Variables is a free Helm Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Helm Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Why Variables Help
When the same value appears many times in a template, capture it once in a variable so you do not repeat the long path everywhere.
The Assignment Syntax
You declare a variable with a dollar name and the colon-equals operator. In Helm, $name holds whatever expression sits on the right.
{{ $name := .Values.appName }}Variables Start With a Dollar
Every template variable is written with a leading dollar sign. That is how Helm tells your variable apart from a field on the dot context.
Reading a Variable Back
Once assigned, drop the variable into your YAML like any value. Helm substitutes the captured result wherever you write $name.
metadata:
name: {{ $name }}Capturing a Built-in Object
Variables shine for long expressions. Save the release name once, then reuse the short variable instead of typing the full path again.
{{ $rel := .Release.Name }}
name: {{ $rel }}-configFirst := Then =
Use colon-equals only to first declare a variable. To change its value later in the same scope, reassign with a plain equals sign.
{{ $count := 1 }}
{{ $count = 3 }}Scope Follows the Block
A variable lives inside the block where you declared it. Define it before an if or range and it stays visible inside that block.
Out of Scope, Out of Reach
Declare a variable inside an if block and it vanishes once the block ends. Outside that scope, Helm cannot see it at all.
Naming Variables Clearly
Pick short, descriptive names like $fullName or $port. A clear variable name makes the template far easier for teammates to read.
Variables vs the Dot
The dot can change as you enter blocks, but a variable keeps its captured value. That stability is exactly why variables are so useful.
Assign From an Expression
The right side of an assignment can be any expression, even a piped one. Helm runs it once and stores the result in your variable.
{{ $port := .Values.port | default 8080 }}Quick Check
You wrote {{ $svc := .Values.service.name }} earlier in the template.
Recap
You can now declare variables with :=, reassign with =, read them back via the dollar sign, and respect block scope. Tidy templates ahead! 🎯
Frequently asked questions
Is the “Assigning Template Variables” lesson free?
Yes — the full text of “Assigning Template Variables” is free to read here on the web, and the Helm Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Helm Academy course, upgrade to CoddyKit PRO.
What will I learn in “Assigning Template Variables”?
Capturing values to reuse across a block. You practise Helm Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Helm Academy?
No prior experience is required. Helm Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Assigning Template Variables” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Helm Academy lesson?
Yes. Every Helm Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Assigning Template Variables
- Preserving the Root Dot Inside range
- nindent vs indent in Practice
- Debugging Rendered Whitespace