Skip to content

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 ​

rust
use cg_component::runtime::{self, Allow};

let attrs = runtime::render_object("customerPanel", &[Allow::Render, Allow::StateRead, Allow::StateWrite]);

It returns Attributes, which output:

html
data-render-blade-object="customerPanel" data-render-allow="render state:read state:write"
AllowAttribute valueAllows
Renderrenderdata-cg-event="renderBlade", data-render-auto
StateReadstate:readdata-state-text / -show / -class / -is / -is-class
StateWritestate:writedata-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:

rust
// 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(&[])));
html
<section{{ panel_attrs }}>…</section>
rust
// 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 isolated mode are in shadow DOM; at the boundary it jumps to the host and keeps looking.
  • Refusal doesn't throw. It's always console.warn and 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:

  1. The handler is the truth: look at the Rust render_object(...) to see what a block may do, without chasing JS.
  2. It catches slips: a read-only block without StateWrite shows 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 ​

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

html
<section{{ calc_a }}>{% include "partials/calculator.html" %}</section>
<section{{ calc_b }}>{% include "partials/calculator.html" %}</section>
rust
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.