Skip to content

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 ​

AttributeValue formatPermissionEffect
data-state-bindkeystate:writeField ↔ state, both ways
data-state-text[!]keystate:readState → textContent
data-state-show[!]keystate:readState → hidden
data-state-class[!]key:classNamestate:readState → classList.toggle
data-state-iskey<operator>valuestate:readComparison result → hidden
data-state-is-classkey<operator>value:className[; …]state:readComparison result → class
data-state-setkey=valuestate:writeWrites on click
data-state-togglekeystate:writeFlips on click
data-state-initk=v k=v or JSONstate:writeOne-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():

rust
Button::new("Chart").state_set("tab", "chart");   // data-state-set="tab=&quot;chart&quot;"
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:

rust
Badge::new("0").attribute("data-state-text", "matchCount");
jinja
{{ cg_button("List", attrs={"data-state-set": "tab=list", "data-state-is-class": "tab=list:is-current"}) }}

Initial values: data-state-init ​

html
<section{{ calc_attrs }} data-state-init="display=0 memory=0 showTape=false">
  • Each value is tried with JSON.parse first: 0 is a number, true a boolean, list a 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:

rust
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 ​

html
<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 value from 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.
ElementType read
checkboxboolean
radioThe checked one's value (bind the whole group to one key)
number / rangenumber (empty string → null)
<select multiple>string[]
Everything elsestring

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.

html
<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 ​

html
<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>
rust
.with_html("report_attrs", Html::raw(runtime::render_object("report", &[Allow::StateRead, Allow::StateWrite]).render(&[])))

When scanning happens ​

WhenRoot
Page boot (site.js calls boot())document
After every fragment insertThe insertion point
After dom.repeat inserts each rowThat 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 innerHTML is not scanned; always go through render fragments or dom.repeat.