Objects and allow lists
A render object declares "what this block of DOM is allowed to do", and its name is also the state namespace. PHP's @renderObject is runtime::render_object in Rust.
Syntax
use cg_component::runtime::{self, Allow};
let attrs = runtime::render_object("customerPanel", &[Allow::Render, Allow::StateRead, Allow::StateWrite]);It returns Attributes, which output:
data-render-blade-object="customerPanel" data-render-allow="render state:read state:write"Allow | Attribute value | Allows |
|---|---|---|
Render | render | data-cg-event="renderBlade", data-render-auto |
StateRead | state:read | data-state-text / -show / -class / -is / -is-class |
StateWrite | state:write | data-state-bind / -set / -toggle / -init, and Linker writing data into state |
The PHP version throws for an empty name or a misspelled action; in Rust the actions are an enum, so a typo doesn't compile. Give the name a meaningful, non-empty string yourself.
Putting it on the page
Three ways, depending on where you build the HTML:
// 1. For a template: turn it into a string and pass it with with_html
view("page", data! {}).with_html("panel_attrs", Html::raw(attrs.render(&[])));<section{{ panel_attrs }}>…</section>// 2. On a component's root element
Badge::new("Draft").attributes(runtime::render_object("summary", &[Allow::StateRead]));
// 3. On the same element as a Linker mount point
let attrs = mount.html() + &runtime::render_object("counter", &[Allow::StateRead, Allow::StateWrite]).render(&[]);Attributes::render(&[]) returns a string that starts with a space ( data-…="…"), so write <section{{ panel_attrs }}> in the template, without an extra space.
How it's decided
permit(element, action)
→ find the nearest [data-render-blade-object] upwards
├─ hit a ShadowRoot → jump to the host and keep looking
├─ reached document without finding one → refuse + console.warn
└─ found → is action in data-render-allow?
├─ no → refuse + console.warn (prints the actual list)
└─ yes → allowed; returns the namespace- The nearest ancestor decides; nothing is inherited. If the inner object lacks
state:write, it's refused even when the outer one has it. - It crosses shadow roots. Fragments in
isolatedmode are in shadow DOM; at the boundary it jumps to the host and keeps looking. - Refusal doesn't throw. It's always
console.warnand the action doesn't run, so one missing attribute doesn't break the whole page.
It isn't a security mechanism
Users can of course add attributes in DevTools. The value of render objects is:
- The handler is the truth: look at the Rust
render_object(...)to see what a block may do, without chasing JS. - It catches slips: a read-only block without
StateWriteshows a mistaken binding in the console.
The real checks are always on the server: fragment functions validate params, and Linker components validate arguments and permissions.
Least privilege
// A read-only summary card: StateRead only
runtime::render_object("summary", &[Allow::StateRead]);
// A filter panel: needs to write, and to trigger redraws
runtime::render_object("filters", &[Allow::Render, Allow::StateRead, Allow::StateWrite]);The same template twice
<section{{ calc_a }}>{% include "partials/calculator.html" %}</section>
<section{{ calc_b }}>{% include "partials/calculator.html" %}</section>let all = [Allow::Render, Allow::StateRead, Allow::StateWrite];
view("calc", data! {})
.with_html("calc_a", Html::raw(runtime::render_object("calcA", &all).render(&[])))
.with_html("calc_b", Html::raw(runtime::render_object("calcB", &all).render(&[])));Not a single key in the template changes: calcA:display and calcB:display are two independent values.