Conditional Blocks with if and else
Rendering resources only when configured.
Conditional Blocks with if and else 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.
Render Only When Needed
Sometimes a resource should appear only if the user asked for it. Helm lets you wrap that YAML in a conditional so it renders on demand.
The if Action
An if block starts with {{ if SOMETHING }} and closes with {{ end }}. Everything in between renders only when the condition is true.
{{ if .Values.ingress.enabled }}
# ingress yaml here
{{ end }}Driven by Values
The condition is usually a value the user sets. Flip ingress.enabled to true in values.yaml and the wrapped manifest suddenly appears.
ingress:
enabled: trueAlways Close With end
Every if must be paired with an {{ end }}. Forget it and Helm throws an unterminated-block error when it tries to render the chart.
Adding an else
Want a fallback? Add an {{ else }} branch. Helm renders the first block when true, otherwise it renders the else block instead.
{{ if .Values.tls }}
protocol: HTTPS
{{ else }}
protocol: HTTP
{{ end }}Chaining With else if
For several cases, chain conditions with {{ else if }}. Helm checks each in order and renders the first branch whose condition is true.
{{ if eq .Values.tier "gold" }}
replicas: 5
{{ else if eq .Values.tier "silver" }}
replicas: 3
{{ else }}
replicas: 1
{{ end }}What Counts as True
An if is true unless the value is empty: false, zero, an empty string, or an empty list all read as false in a condition.
Guarding Optional Fields
A common pattern wraps one optional field, not a whole resource. Here the annotation only renders when the user provides a value for it.
{{ if .Values.email }}
contact: {{ .Values.email }}
{{ end }}Mind the Blank Lines
An if block leaves behind blank lines where the action used to sit. That whitespace can break YAML, so you will soon learn to trim it. 🧹
Preview the Branches
Render with different values and watch which branch wins. helm template with --set lets you confirm each condition before you ever deploy.
helm template ./mychart --set ingress.enabled=trueDefault a Missing Flag
If a user never sets the flag, it reads as empty and your if stays false. That is why charts ship a sensible default for it in values.yaml.
ingress:
enabled: falseQuick Check
You write {{ if .Values.cache }} but the chart fails to render.
Recap
You can now gate YAML with if, add else and else if branches, and remember the closing {{ end }}. Conditional rendering unlocked! 🎯
Frequently asked questions
Is the “Conditional Blocks with if and else” lesson free?
Yes — the full text of “Conditional Blocks with if and else” 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 “Conditional Blocks with if and else”?
Rendering resources only when configured. 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 “Conditional Blocks with if and else” 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
- Conditional Blocks with if and else
- Truthiness and Operators in Conditions
- Looping Over Lists and Maps with range
- Scoping a Block with with