Share a layout across pages

Wrap every Bevel page in one layout component with @layout, a default layout option, or @layout in _Imports.razor.

On this page
Pages/SchedulePage.razorrazor
@layout MainLayout

<h1>Schedule</h1>

A layout is a Razor component that inherits LayoutComponentBase and renders @Body. Bevel wraps the page in it.

Write the layout

Your layout renders the whole HTML document, because Bevel writes a page exactly as it renders.

samples/Bevel.Conference/Components/MainLayout.razorrazor
@inherits LayoutComponentBase
@inject ISiteData Data
@inject ISiteLinks Links

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8"/>
  <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
  <meta name="description" content="@Data.Site.Description"/>
  <link rel="stylesheet" href="@Links.Asset("/css/site.css")"/>
  <SectionOutlet SectionName="head"/>
</head>
<body>
<header class="site-header">
  <a class="brand" href="@(Links.To<HomePage>())">Bevel Conf <span>2027</span></a>
  <nav>
    <a href="@(Links.To<SchedulePage>())">Schedule</a>
    <a href="@Links.Route("/calendar.ics")">Calendar</a>
  </nav>
</header>
<main>
  @Body
</main>
<footer class="site-footer">
  <p>@Data.Site.Title · @Data.Site.City · Made with <a href="https://github.com/khalidabuhakmeh/bevel">Bevel</a></p>
</footer>
</body>
</html>

Bevel adds <!DOCTYPE html> when the output starts with <html and has none. Set PrependDoctype = false to turn this off.

Choose how pages get the layout

Set a default once. Pages without @layout use it.

Program.cscsharp
builder.AddBevel(options => options.DefaultLayout = typeof(MainLayout));

== Per page Add @layout to the top of each page, as the samples do.

razor
@layout MainLayout

== Per folder Put @layout in a _Imports.razor file. Razor applies it to every component in that folder and below.

Pages/_Imports.razorrazor
@layout MainLayout

A page-level @layout wins over DefaultLayout. To render one page with no layout at all, add @attribute [NoLayout]. It beats DefaultLayout and any @layout, including one from _Imports.razor.

Put per-page content in the head

Pages cannot edit the layout's <head> directly. Use a section. The layout declares an outlet and each page fills it.

MainLayout.razorrazor
<head>
  <SectionOutlet SectionName="head"/>
</head>
Pages/SchedulePage.razorrazor
<SectionContent SectionName="head">
  <title>Schedule — @Data.Site.Title</title>
</SectionContent>

Add @using Microsoft.AspNetCore.Components.Sections to _Imports.razor.

Inject ISiteData and ISiteLinks in the layout, as any page does. Use Links.Asset for your CSS, and Links.To and Links.Route for navigation, so URLs follow your BevelOptions.PathBase.