Skip to content

Commit 1423e6c

Browse files
authored
Merge pull request #1898 from joebowbeer/patch-2
Clarify missingkey documentation
2 parents 236e0cd + dbbdf92 commit 1423e6c

File tree

1 file changed

+43
-49
lines changed

1 file changed

+43
-49
lines changed

docs/howto/charts_tips_and_tricks.md

Lines changed: 43 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,56 @@ library](https://masterminds.github.io/sprig/), except `env` and `expandenv`, fo
1818

1919
We also added two special template functions: `include` and `required`. The
2020
`include` function allows you to bring in another template, and then pass the
21-
results to other template functions.
21+
results to other template functions. The `required` function allows you to declare
22+
a particular values entry as required for template rendering. If the value is empty,
23+
the template rendering will fail with a user submitted error message.
2224

23-
For example, this template snippet includes a template called `mytpl`, then
25+
### Using the 'include' Function
26+
27+
Go provides a way of including one template in another using a built-in
28+
`template` directive. However, the built-in function cannot be used in Go
29+
template pipelines.
30+
31+
To make it possible to include a template, and then perform an operation on that
32+
template's output, Helm has a special `include` function:
33+
34+
```
35+
{{ include "toYaml" $value | indent 2 }}
36+
```
37+
38+
The above includes a template called `toYaml`, passes it `$value`, and then
39+
passes the output of that template to the `indent` function.
40+
41+
Because YAML ascribes significance to indentation levels and whitespace, this is
42+
one great way to include snippets of code, but handle indentation in a relevant
43+
context.
44+
45+
This template snippet includes a template called `mytpl`, then
2446
lowercases the result, then wraps that in double quotes.
2547

2648
```yaml
2749
value: {{ include "mytpl" . | lower | quote }}
2850
```
2951
30-
The `required` function allows you to declare a particular values entry as
31-
required for template rendering. If the value is empty, the template rendering
32-
will fail with a user submitted error message.
52+
### Using the 'required' function
53+
54+
Go provides a `missingkey` template option to control behavior when a map is indexed
55+
with a key that's not present in the map. There may be situations where a chart
56+
developer wants to enforce this behavior for select values in the `values.yaml` file.
57+
58+
The `required` function gives developers the ability to declare a value entry as
59+
required for template rendering. If the entry is empty in `values.yaml`, the
60+
template will not render and will return an error message supplied by the
61+
developer.
62+
63+
For example:
64+
65+
```
66+
{{ required "A valid foo is required!" .Values.foo }}
67+
```
68+
69+
The above will render the template when `.Values.foo` is defined, but will fail
70+
to render and exit when `.Values.foo` is undefined.
3371

3472
The following example of the `required` function declares an entry for
3573
`.Values.who` is required, and will print an error message when that entry is
@@ -66,50 +104,6 @@ env:
66104
value: "1234"
67105
```
68106

69-
## Using the 'include' Function
70-
71-
Go provides a way of including one template in another using a built-in
72-
`template` directive. However, the built-in function cannot be used in Go
73-
template pipelines.
74-
75-
To make it possible to include a template, and then perform an operation on that
76-
template's output, Helm has a special `include` function:
77-
78-
```
79-
{{ include "toYaml" $value | indent 2 }}
80-
```
81-
82-
The above includes a template called `toYaml`, passes it `$value`, and then
83-
passes the output of that template to the `indent` function.
84-
85-
Because YAML ascribes significance to indentation levels and whitespace, this is
86-
one great way to include snippets of code, but handle indentation in a relevant
87-
context.
88-
89-
## Using the 'required' function
90-
91-
Go provides a way for setting template options to control behavior when a map is
92-
indexed with a key that's not present in the map. This is typically set with
93-
`template.Options("missingkey=option")`, where `option` can be `default`,
94-
`zero`, or `error`. While setting this option to error will stop execution with
95-
an error, this would apply to every missing key in the map. There may be
96-
situations where a chart developer wants to enforce this behavior for select
97-
values in the `values.yaml` file.
98-
99-
The `required` function gives developers the ability to declare a value entry as
100-
required for template rendering. If the entry is empty in `values.yaml`, the
101-
template will not render and will return an error message supplied by the
102-
developer.
103-
104-
For example:
105-
106-
```
107-
{{ required "A valid foo is required!" .Values.foo }}
108-
```
109-
110-
The above will render the template when `.Values.foo` is defined, but will fail
111-
to render and exit when `.Values.foo` is undefined.
112-
113107
## Using the 'tpl' Function
114108

115109
The `tpl` function allows developers to evaluate strings as templates inside a

0 commit comments

Comments
 (0)