<p>There are some external files (e.g - &quot;panels/pods/memory.txt&quot;)  which is needed to read dynamically based on parameters inside configMap.yaml file</p>
<pre><code>=&gt; cat templates/configMap.yaml
fileContent_via_template: {{ include &quot;readfile&quot; (dict &quot;metrics&quot; &quot;panels/pods/memory.txt&quot;) }}

=&gt; cat panels/pods/memory.txt
This is dummy content which is displayed from memory.txt file
</code></pre>
<p>To read external file, defined a template -</p>
<pre><code>=&gt; templates/_helpers.tpl
{{- define &quot;readfile&quot; -}}
  {{- $content := .Files.Get .metrics -}}
  {{ $content }}
{{- end -}
</code></pre>
<p>It throws below error -</p>
<pre><code>Error: template: grafana/templates/configMap.yaml:2:22: executing &quot;grafana/templates/configMap.yaml&quot; at &lt;include &quot;readfile&quot; (dict &quot;metrics&quot; &quot;panels/pods/memory.txt&quot;)&gt;: error calling include: template: grafana/templates/_helpers.tpl:42:24: executing &quot;readfile&quot; at &lt;.Files.Get&gt;: nil pointer evaluating interface {}.Get
</code></pre>
<p>If same external file is read from configMap.yaml directly, then it works -</p>
<pre><code>=&gt; cat templates/configMap.yaml
fileContent: {{ .Files.Get &quot;panels/pods/memory.txt&quot; }}
</code></pre>
<p>It returns the below output:</p>
<pre><code>---
# Source: grafana/templates/configMap.yaml
fileContent: This is dummy content which is displayed from memory.txt file


</code></pre>

Ashish Kumar
<p>Inside the <code>readfile</code> template, you refer to <code>.Files.Get</code>.  This is normal Go template syntax: from the object <code>.</code>, get the field <code>Files</code>, and within that object, get the field <code>Get</code>.  However, <code>.</code> is no longer the top-level Helm object but rather the parameter to the template.</p>
<p>You could fix this without changing the inner template code by explicitly passing <code>.Files</code> as a parameter</p>
<pre class="lang-none prettyprint-override"><code>{{ include &quot;readfile&quot; (dict &quot;Files&quot; .Files &quot;metrics&quot; &quot;panels/pods/memory&quot;) }}
{{/*                        ^^^^^^^^^^^^^^                               */}}
</code></pre>
<p>If you also needed other characterictics like <code>.Values</code>, or if you needed to pass the whole top-level object on to some other template, you may need to pass the top-level object as a parameter.  In my templates I like to call this <code>top</code> but I don't think I've seen a specific convention for this in the wild.</p>
<p>In this case, you'd need to both change the template to refer to <code>.Files</code> out of the <code>top</code> object</p>
<pre class="lang-none prettyprint-override"><code>{{/* Reads a file as in .Files.Get and ....  Takes a dictionary with two
     fields.  `metrics` is the name of the file to read, and `top` is the
     top-level Helm object. */}}
{{ define &quot;readfile&quot; }}
  {{- $content := .top.Files.Get .metrics -}}
  {{/*-           ^^^^                  -*/}}
  {{ $content }}
{{- end }}
</code></pre>
<p>At the call site, again, you need to add the parameter.</p>
<pre class="lang-none prettyprint-override"><code>{{ include &quot;readfile&quot; (dict &quot;top&quot; . &quot;metrics&quot; &quot;panels/pods/memory&quot;) }}
{{/*                        ^^^^^^^                               */}}
</code></pre>
