Skip to content

Fetching Fragments ​

Server side: make the controller callable ​

Only controllers that use the CanRenderCallSafely trait can be called by renderBlade. The trait itself is the allow list.

php
use App\Traits\CanRenderCallSafely;

class CustomerController extends Controller
{
    use CanRenderCallSafely;

    public function renderCallSafely(array $params = [], string $method = 'components'): View
    {
        // params are untrusted input: pick the keys you need and validate them
        $mode = in_array($params['mode'] ?? '', ['results', 'detail'], true)
            ? $params['mode']
            : 'results';

        return match ($mode) {
            'detail' => view('partials.customer.detail', [
                'customer' => Customer::findOrFail((int) ($params['id'] ?? 0)),
            ]),
            default => view('partials.customer.results', [
                'customers' => Customer::search($params['keyword'] ?? '')->paginate(),
            ]),
        };
    }
}

params are always scalar strings

Validation of params.* only guarantees scalars of length ≤ 2048. Business rules (types, ranges, permissions) are yours to check inside renderCallSafely().

Front end: four ways to trigger ​

1. On click: @renderBlade ​

blade
<a href="{{ route('customer.view', $c) }}"
   @renderBlade(CustomerController::class, 'components', ['id' => $c->id, 'mode' => 'detail'], '#detail')>
    {{ $c->name }}
</a>

Outputs data-cg-event="renderBlade", data-render-name, data-render-method, data-render-target and data-param-*. The href stays, so it still navigates without JavaScript.

2. Automatically after load: @renderAuto ​

blade
<div id="detail" @renderAuto(CustomerController::class, 'components', ['id' => 3, 'mode' => 'detail'])></div>

The container must have an id. Good for heavy content that may never be seen (tabs, collapsed sections), or when you want a loading skeleton.

3. Rendered in place on the server: @renderInline ​

blade
<div id="detail">@renderInline(CustomerController::class, ['id' => 3, 'mode' => 'detail'])</div>

Goes through the same renderCallSafely(), but outputs during the page render, saving a round-trip. Note that it outputs content, so it goes between tags.

4. render.fragment returned from Linker ​

php
return $this->ok()->call('render.fragment', [
    'target' => '#customer-list-results',
    'name' => CustomerController::class,
    'params' => ['mode' => 'results', 'keyword' => $keyword],
]);

The most common use: business logic in Linker, re-rendering handled by renderBlade. See Responses & Actions.

Every trigger must be inside an @renderObject that grants render.

Insert methods ​

methodBehavior
componentsReplaces the target's innerHTML (default); supports fingerprints
isolatedPuts it in a shadow DOM with isolated styles
toastCreates a new node every time (notifications)
pageReplaces the whole body; document stays, so client state is kept

Fragment fingerprints: skip the HTML when nothing changed ​

In components mode the server returns an opaque fingerprint. Send it back on the next request; if the HTML is unchanged, the server replies with just { unchanged: true }. That saves bytes and one DOM replacement (focus and scroll position stay put).

  • It doesn't save server rendering: the HTML must be produced to know whether it changed.
  • The fingerprint is matched together with target|name|method|params, so changing a filter always gets new HTML.
  • To force data-state-init to run again (resetting a form, reopening a wizard), pass fresh: true to render.fragment, or force: true when calling renderBlade directly.
  • fresh does not clear state. To get back to an empty form, run state.clear first, then fresh.
  • Turn it off site-wide with APP_RENDER_BLADE_FINGERPRINT=false.

Fragments containing @linker are never unchanged

@linker issues a new instance uuid on every render. Put mount points in an outer block that isn't re-rendered, and re-render the inner part.

Calling from a CGEvent ​

When you need to compute something before sending, write a CGEvent under resources/js/Events/:

js
const s = state.scope('customerPanel');

renderBlade({
    name: 'App\\Http\\Controllers\\CustomerController',
    method: 'components',
    target: '#customer-panel',
    params: { keyword: s.get('keyword', ''), sort: s.get('sort', 'id') },
});

Pick values explicitly. Don't dump s.all(): that turns your front-end structure into an API contract. State never goes into FormData automatically.

Events ​

Eventdetail
cg:renderedFields returned by the server + { root } (no state)
cg:state{ key, value, previous }
cg:errorExceptions from rendering or subscribers