<p>Helm newbie here. I am trying to loop through files and create a configmap with its contents. The file contents need to have variable defined inside the loop. Below is the configmap I am working with.</p>
<pre><code>apiVersion: v1
kind: ConfigMap
metadata:
  name: {{ template &quot;proj.name&quot; . }}
  labels:
    app.kubernetes.io/name: {{ include &quot;proj.name&quot; . }}
    app.kubernetes.io/version: {{ include &quot;proj.version&quot; . }}
data:
  {{- range $path, $_ := .Files.Glob &quot;myconfig/*.txt&quot; -}}
  {{ $path | base | nindent 2 }}: |
  {{- tpl ($.Files.Get $path) $ | nindent 4 }}
  {{- end -}}
</code></pre>
<p>contents of myconfig/name.txt</p>
<pre><code>i am able to access this -&gt; {{ .Values.somekey }}
But not this -&gt; {{ $path }}
</code></pre>
<p>I get error: <strong>undefined variable &quot;$path&quot;</strong>
Any help would be greatly appreciated. Thank you.</p>

Vijayendar Gururaja
<p>The <code>$path</code> variable is a local variable.  It's not accessible from other template functions or <code>tpl</code> expansions.  The special &quot;root context&quot; variable <code>$</code> doesn't include local variables either.</p>
<p>What you could do in this case is define your own context for the <code>tpl</code> function:</p>
<pre class="lang-none prettyprint-override"><code>{{- $context := dict &quot;somekey&quot; .Values.somekey &quot;path&quot; $path }}
{{- tpl ($.Files.Get $path) $context | nindent 4 }}
</code></pre>
<p>Then in the file you'd refer to the keys provided in that <code>dict</code> (but not other things from outside that explicit context)</p>
<pre class="lang-none prettyprint-override"><code>i am able to access this -&gt; {{ .somekey }}
and also this -&gt; {{ .path }}
but i wouldn't be able to reference .Values.anything
</code></pre>
