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:
Go to the Settings section inside the Umbraco backoffice.
Click ... next to the Templates folder.
Choose Create.
Enter a template name.
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:
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 Layout
value in the template markup, so textpage looks like this:
When a page using the textpage template renders, the final HTML will be merged replacing the @renderBody()
placeholder, and produce the following:
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:
For instance, if you want to be able to add HTML to your <head>
tags write:
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.
On your child page template call @section Head {}
and then type your markup that will be pushed into the Master Template:
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:
Related Articles
Tutorials
Umbraco Learning Base
Last updated