# Layouts

Course: [Blazor](https://www.devtutorials.ai/learn/blazor) · Section: Blazor Components
Source: https://www.devtutorials.ai/learn/blazor/components/layouts

> Shared page frames in Blazor: LayoutComponentBase and @Body, the default layout, nested layouts, LayoutView, the NavMenu, sections with SectionOutlet and SectionContent, and the layout's error UI.

## Layouts Overview

- In **Project Structure**, you saw `MainLayout.razor` in the `Components/Layout` folder.
- A **layout** is the shared frame around your pages: the header, the menu and the footer.
- Each page shows its own content **inside** that frame.
- In this lesson, you will make layouts and choose which page uses which layout.

**Real-World Example:**

- Every page of Ravi's app shows the shop name at the top, a menu on the left and the opening hours at the bottom.
- Ravi puts these in one layout.
- The Menu, Order and Contact pages only contain their own content.

<figure className="blz-fig">
<svg viewBox="0 0 620 230" role="img" aria-labelledby="fig-ly-frame">
  <title id="fig-ly-frame">Animation: a layout with a header, a side menu and a footer stays the same. In the middle, where @Body is, the page content changes from Menu to Order to Contact.</title>
  <rect className="box" x="110" y="10" width="400" height="210" rx="10" />
  <rect className="pur" x="110" y="10" width="400" height="40" rx="10" />
  <text className="t" x="310" y="36" textAnchor="middle" style={{ fontSize: 14, fontWeight: 700 }}>☕ Ravi's Tea Shop (layout)</text>
  <rect x="110" y="50" width="100" height="130" style={{ fill: "#3a0647" }} />
  <text x="160" y="80" textAnchor="middle" style={{ fill: "#fff", fontFamily: "var(--sans)", fontSize: 12 }}>Menu</text>
  <text x="160" y="105" textAnchor="middle" style={{ fill: "#fff", fontFamily: "var(--sans)", fontSize: 12 }}>Order</text>
  <text x="160" y="130" textAnchor="middle" style={{ fill: "#fff", fontFamily: "var(--sans)", fontSize: 12 }}>Contact</text>
  <rect className="grn" x="225" y="62" width="270" height="106" rx="8" style={{ strokeDasharray: "5 4", strokeWidth: 2 }} />
  <text className="t m" x="360" y="84" textAnchor="middle" style={{ fontSize: 12 }}>@Body</text>
  <text className="t" x="360" y="125" textAnchor="middle" style={{ fontSize: 16, fontWeight: 700 }}>
    <tspan>Menu page<animate attributeName="opacity" values="1;0;0" dur="6s" repeatCount="indefinite" calcMode="discrete" /></tspan>
  </text>
  <text className="t" x="360" y="125" textAnchor="middle" style={{ fontSize: 16, fontWeight: 700 }} opacity="0">Order page<animate attributeName="opacity" values="0;1;0" dur="6s" repeatCount="indefinite" calcMode="discrete" /></text>
  <text className="t" x="360" y="125" textAnchor="middle" style={{ fontSize: 16, fontWeight: 700 }} opacity="0">Contact page<animate attributeName="opacity" values="0;0;1" dur="6s" repeatCount="indefinite" calcMode="discrete" /></text>
  <rect className="pur" x="110" y="180" width="400" height="40" rx="10" />
  <text className="t" x="310" y="205" textAnchor="middle" style={{ fontSize: 12 }}>Open 7 AM to 9 PM (layout)</text>
</svg>
<figcaption>The layout stays. Only the page inside @Body changes.</figcaption>
</figure>

## Layout Components (LayoutComponentBase)

First, one word you need:

- **Inherit** = take everything from another class, so you do not write it again.

Now the layout:

- A layout is a normal `.razor` file with one extra line at the top: `@inherits LayoutComponentBase`.
- This line turns the component into a layout.

## Rendering Page Content (@Body)

- `@Body` marks **where** the page goes inside the layout.
- Without `@Body`, you see only the frame. The page is missing.

In a real app, the Router puts each page inside its layout. In the code boxes, `LayoutView` does that job. You learn it later in this lesson.

In the below example, `ShopLayout.razor` has a header and a footer, with `@Body` between them. `Home.razor` shows "Today's special" inside it. Press **▶ Run**.

      <h2>Today's special</h2>
      <p>Masala Tea with fresh ginger.</p>
`,
    "Components/Layout/ShopLayout.razor": `@inherits LayoutComponentBase

<header style="background: #6d28d9; color: white; padding: 8px;">
      ☕ Ravi's Tea Shop
</header>

<main style="padding: 8px;">
      @Body
</main>

<footer style="border-top: 1px solid #ccc; padding: 8px; color: gray;">
      Open 7 AM to 9 PM
</footer>
`,
  }}
/>

- Delete `@Body` in `ShopLayout.razor` and press **▶ Run**. The header and footer stay, but the page content disappears.

## Default Layout (RouteView DefaultLayout)

- The app's main layout is set **once**, in `Routes.razor`.
- `DefaultLayout="typeof(Layout.MainLayout)"` means every page uses `MainLayout`.

`}
/>

## Nested Layouts

- A nested layout is a layout that is placed inside another layout.
- The inner layout uses `@layout` to connect itself to the outer layout.
- When a page uses the inner layout, it automatically gets both layouts.

In this example, `MenuLayout.razor` adds a sidebar and uses `MainShell` as its parent layout. So, a page that uses `MenuLayout` gets both the sidebar from `MenuLayout` and the shared page structure from `MainShell`. This is called a nested layout.

      <p>Masala Tea: ₹20</p>
      <p>Ginger Tea: ₹25</p>
`,
    "Components/Layout/MenuLayout.razor": `@inherits LayoutComponentBase
@layout MainShell

<div style="border: 2px dashed #16a34a; padding: 8px;">
      <b>🍵 Menu section</b>
      @Body
</div>
`,
    "Components/Layout/MainShell.razor": `@inherits LayoutComponentBase

<header style="background: #6d28d9; color: white; padding: 8px;">☕ Ravi's Tea Shop</header>
<div style="padding: 8px;">@Body</div>
<footer style="color: gray; padding: 8px;">© Ravi's Tea Shop</footer>
`,
  }}
/>

## LayoutView Component

- `LayoutView` displays content inside a layout without using the Router.
- It is useful for showing content that is not a page, such as a "Not Found" message.

In the example below, `Home.razor` uses `LayoutView` to display content inside either `ShopLayout` or `KioskLayout`, and the buttons switch between the two layouts.

<button @onclick="() => layout = typeof(KioskLayout)">Kiosk layout</button>

      <p>Masala Tea is ready to order.</p>

@code {
      private Type layout = typeof(ShopLayout);
}
`,
    "Components/Layout/ShopLayout.razor": `@inherits LayoutComponentBase

<div style="display: flex; gap: 8px;">
      <nav style="background: #3a0647; color: white; padding: 8px;">Menu<br />Order<br />Contact</nav>
      <main>@Body</main>
</div>
`,
    "Components/Layout/KioskLayout.razor": `@inherits LayoutComponentBase

<div style="font-size: 1.4em; text-align: center; border: 4px solid #ea580c; padding: 12px;">
      @Body
</div>
`,
  }}
/>

`LayoutView` ignores any `@layout` line inside the content. For normal pages, use `@layout`.

## Navigation Menu in a Layout (NavMenu)

- In **Built-in Components**, you made menu links with `NavLink`.
- The template keeps those links in `NavMenu.razor` and shows `<NavMenu />` in `MainLayout`.
- So the menu appears on every page.

      <div class="sidebar">
      </div>

      <main>
          <div class="top-row px-4">
              <a href="https://learn.microsoft.com/aspnet/core/" target="_blank">About</a>
          </div>

          <article class="content px-4">
              @Body
          </article>
      </main>
</div>

<div id="blazor-error-ui" data-nosnippet>
      An unhandled error has occurred.
      <a href="." class="reload">Reload</a>
      <span class="dismiss">🗙</span>
</div>
`}
/>

## Sections

- A section allows a page to place content in a specific area of a layout, such as a header or toolbar.
- The layout defines where the content should appear using `SectionOutlet`.
- The page provides the content for that location using `SectionContent`.

In the example below, `ShopLayout.razor` creates a location named "top-bar" using `SectionOutlet`. `Home.razor` fills that location with an Order now button using `SectionContent`, so the button appears in the header of the layout.

          <button>🛒 Order now</button>

      <p>Masala Tea: ₹20</p>
`,
    "Components/Layout/ShopLayout.razor": `@inherits LayoutComponentBase
@using Microsoft.AspNetCore.Components.Sections

<header style="background: #6d28d9; color: white; padding: 8px; display: flex; justify-content: space-between;">
      <span>☕ Ravi's Tea Shop</span>
</header>

<main style="padding: 8px;">@Body</main>
`,
  }}
/>

## Layout Error UI

- `MainLayout` has a hidden yellow bar. Blazor shows it when an error is not caught.
- In **Built-in Components**, you used `ErrorBoundary`. Put it around `@Body` so one broken page does not break the whole frame.

In the below example, `SafeLayout.razor` wraps `@Body` in an `ErrorBoundary`. Click the button: the page shows an error message, but the header still works.

`,
    "Components/Layout/SafeLayout.razor": `@inherits LayoutComponentBase

<header style="background: #6d28d9; color: white; padding: 8px;">☕ Ravi's Tea Shop (still works)</header>

          @Body
          <p style="background: lightyellow; padding: 8px;">This page had a problem. Please try another page.</p>
`,
    "Components/BrokenWidget.razor": `<button @onclick="Break">Break the page</button>

@code {
      private void Break()
      {
          throw new InvalidOperationException("Oops");
      }
}
`,
  }}
/>

In the template, the error bar's style lives in `MainLayout.razor.css` under `#blazor-error-ui`. Change it there to match your app's colours.

## Common mistakes

<div className="tscroll">

| Mistake | Wrong idea | Correct idea | What goes wrong |
|---|---|---|---|
| Forgetting `@Body` | The layout adds the page by itself | Put `@Body` where the page goes | Only the header and footer show |
| A layout without `@inherits LayoutComponentBase` | Any component can be a layout | Add `@inherits LayoutComponentBase` | An error: `Body` does not exist |
| Expecting `LayoutView` to read the page's `@layout` | It works like the Router | It uses only its `Layout` value | The wrong layout shows |
| Different names in `SectionOutlet` and `SectionContent` | Any name connects them | Use the same `SectionName` | The content never appears |

</div>

## Check Yourself

## Exercises

## What to Remember

- A layout inherits `LayoutComponentBase` and shows the page at `@Body`.
- `DefaultLayout` in `Routes.razor` sets the main layout for every page.
- A layout with its own `@layout` is nested.
- `LayoutView` shows content in a layout without the Router.
- Put `NavMenu` in the layout, so every page has the menu.
- `SectionOutlet` and `SectionContent` let a page fill a spot in the layout.
- `#blazor-error-ui` shows uncaught errors. Wrap `@Body` in `ErrorBoundary` to keep the frame working.

Next: **Forms & Events**
