Skip to content

Creating a theme

A theme is a class that implements ITheme, plus a layout component. It gives the designer its breakpoints and CSS classes, adds the site's stylesheets and scripts to every page, and decides where the template's areas render. This article builds a small one on Veva 7.26.

A theme is the code behind a site's look. Editors never edit it. They work with templates, and each template picks a theme. This article builds a small theme with one layout, on Veva 7.26 and .NET 10.

It assumes a site set up as in Getting started, and a module to register things in, as in Creating custom components.

How the pieces fit

  • Theme: a class that implements ITheme (namespace Veva.Core.Domain.Pages). It lists the breakpoints and CSS classes the designer offers, and adds stylesheets, scripts and the layout to each page request.
  • Layout: a Razor component that inherits LayoutComponentBase. It decides where the template's areas render.
  • Template: a record in the CMS, not code. It picks a theme and owns the components in the layout's areas, such as a header and a footer. Several templates can use one theme.
  • Page Area: a component an editor places in a template. The page's own content shows there.

Where the theme lives

Put the theme in a Razor class library (<Project Sdk="Microsoft.NET.Sdk.Razor">) that references Veva.Core.Domain and Veva.Core.Application.BlazorComponents, and reference that project from the web project. Stylesheets and scripts go in the library's wwwroot folder.

The theme class

ITheme asks for a Description, the MediaQueryBreakpoints and a ConfigurePageRequest method. CssClasses is optional.

using Veva.Core.Application.BlazorComponents.Extensions;
using Veva.Core.Domain.Pages;
using MyTheme.Layout;

namespace MyTheme;

public class SimpleTheme : ITheme
{
    // The name the theme is registered under. Templates store it.
    public const string ThemeName = "My Simple Theme";

    public string Description => "A small theme with a header and a footer";

    // Widest first. null means no upper limit.
    public IEnumerable<MediaQueryBreakpoint> MediaQueryBreakpoints =>
    [
        new(null, "Desktop", "desktop_windows"),
        new(1024, "Tablet", "tablet_mac"),
        new(568, "Phone", "stay_primary_portrait"),
    ];

    // Offered in the designer's class pickers. The first value is the scope.
    public IEnumerable<CssClass> CssClasses =>
    [
        new("a,button", "btn", "Button"),
        new("a,button", "btn-primary", "Primary button"),
        new("div,section", "container", "Centred container"),
    ];

    public void ConfigurePageRequest(PageRequest pageRequest)
    {
        // Required: without a layout the page has nothing to render in.
        pageRequest.Layout = typeof(MainLayout);

        pageRequest.AddViewPortMeta();

        // Files from this project's wwwroot folder.
        pageRequest.AddStyles<SimpleTheme>("/styles.css", preload: true);
        pageRequest.AddScript<SimpleTheme>("/theme.js", defer: true);

        // The scoped CSS of the components.
        pageRequest.AddComponentStyles();
    }
}

A few things to know about it:

  • Breakpoints. MediaQueryBreakpoint takes a width, a description and an icon name. A width means up to and including that many pixels. The viewport buttons under the page designer follow the order you list them in.
  • CSS classes. CssClass takes a scope, a class name and a description. The scope is a comma-separated list of element names: a class with the scope a,button is offered only where a component asks for a link or button class.
  • Files. AddStyles<T> and AddScript<T> take a path inside the wwwroot folder of the project that holds T, and add a version to the address, so browsers fetch a changed file after a deploy. The extension methods are in Veva.Core.Application.BlazorComponents.Extensions.

The layout

A theme needs at least one layout. Add Layout/MainLayout.razor:

@inherits LayoutComponentBase
@using Veva.Core.Application.BlazorComponents.Components

<header>
    <TemplateContent Name="header" />
</header>

<main>
    @Body
</main>

<footer>
    <TemplateContent Name="footer" />
</footer>

Each TemplateContent is a named area of the template. Editors fill it in the designer's template mode, and what they put there shows on every page that uses the template.

@Body renders the template's area named Content. Write it once, and do not add a TemplateContent named Content beside it.

Registering the theme

Register the theme by name in your module's ConfigureServices. AddNamed is in the namespace Veva.Core.Application.DependencyInjection.

using Veva.Core.Application.DependencyInjection;
using Veva.Core.Domain.Pages;

public void ConfigureServices(IServiceCollection services, IConfiguration configuration)
{
    services.AddNamed<ITheme, SimpleTheme>(SimpleTheme.ThemeName);
}

ITheme has no name property: the name you register it under is the theme's name. The template's theme picker shows it, and every template that uses the theme stores it. Do not change it later. A template whose theme name matches no registered theme has no layout, and its pages show "Layout not found".

Using it in a template

Run the site and sign in. Open a page in the designer and switch to template mode. In the template picker, add a template, give it a name and choose your theme. The header and footer areas from the layout are now there to fill.

The Content area needs a Page Area component (category Template). A page's own content shows only where the template has one. A template you create in the admin starts with a Page Area in Content, so you only add one yourself when you want the page content somewhere else, for example in one column of a Grid.

Publish the template. A page uses the template chosen in its page properties. A page without one uses the website's default template, which is set in the website properties.

Settings per template

A public property on the theme with [EditorBrowsable] becomes a setting that editors fill in per template, the same way as a component's properties in Adding user-configurable properties. Two templates of one theme can then differ, for example in a body class or a background colour. The layout reads the values from the theme: take the PageRequest as a cascading parameter named PageRequest, and cast its Theme property to your theme class.

Back to top

Type to search the docs, components and API.

↑↓ to move ↵ to open Esc to close Search by Veva