Skip to content

Commit 0795026

Browse files
committed
document attributes
1 parent e49fd51 commit 0795026

File tree

4 files changed

+141
-0
lines changed

4 files changed

+141
-0
lines changed

src/SUMMARY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
- [Messaging](./components/messages.md)
99
- [Element query](./components/element-query.md)
1010
- [Reusable components](./components/reusable.md)
11+
- [Attributes](./attributes.md)
1112
- [Runtime](./runtime.md)
1213
- [Backend](./backend.md)
1314
- [Templates](./templates.md)

src/attributes.md

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
# Attributes
2+
3+
Both components and elements have attributes.
4+
5+
These can be accessed through events.
6+
7+
## Example
8+
9+
```rust
10+
fn on_key(
11+
&mut self,
12+
key: KeyEvent,
13+
state: &mut Self::State,
14+
mut children: Children<'_, '_>,
15+
mut context: Context<'_, '_, Self::State>,
16+
) {
17+
// Components attributes
18+
context.attributes.get_as::<u32>("int").unwrap();
19+
20+
// Element attributes
21+
children.elements().by_tag("text").first(|el, attr| {
22+
attr.get_as::<bool>("is_true").unwrap();
23+
});
24+
25+
}
26+
```
27+
28+
## Attribute functions
29+
30+
These functions are available on attribute collections for both components and
31+
elements:
32+
33+
34+
### `get_as<T>(&str)`
35+
36+
Get a value as a given type.
37+
38+
```
39+
text [my_attribute: 123, is_true: false] "..."
40+
```
41+
42+
```rust
43+
attributes.get_as::<u32>("my_attribute").unwrap();
44+
attributes.get_as::<u8>("my_attribute").unwrap();
45+
attributes.get_as::<bool>("is_true").unwrap();
46+
```
47+
48+
### `get(&str)`
49+
50+
Get a value as a
51+
[`ValueKind`](https://docs.rs/anathema-value-resolver/latest/anathema_value_resolver/enum.ValueKind.html#variants).
52+
This is only useful if the value type of the attribute can change or is unknown.
53+
54+
55+
### `set(&str, impl Into<ValueKind>)`
56+
57+
Set an attribute value.
58+
59+
```rust
60+
attributes.set("key", 123).unwrap();
61+
attributes.set("other_key", false).unwrap();
62+
```
63+
64+
### `remove(&str)`
65+
66+
Remove an attribute value.
67+
68+
```rust
69+
attributes.remove("key");
70+
```
71+
72+
### `iter_as::<T>(&str)`
73+
74+
Get an iterator over values of an attribute
75+
76+
```
77+
@my_component [list: [1, 2, 3]]
78+
```
79+
80+
```rust
81+
let mut iter = attributes.iter_as::<u8>("list");
82+
for val in iter {
83+
// val is a u8
84+
}
85+
```
86+
87+
### `value_as<T>(&str)`
88+
89+
Get the value of an element / component as a `T`.
90+
91+
```rust
92+
attributes.value_as::<&str>().unwrap();
93+
```
94+
95+
### `value(&str)`
96+
97+
Get the value of an element / component.
98+
99+
100+
### `set_value(&str, value)`
101+
102+
Set the value of an element / component.
103+
104+
```rust
105+
attributes.set_value(123);
106+
```
107+

src/templates/functions.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,3 +163,13 @@ Will output:
163163
│hi │
164164
└────┘
165165
```
166+
167+
## `len(arg)`
168+
169+
Returns the length of a collection or a string
170+
171+
```
172+
text len(0..2)
173+
```
174+
Will output: `3`
175+

src/templates/variables.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,29 @@ and they are global across **all** templates.
1616
Once a global is defined it can not be re-defined.
1717
Trying to define a global twice will result in an error.
1818

19+
### Runtime Globals
20+
21+
Globals can be registered with the runtime as well.
22+
23+
Registering a global with the same name twice will result in an error.
24+
25+
#### Example
26+
27+
```rust
28+
let mut builder = Runtime::builder(doc, &backend);
29+
builder.register_global("string_value", "hello").unwrap();
30+
builder.register_global("num_value", 123).unwrap();
31+
builder.register_global("a_list", [1, 2, 3]).unwrap();
32+
```
33+
34+
```
35+
vstack
36+
text string_value
37+
text num_value
38+
for i in a_list
39+
text i
40+
```
41+
1942
### Example
2043

2144
```

0 commit comments

Comments
 (0)