Skip to content

Commit 596b767

Browse files
authored
Merge pull request #27 from togglebyte/dev
document locals and globals
2 parents 7178442 + ddf69e2 commit 596b767

File tree

3 files changed

+77
-40
lines changed

3 files changed

+77
-40
lines changed

src/SUMMARY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
- [Either](./templates/either.md)
1717
- [Switch / Case](./templates/switch-case.md)
1818
- [With](./templates/with.md)
19+
- [Variables](./templates/variables.md)
1920
- [Functions](./templates/functions.md)
2021
- [Custom functions](./templates/functions/custom.md)
2122
- [Elements](./templates/elements.md)

src/templates.md

Lines changed: 0 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -99,43 +99,3 @@ behaviour differs between static values (defined in templates) and dynamic value
9999
```
100100
text state.maybe_value ? attributes.maybe_this ? "there was no value"
101101
```
102-
103-
## Constants / Globals
104-
105-
Constants are global and regardless of where in a template they are declared
106-
they will be hoisted to the top.
107-
108-
### Example
109-
110-
The following example will print `1`
111-
```
112-
text glob
113-
114-
let glob = 1
115-
```
116-
117-
The following example will print `1` then `2`, as the `inner` component will override the
118-
outer components global.
119-
```
120-
// main.aml
121-
let glob = 1
122-
vstack
123-
text glob
124-
@inner
125-
126-
// inner.aml
127-
let glob = 2
128-
text glob
129-
```
130-
131-
The following example will print `2`, as the `inner` component will override the
132-
outer components global.
133-
```
134-
// main.aml
135-
let glob = glob
136-
@inner
137-
138-
// inner.aml
139-
let glob = 2
140-
text glob
141-
```

src/templates/variables.md

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
# Variables
2+
3+
Template variables are closer to constants than variables, since they can not be reassigned.
4+
5+
There are two kinds:
6+
* Global
7+
* Local
8+
9+
A local value will always shadow a global value with the same name, and `with` / `for` will always shadow local values.
10+
11+
## Global
12+
13+
Globals are defined using the `global` keyword: `global <ident> = <expression>`.
14+
15+
Once a global is defined it can not be re-defined.
16+
Trying to define a global twice will result in an error.
17+
18+
### Example
19+
20+
```
21+
global value = 1
22+
text value
23+
```
24+
25+
### Notes
26+
27+
Global values can be defined anywhere in the template, but it's best practice to
28+
place them at the to of the template.
29+
30+
## Local
31+
32+
<small><i>The old `let` keyword is now an alias for `local`.</i></small>
33+
34+
Locals are defined using the `local` keyword: `local <ident> = <expression>`.
35+
36+
A `local` value is never accessible outside the current component, and is scoped
37+
to the parent element.
38+
39+
### Example
40+
41+
```
42+
local value = 1
43+
text value
44+
```
45+
46+
### Notes
47+
48+
Unlike `global`s the placement within the template is relevant for `local`s.
49+
50+
The following will display `1`:
51+
```
52+
local x = 1
53+
vstack
54+
text x
55+
```
56+
57+
The following template
58+
59+
```
60+
local x = 1
61+
vstack
62+
text x
63+
vstack
64+
local x = 2
65+
text x
66+
text x
67+
```
68+
69+
will display
70+
71+
```
72+
1
73+
2
74+
1
75+
```
76+

0 commit comments

Comments
 (0)