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.
How a fragment loads
Section titled “How a fragment loads”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:
Defining a fragment
Section titled “Defining a fragment”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.')); }}Rendering a fragment
Section titled “Rendering a fragment”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);Reloading a fragment
Section titled “Reloading a fragment”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.