Helm Named Templates: Difference between revisions
Line 14: | Line 14: | ||
By using the specific chart name as a prefix we can avoid any conflicts that may arise due to two different charts that declare sub-templates of the same name. | By using the specific chart name as a prefix we can avoid any conflicts that may arise due to two different charts that declare sub-templates of the same name. | ||
==Sub-Template Files== | ==Sub-Template Files== | ||
Sub-templates can be declared inside other template files, in helper files, for example [[Helm_Chart_helpers.tpl|_helpers.tpl]] or in their own files, inside the chart's templates/ directory. For more details on templates/ directory and template file conventions, see: | Sub-templates can be declared inside other template files, in helper files, for example [[Helm_Chart_helpers.tpl|_helpers.tpl]] or in their own files, inside the chart's [[Helm_Templates#The_templates.2F_Directory|templates/]] directory. For more details on templates/ directory and template file conventions, see: | ||
{{Internal|Helm_Templates#The_templates.2F_Directory|templates/}} | {{Internal|Helm_Templates#The_templates.2F_Directory|templates/}} | ||
Revision as of 23:40, 7 October 2020
External
Internal
Overview
A named template, also known as a partial, sub-template or embedded template, is a fragment of text that is declared in one file and then rendered in-line into another template, usually defined in a different file, every time it is invoked with the template action or the include function. A sub-template is declared with the define action. A sub-template name has a name, declared when the sub-template is defined. The sub-template body may contain action and function declaration, which will be rendered when the enclosing template is rendered.
Sub-Template Elements
Sub-Template Name
Sub-template names, including those declared in subcharts, are global. This means that if two sub-templates have the same name, whichever is loaded last will be the one to be used. For this reason, is a good practice to name the sub-templates with chart-specific names, and prefix the name of the sub-template with the name of the chart:
mychart.mysubtemplate
By using the specific chart name as a prefix we can avoid any conflicts that may arise due to two different charts that declare sub-templates of the same name.
Sub-Template Files
Sub-templates can be declared inside other template files, in helper files, for example _helpers.tpl or in their own files, inside the chart's templates/ directory. For more details on templates/ directory and template file conventions, see:
Sub-Template Scope
Actions
define
The 'define' action is used to declare the name and the content of the sub-template:
{{- define "mychart.mysubtemplate" }}
labels:
color: blue
date: {{ now | htmlDate }}
{{- end }}
template
The 'template' action renders the specified sub-template, with the given scope, in the enclosing template.
...
kind: ConfigMap
metadata:
name: test-cm
{{- template "mychart.mysubtemplate" . }}
...