Preserving the Root Dot Inside range
Using a captured root reference inside loops.
Preserving the Root Dot Inside range is a free Helm Academy lesson on CoddyKit — lesson 2 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.
The Dot Can Move
Inside a range loop, the dot stops meaning the chart root and instead points at the current item. That trips up many first-time authors.
Why That Is a Problem
Once the dot is rebound, expressions like .Values or .Release no longer work inside the loop, because the root context is no longer the dot.
Capture the Root First
The fix is simple: before the loop, save the root dot into a variable. Then reach the original context through that variable inside range.
{{ $root := . }}Use the Saved Root
Inside the loop, swap any chart-wide path to go through your variable. Write $root.Values instead of .Values to escape the local dot.
{{ range .Values.items }}
release: {{ $root.Release.Name }}
{{ end }}The Local Dot Still Works
For the loop item itself, keep using the plain dot. Inside range the dot is the current element, which is exactly what you want there.
{{ range .Values.ports }}
- port: {{ . }}
{{ end }}Mixing Item and Root
Real templates need both at once: the item from the dot and a chart value from the root. Capturing $root lets you reference each cleanly.
{{ $root := . }}
{{ range .Values.hosts }}
host: {{ . }}.{{ $root.Values.domain }}
{{ end }}A Common Naming Choice
Many charts name this variable $root or $top, but the name is yours. What matters is capturing the dot before range rebinds it.
range With Index and Value
The two-variable form binds the index and value directly, so the dot is free. Even so, capturing the root stays the safest habit.
{{ range $i, $v := .Values.ports }}
- {{ $i }}: {{ $v }}
{{ end }}Same Trap Inside with
The with block also rebinds the dot to a subtree. The very same root-capture trick rescues your chart-wide references inside it.
Test the Loop Output
Render the chart and inspect the loop. helm template shows whether each iteration resolved the root values the way you expected.
helm template ./mychartCapture Before You Enter
Order matters: assign $root := . outside the loop. Capture it inside range and the dot is already rebound, so you save the wrong value.
Quick Check
Inside a range loop, {{ .Values.port }} suddenly returns nothing.
Recap
You learned that range rebinds the dot, that $root := . saves the original context, and that the local dot stays the loop item. 🔁
Frequently asked questions
Is the “Preserving the Root Dot Inside range” lesson free?
Yes — the full text of “Preserving the Root Dot Inside range” 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 “Preserving the Root Dot Inside range”?
Using a captured root reference inside loops. 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 2 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Preserving the Root Dot Inside range” 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