Skip to content

Commit a825a96

Browse files
authored
Merge pull request #18 from togglebyte/dev
new `either` docs
2 parents 2a5b6de + 9b780c2 commit a825a96

File tree

5 files changed

+64
-2
lines changed

5 files changed

+64
-2
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
# Anathema guide
22

3-
A guide to using the Anathema TUI library.
3+
[The Guide](https://togglebyte.github.io/anathema-guide/) to using the Anathema TUI library.

src/SUMMARY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
- [Templates](./templates.md)
1414
- [Loops](./templates/loops.md)
1515
- [If / Else](./templates/if-else.md)
16+
- [Either](./templates/either.md)
1617
- [Switch / Case](./templates/switch-case.md)
1718
- [With](./templates/with.md)
1819
- [Functions](./templates/functions.md)

src/templates.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ text "`ident` in the component attributes: " attributes.ident
7979
Some elements can have one or more children.
8080

8181
Example: a border element with a text element inside
82+
8283
```
8384
border
8485
text "look, a border"
@@ -88,11 +89,13 @@ border
8889
* [If / Else](./templates/if-else.md)
8990
* [Switch / Case](./templates/switch-case.md)
9091

91-
9292
## Either
9393

9494
The `?` symbol can be used to have a fallback value.
9595

96+
See [Either](./templates/either.md) for specifics on how this works as the
97+
behaviour differs between static values (defined in templates) and dynamic values (such as attributes and states).
98+
9699
```
97100
text state.maybe_value ? attributes.maybe_this ? "there was no value"
98101
```

src/templates/either.md

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# Either
2+
3+
The Either operator: `?` can be used to provide fallback values.
4+
5+
Only dynamic values (attributes and state) are subject to truthiness
6+
checks; Static values (defined using `let` in a template) are never (even if they are false).
7+
8+
```
9+
Does this exist?
10+
and is it truthy?
11+
/ \
12+
/ \
13+
yes no -------+
14+
| |
15+
V V
16+
text state.value ? "default"
17+
```
18+
19+
## Example
20+
21+
This will always use the static value, and print `false`:
22+
23+
```
24+
text false ? "hello"
25+
```
26+
However
27+
```
28+
text state.maybe_false ? "hello"
29+
```
30+
31+
Would print `hello` **if** `maybe_false` is any of the values in the [fallback table](./either.md#fallback-table):
32+
33+
### Fallback table
34+
35+
These values causes fallback.
36+
37+
* `null`
38+
* `0`
39+
* `""`
40+
* `[]`
41+
* `{}`
42+
* `false`
43+
44+
This means that any state or attribute value that resolves to a value listed
45+
above will use the fallback.

src/templates/switch-case.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,14 @@
11
# Switch / Case
2+
3+
It's possible to use `switch` / `case` to determine what elements to render.
4+
5+
The `default` branch has to be the last one, and is not required.
6+
7+
## Example
8+
9+
```
10+
switch value
11+
case 1: text "one"
12+
case 2: text "two"
13+
default: text "default"
14+
```

0 commit comments

Comments
 (0)