DevOps Bootcamp · Lesson

Scoping a Block with with

Narrowing the dot to a subtree, and its pitfalls.

Lesson 4 of 413 steps

Scoping a Block with with is a free DevOps Bootcamp lesson on CoddyKit. This is lesson 4 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the DevOps Bootcamp learning path, and your progress syncs across the web and the CoddyKit app. The DevOps Bootcamp course includes 4 lessons in total.

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 ./mychart

Nesting 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! 🎯

Free to start

Learn DevOps Bootcamp with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
142
Lessons
568

Frequently Asked Questions

Is the “Scoping a Block with with” lesson free?

Yes — any 3 lessons of the DevOps Bootcamp learning path, including “Scoping a Block with with”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The DevOps Bootcamp course includes 4 lessons in total.

What will I learn in “Scoping a Block with with”?

Narrowing the dot to a subtree, and its pitfalls. You practise DevOps Bootcamp 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 DevOps Bootcamp?

No prior experience is required. DevOps Bootcamp on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 4 of 4.

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 DevOps Bootcamp lesson?

Yes. Every DevOps Bootcamp 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

  1. Conditional Blocks with if and else
  2. Truthiness and Operators in Conditions
  3. Looping Over Lists and Maps with range
  4. Scoping a Block with with
← Back to DevOps Bootcamp