Declarative bindings
data-state-* attributes connect the DOM to the state. When values change the DOM updates itself; no JS needed. Write these attributes straight into MiniJinja templates, or add them with a component's .attribute(...).
Every attribute must be inside a render object that allows the matching action.
Attributes
| Attribute | Value format | Permission | Effect |
|---|---|---|---|
data-state-bind | key | state:write | Field ↔ state, both ways |
data-state-text | [!]key | state:read | State → textContent |
data-state-show | [!]key | state:read | State → hidden |
data-state-class | [!]key:className | state:read | State → classList.toggle |
data-state-is | key<operator>value | state:read | Comparison result → hidden |
data-state-is-class | key<operator>value:className[; …] | state:read | Comparison result → class |
data-state-set | key=value | state:write | Writes on click |
data-state-toggle | key | state:write | Flips on click |
data-state-init | k=v k=v or JSON | state:write | One-time initial values |
Prefixes:
!: negate (text / show / class only).@: leave the namespace and use a global key; can be combined:!@theme.
Adding them from Rust
Button has two shortcuts, like PHP's stateSet() / stateToggle():
Button::new("Chart").state_set("tab", "chart"); // data-state-set="tab="chart""
Button::new("Expand").state_toggle("open"); // data-state-toggle="open"
Button::new("Open").opens_modal("editOpen"); // = state_set("editOpen", true)state_set JSON-encodes the value ("chart" with quotes), so strings and numbers read back with the right type. Keys must match ^@?[A-Za-z][A-Za-z0-9_.-]*$; an invalid key panics.
Use .attribute(name, value) for the other attributes, or attrs in templates:
Badge::new("0").attribute("data-state-text", "matchCount");{{ cg_button("List", attrs={"data-state-set": "tab=list", "data-state-is-class": "tab=list:is-current"}) }}Initial values: data-state-init
<section{{ calc_attrs }} data-state-init="display=0 memory=0 showTape=false">- Each value is tried with
JSON.parsefirst:0is a number,truea boolean,lista string. - It only writes keys that don't exist yet, so a re-render doesn't overwrite what the user changed.
- For values with spaces, use JSON:
data-state-init='{"title": "hello world"}'.
To bring in server values from Rust, build the JSON with serde and let the template escape it into the attribute; don't concatenate strings yourself:
let init = serde_json::json!({ "title": order.title, "qty": order.qty }).to_string();
view("order", data! { state_init => init }) // template: data-state-init="{{ state_init }}" (escaped automatically)Two-way fields: data-state-bind
<input name="name" data-state-bind="name" value="{{ customer.name }}">Priority: a value already in state > the HTML value > leave it alone.
- First render: the state is empty → it takes the
valuefrom the server. - Later re-renders: the state has a value → it's written back into the field, so unsaved edits stay.
- A focused field isn't written back, so the cursor doesn't jump while typing.
| Element | Type read |
|---|---|
| checkbox | boolean |
| radio | The checked one's value (bind the whole group to one key) |
| number / range | number (empty string → null) |
<select multiple> | string[] |
| Everything else | string |
Don't bind type="file"
It stores C:\fakepath\…. This starter has no upload component yet; send files through a normal multipart route.
Comparisons: data-state-is / data-state-is-class
Six operators: = != > >= < <=; the right-hand side can only be a literal.
<div data-state-is="step=2">Step two</div>
<li data-state-is-class="step=2:is-current; step>2:is-done">Payment</li>Chain rules with semicolons. 2 and "2" are equal; when both sides convert to numbers, they're compared as numbers.
No arithmetic
You can compare, not compute: no a+b, no step=max-1. To compute something, have the server return a value (Linker's data), or write your own JS.
Example: tabs
<section{{ report_attrs }} data-state-init="tab=list">
<nav>
<button type="button" data-state-set="tab=list" data-state-is-class="tab=list:is-current">List</button>
<button type="button" data-state-set="tab=chart" data-state-is-class="tab=chart:is-current">Chart</button>
</nav>
<div data-state-is="tab=list">…list…</div>
<div data-state-is="tab=chart">…chart…</div>
</section>.with_html("report_attrs", Html::raw(runtime::render_object("report", &[Allow::StateRead, Allow::StateWrite]).render(&[])))When scanning happens
| When | Root |
|---|---|
Page boot (site.js calls boot()) | document |
| After every fragment insert | The insertion point |
After dom.repeat inserts each row | That row |
The order is fixed: inline <script> → CGEvent.bind() → bindState() → renderBladeAuto().
- Each slot of each element is bound only once (
WeakMap), so repeated scans are harmless. - When nodes are replaced their subscriptions drop off automatically; callers don't do anything.
- HTML you insert yourself with
innerHTMLis not scanned; always go through render fragments ordom.repeat.