Blazor Custom Layouts

Most popular web applications have multiple different page layouts.  I can think of 2 different ones off the top of my head, Log In and Main.  You could add a message dialog and your standard CURD pages as well.

Blazor uses a simple implementation to allow you to define the layout of pages.  They provide a main layout as well as allowing you to create any additional layouts you need by inheriting from the Blazor layout component because in Blazor, everything is a component, even the layouts.

Default Layout

Here is the default MainLayout.razor

@inherits LayoutComponentBase

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

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

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

You can see that it inherits from LayoutComponentBase, Navigation section, an about link and then the main body “@Body”.

Custom Layout


@inherits LayoutComponentBase

<div class="main">
<header>
<h1>This is the header</h1>
</header>

<div class="content">
@Body
</div>

<footer>
This is the footer
</footer>
</div>

 This one well inherits from LayoutCompontBase, has a header section, a footer section and the “@Body” tag as well.

When you use the layout, it will place the content of your page in the @Body tag.

Here is the counter page using the default layout page:



Here is the same page but with our custom layout:



You can see that the counter part of the pages shown are the same and just the “master” part of the page changed.

Using Layouts


We have seen that we can set the layout in the component by using @layout.  There are a few other ways we can set the layout.

Start Up


You can set the default layout in the app.razor file:

<Router AppAssembly="@typeof(Program).Assembly">
    <Found Context="routeData">
        <RouteView RouteData="@routeData" DefaultLayout="@typeof(MainLayout)" />
    </Found>
    <NotFound>
        <LayoutView Layout="@typeof(MainLayout)">
            <p>Sorry, there's nothing at this address.</p>
        </LayoutView>
    </NotFound>
</Router>

In this file the default layout is set => DefaultLayout="@typeof(MainLayout)"
We are also setting the layout for when the user hits a page that is not defined will see a different page layout => Layout="@typeof(MainLayout)”

_Imports File


You can define additional layouts in place them in the _Imports.razor file and place the _Import.razor file in any folder and then that layout is available to all files at the level and lower.

Attributes
You can define a layout in the class file as well:
[Microsoft.AspNetCore.Components.LayoutAttribute(typeof(MainLayout))]
public class AdminUsers : Microsoft.AspNetCore.Components.ComponentBase
{
}


Summary


A layout is just a Blazor component, HTML, CSS and code.  You can create any layout page with any functionality to meet your needs.  We saw that you can define a page’s layout in the startup, the component, the class or in the _Imports file.



Comments

Popular posts from this blog

Yes, Blazor Server can scale!

Blazor new and improved Search Box

Blazor - Cool Blur Loading Page