<p>I would like to create a template file which could interact with lists as regular values. Basically, loop over them.
So in other words, imagine the following 2 files:</p>
<pre><code>values1.yaml

jobs:
  single: true
  foo: bar
  kek: bor
</code></pre>
<p>and</p>
<pre><code>values2.yaml

jobs:
  - foo: bar
    kek: bor
  - foo: rab
    kek: rob
</code></pre>
<p>How can I create a template that works on both values files?
I personally thought of injecting the first file in a list by doing something like:</p>
<pre><code>{{ if Values.jobs.single }}
    {{ $myList := list .Values.jobs }}
{{ else }}
    {{ $myList := .Values.jobs }}
{{- end }}
{{- range $myList }}
   ...
</code></pre>
<p>Unfortunately, this does not seem to work, because assignments are local (so only available within the if-else block).</p>

Jeroen
<p>Helm has <a href="https://docs.helm.sh/docs/chart_template_guide/function_list/#reflection-functions" rel="nofollow noreferrer">a couple of functions that can inspect the type of an object</a>.  These generally use the Go terminology, so what you'd normally call a &quot;list&quot; in most languages is a &quot;slice&quot; in Go.</p>
<p>To simplify some issues around variable scoping, I'd also define the variable only once, and use the <a href="https://docs.helm.sh/docs/chart_template_guide/function_list/#ternary" rel="nofollow noreferrer"><code>ternary</code></a> function to move the conditional inline.</p>
<pre class="lang-none prettyprint-override"><code>{{- $j := .Values.jobs }}
{{- $myList := kindIs &quot;slice&quot; $j | ternary $j (list $j) }}
{{- range $myList }}...{{ end }}
</code></pre>
<p>In a language with more natural support for inline conditionals, this would be the equivalent of</p>
<pre class="lang-rb prettyprint-override"><code>j = values[:jobs]
my_list = if j.is_a?(Array)
          then j
          else [j]
          end
my_list.each {|dict| ...}
</code></pre>
