Skip to content

Commit 7e55a60

Browse files
committed
learn: elaborate on vars and printf in cf2
1 parent 0c22759 commit 7e55a60

File tree

1 file changed

+24
-18
lines changed

1 file changed

+24
-18
lines changed

content/learn/intermediate/control-flow-2.md

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ Consider the following program, which iterates over a slice of snacks and genera
3535
Within the range block, the dot `.` is set to successive elements of the slice. In the first iteration, for instance,
3636
`.` holds the first element of the slice: `(sdict "Name" "chips" "Calories" 540)`. Hence
3737

38-
```txt
38+
```yag
3939
{{ .Name }} contain {{ .Calories }} calories.
4040
```
4141

@@ -55,9 +55,9 @@ chips contain 540 calories.
5555
crackers contain 500 calories.
5656
```
5757

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.
6161

6262
```yag
6363
{{ range $snacks }}
@@ -73,8 +73,8 @@ One solution, then, is to remove the whitespace in our source code, save the fin
7373
{{ end }}
7474
```
7575

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_.
7878

7979
```yag
8080
{{ range $snacks }}
@@ -95,30 +95,36 @@ Use trim markers `{{-` and `-}}` to remove unwanted whitespace in output while k
9595
#### Ranging over maps
9696

9797
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.
99100

100101
```yag
101-
{{/* key is fruit; value is price */}}
102102
{{ $fruitPrices := sdict "pineapple" 3.50 "apple" 1.50 "banana" 2.60 }}
103103
104-
{{/*
105-
Variable names are arbitrary:
106-
range $foo, $bar := $fruitPages
107-
would work too as long as you are consistent.
108-
*/}}
109104
{{ range $fruit, $price := $fruitPrices }}
110105
{{- $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-
*/}}
116106
{{ end }}
117107
```
118108

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+
119123
The two-variable form of range can also be used with a slice, in which case the first variable tracks the position of
120124
the element starting from `0`.
121125

126+
{{< /callout >}}
127+
122128
#### Rarer forms of range
123129

124130
There are a few other, less common ways to invoke the range action.

0 commit comments

Comments
 (0)