Scoping a Block with with
Narrowing the dot to a subtree, and its pitfalls.
Scoping a Block with with is a free Helm Academy lesson on CoddyKit — lesson 4 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.
Stop Repeating Long Paths
Typing .Values.image.repository over and over gets noisy. The with action narrows the dot to a subtree so you can write shorter paths.
The with Action
A with block opens with {{ with SOMETHING }} and closes with {{ end }}. Inside it, the dot is reset to whatever you passed in.
{{ with .Values.image }}
repository: {{ .repository }}
tag: {{ .tag }}
{{ end }}The Dot Moves In
Once inside, .repository means .Values.image.repository. The with keyword rebinds the dot to the subtree, trimming every path below it.
image:
repository: nginx
tag: "1.27"It Doubles as a Guard
If the value is empty or missing, with skips its whole block. So it both scopes the dot and acts like an if at the same time. 🛡️
Adding an else
Like if, with accepts an else branch. The else renders when the value is empty, giving you a tidy fallback for optional config.
{{ with .Values.resources }}
resources:
{{ toYaml . | indent 2 }}
{{ else }}
# no resources set
{{ end }}The Scope Pitfall
Here is the trap: inside with, the dot is no longer the root. So .Release.Name breaks, because Release does not live under the subtree.
Reach Back With $
To use root values inside with, prefix them with $. The dollar always points at the original root context, no matter how deep you are.
{{ with .Values.image }}
name: {{ $.Release.Name }}
image: {{ .repository }}
{{ end }}Keep Blocks Small
Because with hijacks the dot, large blocks get confusing fast. Keep with sections short so it stays obvious what the dot points to.
with vs if
Use if when you only test a flag, and use with when you also want to dive into that value. with changes the dot; if leaves it alone.
Preview the Scope
Unsure what the dot holds? Render with helm template and read the output, or print the scope while debugging a tricky with block.
helm template ./mychartNesting with Blocks
You can nest a with inside another to dive deeper. Each level moves the dot one step further down, so keep track of where you are.
{{ with .Values.image }}
{{ with .pullSecrets }}
# . is now pullSecrets
{{ end }}
{{ end }}Quick Check
Inside a {{ with .Values.image }} block, you try to read .Release.Name and it is empty.
Recap
You now scope the dot with with, get a free emptiness guard, add an else fallback, and reach the root with $. Cleaner templates! 🎯
Frequently asked questions
Is the “Scoping a Block with with” lesson free?
Yes — the full text of “Scoping a Block with with” 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 “Scoping a Block with with”?
Narrowing the dot to a subtree, and its pitfalls. 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 4 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Scoping a Block with with” 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.