How Page Modules Work in .NET 5

Page Modules in Agility CMS are the functional components that make up a page. Editors use these to compose what type of content is on each page and in what order they appear.

Developers define what page modules are available in the CMS and what fields they have. In your .NET site, each module defined in Agility CMS should correspond to 2 main components, your View and your ViewComponents.

Your ViewComponent, a .cs file, can be thought of as the "Backend" of the Page Module where most of the logic is happening. The View, a .cshtml file, is the front-end component.

Generally speaking, if you don't have any modules defined or editors don't add modules to pages, then there won't be anything to output for your pages.

Pages, Page Templates, Content Zones & Modules

Learn how pages are generated and rendered

What is in a Page Module?

A Page Module in Agility CMS has a name, description, and fields.

The Name is a representation of the module's intended functionality, such as Posts Listing or Rich Text Area.

It is recommended that each module gets a description, this helps users understand when and where to use this definition.

Fields represent the content that can be managed by editors within the module. These fields are then used in code (passed as props) to display the content the editor intended.

Learn how to How to Create a Page Module.

An Example

in the agilitycms-dotnet5-starterthe name of the Page Module is used to find a corresponding .cs and .cshtml file that matches the same name. If a match is found, that component is dynamically imported and rendered.

For example, if a module has a reference name of RichTextArea in the CMS, then while the page is being rendered, it will look for ./Views/PageModules/RichTextArea.cshtml and ./ViewComponents/PageModules/RichTextArea.cs.

Screen_Shot_2021-07-02_at_11.30.17_AM.png

Internally, the Page Module will be dynamically imported and pass all the field values for that module as props, or in this case, @Models.

RichTextArea.cshtml:

@model Agility.Models.RichTextArea

<div class="relative px-8">
    <div class="max-w-2xl mx-auto my-12 md:mt-18 lg:mt-20">
        <div class="prose max-w-full mx-auto">@Html.Raw(Model.TextBlob)</div>
    </div>
</div>

RichTextArea.cs:

using System.Threading.Tasks;
using Agility.NET5.FetchAPI.Helpers;
using Agility.NET5.FetchAPI.Models;
using Microsoft.AspNetCore.Mvc;

namespace Agility.NET5.Starter.ViewComponents.PageModules
{
    public class RichTextArea : ViewComponent
    {
        public Task<IViewComponentResult> InvokeAsync(ModuleModel moduleModel)
        {
            var module = DynamicHelpers.DeserializeTo<Agility.Models.RichTextArea>(moduleModel.Module);
            return Task.Run<IViewComponentResult>(() => View("/Views/PageModules/RichTextArea.cshtml", module));
        }
    }
}

How to Change the Fields

You can alter your fields at any time and the field values passed to your component will update automatically.

  Adding a New Page Module?

If you're adding a new Page Module, be sure to run the Agility.NET5.AgilityCLI to download the Page Module Model from your Agility CMS instance, and generates the class of the Page Module for you.

0 out of 0 found this helpful

Comments

0 comments

Please sign in to leave a comment.