helm 中的“template”和“include”关键字有何不同?两者似乎都是用来渲染模板参数的
Dav*_*aze 44
template是 Go 语言核心的一部分text/template。它总是将其结果呈现给模板输出;它不产生值,并且其结果无法捕获在变量中或包含在管道中。
include是一个Helm 扩展。它捕获模板输出并将其作为字符串返回。其结果可以像任何其他支持函数调用一样使用。 include不是“关键字”或“动作”或“特殊形式”,从模板语言的角度来看,它是一个普通的扩展函数。
如果您不确定,在 Helm 图表的背景下,include通常不会错。
这种差异最重要的地方是您有一个生成 YAML 片段的块,并且您需要缩进它。Helm 包含一个indent可以执行此操作的函数,但它需要一个字符串来执行此操作,因此您需要使用includeand 而不是template这里。
{{- define "foo.labels" -}}
foo: bar
{{ end -}}
metadata:
labels:
{{ include "foo.labels" . | indent 4 }}
spec:
template:
metadata:
{{ include "foo.labels" . | indent 8 }}
Run Code Online (Sandbox Code Playgroud)
对于更直接的示例,请考虑仅引用其参数的模板。如果使用 调用此函数template,模板管道语法将应用于模板参数。如果使用 调用它include,它将应用于模板调用的结果。将此与 结合起来indent,无论您在引号内部还是外部看到缩进,都会有明显的差异。
{{- define "foo.labels" -}}
foo: bar
{{ end -}}
metadata:
labels:
{{ include "foo.labels" . | indent 4 }}
spec:
template:
metadata:
{{ include "foo.labels" . | indent 8 }}
Run Code Online (Sandbox Code Playgroud)
{{ define "quote" }}{{ quote . }}{{ end }}
{{/* "hello" is indented, then " hello" is quoted */}}
Template: {{ template "quote" "hello" | indent 2 }}
{{/* "hello" is quoted, then '"hello"' is indented */}}
Include: {{ include "quote" "hello" | indent 2 }}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
14526 次 |
| 最近记录: |