> ## Documentation Index
> Fetch the complete documentation index at: https://mintlify.com/nuxt/nuxt/llms.txt
> Use this file to discover all available pages before exploring further.

# setPageLayout

> Dynamically change the layout of a page.

<Warning>
  `setPageLayout` allows you to dynamically change the layout of a page. It relies on access to the Nuxt context and therefore can only be called within the Nuxt context.
</Warning>

## Type Signature

```ts theme={null}
function setPageLayout(layout: string, props?: Record<string, any>): void
```

## Parameters

<ParamField path="layout" type="string" required>
  The name of the layout to set for the current page.
</ParamField>

<ParamField path="props" type="Record<string, any>" optional>
  Props to pass to the layout component.
</ParamField>

## Examples

### Basic Usage

```ts theme={null}
// app/middleware/custom-layout.ts
export default defineNuxtRouteMiddleware((to) => {
  // Set the layout on the route you are navigating _to_
  setPageLayout('other')
})
```

### Passing Props to Layouts

You can pass props to the layout by providing an object as the second argument:

```ts theme={null}
// app/middleware/admin-layout.ts
export default defineNuxtRouteMiddleware((to) => {
  setPageLayout('admin', {
    sidebar: true,
    title: 'Dashboard',
  })
})
```

The layout can then receive these props:

```vue theme={null}
<!-- app/layouts/admin.vue -->
<script setup lang="ts">
const props = defineProps<{
  sidebar?: boolean
  title?: string
}>()
</script>

<template>
  <div>
    <aside v-if="sidebar">
      Sidebar
    </aside>
    <main>
      <h1>{{ title }}</h1>
      <slot />
    </main>
  </div>
</template>
```

## Notes

<Note>
  If you choose to set the layout dynamically on the server side, you *must* do so before the layout is rendered by Vue (that is, within a plugin or route middleware) to avoid a hydration mismatch.
</Note>

## See Also

* [Layouts Guide](/guide/directory-structure/layouts)
* [Route Middleware](/guide/directory-structure/middleware)
* `definePageMeta`
