Templates

Templating in Umbraco including inheriting from master template

Templating in Umbraco builds on the concept of Razor Views from ASP.NET MVC - if you already know this, then you are ready to create your first template - if not, this is a quick and handy guide.

Creating your first Template

By default, all document types should have a template attached - but in case you need an alternative template or a new one, you can create one:

  1. Go to the Settings section inside the Umbraco backoffice.

  2. Click ... next to the Templates folder.

  3. Choose Create.

  4. Enter a template name.

  5. Click the Save button. You will now see the default template markup in the backoffice template editor.

Allowing a Template on a Document Type

To use a template on a document, you must first allow it on the content's type. Open the Document Type you want to use the template, go to the Templates tab and select the template under the Allowed Templates section.

Inheriting a Master Template

A template can inherit content from a master template by using the ASP.NET views Layout feature. Let's say we have a template called MasterView, containing the following HTML:

@inherits Umbraco.Cms.Web.Common.Views.UmbracoViewPage
@{
    Layout = null;
}
<!DOCTYPE html>
<html lang="en">
    <body>
        <h1>Hello world</h1>
        @RenderBody()
    </body>
</html>

We then create a new template called textpage and in the template editor, click on the Master Template button and set its master template to the template called MasterView:

This changes the Layoutvalue in the template markup, so textpage looks like this:

@inherits Umbraco.Web.Mvc.UmbracoViewPage
@{
    Layout = "MasterView.cshtml";
}
<p>My content</p>

When a page using the textpage template renders, the final HTML will be merged replacing the @renderBody() placeholder, and produce the following:

@inherits Umbraco.Web.Mvc.UmbracoViewPage
@{
    Layout = null;
}
<!DOCTYPE html>
<html lang="en">
    <body>
        <h1>Hello world</h1>
        <p>My content</p>
    </body>
</html>

Template Sections

What's good to know, is that you are not limited to a single section. Template sections allow child templates that inherit the master layout template to insert HTML code up into the main layout template. For example, a child template inserting code into the <head> tag of the master template.

You can do this by using named sections. Add named sections to your master template with the following code:

@RenderSection("SectionName")

For instance, if you want to be able to add HTML to your <head> tags write:

@inherits Umbraco.Web.Mvc.UmbracoViewPage
@{
    Layout = null;
}

<html>
    <head>
        <title>Title</title>
        @RenderSection("Head")
    </head>

    <body>
    </body>
</html>

By default, when rendering a named section, the section is not mandatory. To make the section mandatory, add true or enable Section is mandatory field in the Sections option.

@RenderSection("Head", true)

On your child page template call @section Head {} and then type your markup that will be pushed into the Master Template:

@section Head {
    <style>
        body {
            background: #ff0000;
        }
    </style>
}

Injecting Partial Views

Another way to reuse HTML is to use partial views - which are small reusable views that can be injected into another view.

Like templates, create a partial view, by clicking ... next to the Partial Views folder and selecting Create. You can then either create an empty partial view or create a partial view from a snippet.

The created partial view can now be injected into any template by using the @Html.Partial() method like so:

@inherits Umbraco.Web.Mvc.UmbracoViewPage
@{
    Layout = "MasterView.cshtml";
}

<h1>My new page</h1>
@Html.Partial("a-new-view")

Tutorials

Umbraco Learning Base

Playlist: Templates in Umbraco

Last updated