Page and layout props
The current Inertia page (page, usePage), and the functions a page uses to hand props up to its layouts (setLayoutProps, resetLayoutProps).
page
The current page: its component name, props, URL, flash data and the rest of what the server sent. Every read is tracked, so templates and getters that use it update when Inertia swaps the page or a partial reload changes its props. It's read-only.
import { page } from 'inertia-ember';
<template>
{{#if page.props.auth.user}}
Signed in as
{{page.props.auth.user.name}}
{{/if}}
</template>Signature
const page: Page<SharedPageProps>;Fields
The ones you'll read most; the full shape is Page from @inertiajs/core.
| Name | Type | Description |
|---|---|---|
component | string | The page's name, as the server sent it (Users/Show). |
props | PageProps & SharedPageProps & { errors } | The page's props, the shared ones included. errors holds the validation errors. |
url | string | The page's URL, path and query. |
version | string | null | The asset version the server sent. |
flash | FlashData | Flash data for this visit, typed by flashDataType. |
deferredProps | Record<string, string[]> | undefined | The props the server will send later, by group. |
clearHistory, encryptHistory | boolean | undefined | The history flags the server set. |
Setting a field throws: [inertia-ember] The page is read-only. Use router.replace() to change it.
Examples
Read shared props anywhere
page works outside pages too: in a layout, a component deep in the tree, or a getter.
import Component from '@glimmer/component';
import { page } from 'inertia-ember';
export default class UserMenu extends Component {
get initials() {
return page.props.auth.user?.name.slice(0, 2).toUpperCase();
}
<template>
<span class='avatar'>{{this.initials}}</span>
</template>
}Highlight the current link
import { Link, page } from 'inertia-ember';
const isCurrent = (prefix: string) => page.url.startsWith(prefix);
<template>
<Link @href='/orders' class={{if (isCurrent '/orders') 'active'}}>Orders</Link>
</template>Show flash messages
The server flashes a toast; with flashDataType declared it's typed:
import { page } from 'inertia-ember';
<template>
{{#if page.flash.toast}}
<div role='status'>{{page.flash.toast}}</div>
{{/if}}
</template>usePage
The same page as page, with its props typed as your shared props plus the ones you name. It takes no owner and needs no cleanup.
import { usePage } from 'inertia-ember';
const { props } = usePage<{ invoice: { number: string } }>();
props.invoice.number; // string
props.auth.user; // from sharedPagePropsSignature
function usePage<TPageProps extends object = PageProps>(): Omit<Page<SharedPageProps>, 'props'> & {
props: PagePropsOf<TPageProps>;
};Returns
The page object, typed. props is PagePropsOf<TPageProps>: your props, the shared ones and errors, with no catch-all key, so a prop you didn't name is a type error.
Examples
Typed props in a component that isn't a page
import Component from '@glimmer/component';
import { usePage } from 'inertia-ember';
export default class InvoiceTotal extends Component {
get total() {
const { lines } = usePage<{ lines: { amount: number }[] }>().props;
return lines.reduce((sum, line) => sum + line.amount, 0);
}
<template>{{this.total}}</template>
}Call it where you read, as here in a getter, or keep the result: it's the same tracked object either way.
setLayoutProps
Passes props from a page up to its layouts. Layouts get them in @props, merged over the page's props. With an object they reach every layout; with a name first, only the layout of that name.
import { setLayoutProps } from 'inertia-ember';
setLayoutProps({ title: 'Inbox' });
setLayoutProps('sidebar', { collapsed: true });Signature
function setLayoutProps(props: Partial<LayoutProps>): void;
function setLayoutProps<K extends keyof NamedLayoutProps>(name: K, props: Partial<NamedLayoutProps[K]>): void;Arguments
| Name | Type | Description |
|---|---|---|
name | keyof NamedLayoutProps | Optional. The name of the layout to reach, from a page's named layouts ({ sidebar: SidebarLayout }). |
props | Partial<LayoutProps> or Partial<NamedLayoutProps[K]> | Merged into what was set before; setting the same values again does nothing. |
LayoutProps and NamedLayoutProps come from layoutProps and namedLayoutProps in your InertiaConfig (see Types); without them any object goes.
Layout props are cleared on every visit that doesn't preserve state, so each page sets its own. Layouts see the change on the next microtask, not synchronously.
In a layout, @props is built from the page's props, then the props given with the layout ([AppLayout, { title }]), then the shared layout props, then the ones set for its name: later ones win.
Examples
A page sets the layout's title
// layouts/AppLayout.gts
import Component from '@glimmer/component';
import type { LayoutSignature } from 'inertia-ember';
export default class AppLayout extends Component<LayoutSignature<{ title?: string }>> {
<template>
<header><h1>{{@props.title}}</h1></header>
<main>{{yield}}</main>
</template>
}// pages/Inbox.gts
import Component from '@glimmer/component';
import type Owner from '@ember/owner';
import { type PageSignature, setLayoutProps } from 'inertia-ember';
export default class Inbox extends Component<PageSignature<{ unread: number }>> {
constructor(owner: Owner, args: PageSignature<{ unread: number }>['Args']) {
super(owner, args);
setLayoutProps({ title: `Inbox (${args.props.unread})` });
}
<template>…</template>
}Reach one named layout
A page that exports named layouts can address each one:
// pages/Settings.gts
import AppLayout from '../layouts/AppLayout.gts';
import SidebarLayout from '../layouts/SidebarLayout.gts';
export const layout = { app: AppLayout, sidebar: SidebarLayout };setLayoutProps('sidebar', { collapsed: true }); // only SidebarLayout gets `@props.collapsed`The layout receives its name as @name.
Type layout props
declare module '@inertiajs/core' {
export interface InertiaConfig {
layoutProps: { title: string };
namedLayoutProps: { sidebar: { collapsed: boolean } };
}
}
setLayoutProps({ title: 'Inbox' });
setLayoutProps('sidebar', { collapsed: 'yes' }); // type error: collapsed is a booleanresetLayoutProps
Clears every prop setLayoutProps set, shared and named. Inertia already does this on each visit that doesn't preserve state; call it to clear them sooner.
Signature
function resetLayoutProps(): void;Examples
import { resetLayoutProps } from 'inertia-ember';
resetLayoutProps(); // the layouts go back to the page's propsRelated: Pages and layouts, TypeScript and Glint, Types, router, Hooks.