Skip to content

Fragments

A fragment is a slice of UI that resolves on its own — separate from the page that hosts it. Because it has its own endpoint, it can load lazily and be reloaded on its own, which makes it a good fit for expensive panels, deferred content, or UI an action wants to refresh.

The page ships only a placeholder. The fragment fetches its own schema from its endpoint when it mounts, and the same endpoint lets an action reload it later without touching the rest of the page:

 
Lazy load and independent reload

Extend FragmentDefinition and build its schema(), the same way a page does. The #[AsFragment] attribute registers it.

use Lattice\Lattice\Attributes\AsFragment;
use Lattice\Lattice\Core\Components\Text;
use Lattice\Lattice\Core\PageSchema;
use Lattice\Lattice\Fragments\FragmentDefinition;
#[AsFragment('app.two-factor-setup')]
class TwoFactorSetupFragment extends FragmentDefinition
{
public function schema(PageSchema $schema): PageSchema
{
return $schema->component(Text::make('Scan the QR code to finish setup.'));
}
}

Render it with Fragment::lazy(), passing the definition class. The page ships a placeholder and the fragment fetches its own schema from its endpoint when it mounts.

use Lattice\Lattice\Fragments\Components\Fragment;
Fragment::lazy(TwoFactorSetupFragment::class);

A fragment is addressed by its id, so an action can refresh it with the reloadComponent effect — re-running its schema() without touching the rest of the page:

return ActionResult::success()->reloadComponent('app.two-factor-setup');

Fragments honor authorization like any definition: an unauthorized fragment resolves to nothing.