<p>I am new to helm and helm templating language. I have the following in my _helper.tpl:</p>

<pre><code>{{/*
Get couchdb password
*/}}
{{- define "couchdb.password" -}}
{{- $name := default .Chart.Name .Values.nameOverride -}}
{{- printf "'@refsec/couchdb-%s/adminPassword'" $name -}}
{{- end -}}

{{/* 
Get couchdb username 
*/}}
{{- define "couchdb.username" -}}
{{- $name := default .Chart.Name .Values.nameOverride -}}
{{- printf "'@refsec/couchdb-%s/adminUsername'" $name -}}
{{- end -}}
</code></pre>

<p>But there is an issue with this piece of code. REPETTION!! As you see this line is repeated 2 times: <code>{{- $name := default .Chart.Name .Values.nameOverride -}}</code></p>

<p>Can I define a global variable for these casesa and use it over and over without repetition?</p>

<p>In principle you can define a template just for that one line</p>

<pre><code>{{- define "couchdb.chart.name" -}}
{{- default .Chart.Name .Values.nameOverride -}}
{{- end -}}
</code></pre>

<p>But the syntax to invoke it isn't actually much shorter</p>

<pre><code>{{- define "couchdb.password" -}}
{{- $name := include "couchdb.chart.name" . -}}
{{- printf "'@refsec/couchdb-%s/adminPassword'" $name -}}
{{- end -}}

{{- define "couchdb.username" -}}
{{- printf "'@refsec/couchdb-%s/adminUsername'" (include "couchdb.chart.name" .) -}}
{{- end -}}
</code></pre>

<p>I'm pretty sure the Go text/template language doesn't have global variables in the form you're suggesting here.  The <a href="https://godoc.org/text/template" rel="nofollow noreferrer">documentation</a> describes variable syntax and invocation, but contains the slightly cryptic note</p>

<blockquote>
  <p>A template invocation does not inherit variables from the point of its invocation.</p>
</blockquote>

<p>and I think from reading other context there's not "global scope" so much as "a main template".</p>

<p>In any case, I've never seen a global variable in a Helm chart, and even variables in the form you show in the question are a little bit unusual.</p>
