@@ -35,7 +35,7 @@ Consider the following program, which iterates over a slice of snacks and genera
35
35
Within the range block, the dot ` . ` is set to successive elements of the slice. In the first iteration, for instance,
36
36
` . ` holds the first element of the slice: ` (sdict "Name" "chips" "Calories" 540) ` . Hence
37
37
38
- ``` txt
38
+ ``` yag
39
39
{{ .Name }} contain {{ .Calories }} calories.
40
40
```
41
41
@@ -55,9 +55,9 @@ chips contain 540 calories.
55
55
crackers contain 500 calories.
56
56
```
57
57
58
- Observe that this output contains some unwanted whitespace; ideally, we want each snack to appear on a separate line
59
- with no leading indentation. However, the extra whitespace is to be expected with our current program: the range block
60
- is indented, and YAGPDB simply reproduces that indentation.
58
+ This output contains some unwanted whitespace: ideally, we want each snack to appear on a separate line with no leading
59
+ indentation. However, the extra whitespace is to be expected with our current program: the range block is indented, and
60
+ YAGPDB is simply reproducing that indentation.
61
61
62
62
``` yag
63
63
{{ range $snacks }}
@@ -73,8 +73,8 @@ One solution, then, is to remove the whitespace in our source code, save the fin
73
73
{{ end }}
74
74
```
75
75
76
- Although this version works, we have sacrificed readability. To retain the indentation in our source code while
77
- simultaneously avoiding unwanted whitespace in our output, we can use _ trim markers_ .
76
+ Although this version works, we have sacrificed readability in the process . To retain the indentation in our source code
77
+ while simultaneously avoiding unwanted whitespace in our output, we can use _ trim markers_ .
78
78
79
79
``` yag
80
80
{{ range $snacks }}
@@ -95,30 +95,36 @@ Use trim markers `{{-` and `-}}` to remove unwanted whitespace in output while k
95
95
#### Ranging over maps
96
96
97
97
It is also possible to range over the (key, value) pairs of a map. To do so, assign two variables to the result of the
98
- range action, corresponding to the key and value respectively:
98
+ range action, corresponding to the key and value respectively. For example, the following program outputs the prices of
99
+ various types of fruit, formatted nicely to 2 decimal places with the ` printf ` function.
99
100
100
101
``` yag
101
- {{/* key is fruit; value is price */}}
102
102
{{ $fruitPrices := sdict "pineapple" 3.50 "apple" 1.50 "banana" 2.60 }}
103
103
104
- {{/*
105
- Variable names are arbitrary:
106
- range $foo, $bar := $fruitPages
107
- would work too as long as you are consistent.
108
- */}}
109
104
{{ range $fruit, $price := $fruitPrices }}
110
105
{{- $fruit }} costs ${{ printf "%.02f" $price }}.
111
- {{- /*
112
- As with a slice, in each iteration the dot . is set to the current value, so
113
- printf "%.02f" .
114
- also works.
115
- */}}
116
106
{{ end }}
117
107
```
118
108
109
+ Note that the names of the variables assigned to the key and value are arbitrary; instead of
110
+ ` range $fruit, $price := $fruitPrices ` , we could also have written ` range $k, $v := $fruitPrices ` . However,
111
+ if we use the names ` $k ` , ` $v ` , we must consistently refer to those in the loop body. That is, the following program
112
+ is erroneous:
113
+
114
+ ``` yag
115
+ {{ range $k, $v := $fruitPrices }}
116
+ {{- /* ERROR: undefined variables $fruit, $price */}}
117
+ {{- $fruit }} costs ${{ printf "%.02f" $price }}.
118
+ {{ end }}
119
+ ```
120
+
121
+ {{< callout context="tip" title="Tip" icon="outline/rocket" >}}
122
+
119
123
The two-variable form of range can also be used with a slice, in which case the first variable tracks the position of
120
124
the element starting from ` 0 ` .
121
125
126
+ {{< /callout >}}
127
+
122
128
#### Rarer forms of range
123
129
124
130
There are a few other, less common ways to invoke the range action.
0 commit comments