<p>I'm using a Helm chart and I was wondering how I can define a value by default. In my case, I wanna define a date when it isn't defined in values.yaml and I have the following code:</p>
<pre><code>{{- if ne .Value.persistence.home.restoreBackup.date &quot;&quot; }}
{{- $bkDate := .Value.persistence.home.restoreBackup.date }}
{{- else }}
{{- $bkDate := &quot;2022-01-01&quot; }}
{{- end }}
</code></pre>
<p>I wanna set <strong>$bkDate</strong> to an specific date if it is not defined in <code>.Value.persistence.home.restoreBackup.date</code> but when I try to print <strong>$bkDate</strong> it is empty.</p>
<p>Do you know what is wrong here?</p>

Felipe
<p>The <a href="https://pkg.go.dev/text/template" rel="nofollow noreferrer">Go <code>text/template</code> documentation</a> notes (under &quot;Variables&quot;):</p>
<blockquote>
<p>A variable's scope extends to the &quot;end&quot; action of the control structure (&quot;if&quot;, &quot;with&quot;, or &quot;range&quot;) in which it is declared....</p>
</blockquote>
<p>This essentially means you can't define a variable inside an <code>if</code> block and have it visible outside the block.</p>
<p>For what you want to do, the <a href="https://docs.helm.sh/docs/chart_template_guide/function_list/#default" rel="nofollow noreferrer">Helm <code>default</code> function</a> provides a straightforward workaround.  You can unconditionally define the variable, but its value is the Helm value or else some default if that's not defined.</p>
<pre class="lang-none prettyprint-override"><code>{{- $bkDate := .Value.persistence.home.restoreBackup.date | default &quot;2022-01-01&quot; -}}
</code></pre>
<p>(Note that a couple of things other than empty-string are &quot;false&quot; for purposes of <code>default</code>, including &quot;unset&quot;, <code>nil</code>, and empty-list, but practically this won't matter to you.)</p>
<p>If you need more complex logic than this then the <a href="https://docs.helm.sh/docs/chart_template_guide/function_list/#ternary" rel="nofollow noreferrer"><code>ternary</code></a> function could meet your needs <code>{{- $var := $someCondition | ternary &quot;trueValue&quot; &quot;falseValue&quot; -}}</code> but this leads to hard-to-read expressions, and it might be better to refactor to avoid this.</p>
