<p>How do I make an optional block in the values file and then refer to it in the template?</p>
<p>For examples, say I have a values file that looks like the following:</p>
<pre><code># values.yaml
foo:
   bar: &quot;something&quot;
</code></pre>
<p>And then I have a helm template that looks like this:</p>
<pre><code>{{ .Values.foo.bar }}
</code></pre>
<p>What if I want to make the foo.bar in the values file optional? An error is raised if the <code>foo</code> key does not exist in the values.</p>
<p>I've tried adding as an if conditional. However, this still fails if the <code>foo</code> key is missing:</p>
<pre><code>{{ if .Values.foo.bar }}
{{ .Values.foo.bar }}
{{ end }}
</code></pre>

<h3>Simple workaround</h3>
<p>Wrap <strong>each nullable level</strong> with parentheses <code>()</code>.</p>
<pre><code>{{ ((.Values.foo).bar) }}
</code></pre>
<p>Or</p>
<pre><code>{{ if ((.Values.foo).bar) }}
{{ .Values.foo.bar }}
{{ end }}
</code></pre>
<h3>How does it work?</h3>
<p>Helm uses the go <code>text/template</code> and inherits the behaviours from there.</p>
<p>Each pair of parentheses <code>()</code> can be considered a <code>pipeline</code>.</p>
<p>From the doc (<a href="https://pkg.go.dev/text/template#hdr-Actions" rel="noreferrer">https://pkg.go.dev/text/template#hdr-Actions</a>)</p>
<p>It is:</p>
<blockquote>
<p>The default textual representation (the same as would be printed by fmt.Print)...</p>
</blockquote>
<p>With the behaviour:</p>
<blockquote>
<p>If the value of the pipeline is empty, <strong>no output is generated</strong>... The empty values are false, 0, <strong>any nil pointer or interface value</strong>, and any array, slice, map, or string of length zero.</p>
</blockquote>
<p>As such, by wrapping <strong>each nullable level</strong> with parentheses, when they are chained, the predecessor nil pointer gracefully generates no output to the successor and so on, achieving the nested nullable fields workaround.</p>
