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:
Open the Settings section inside the Umbraco backoffice and right-click the Templates folder. Choose Create. Enter a template name and click the Save button. You will now see the default template markup in the backoffice template editor.
![](https://docs.umbraco.com/~gitbook/image?url=https%3A%2F%2F3872888104-files.gitbook.io%2F%7E%2Ffiles%2Fv0%2Fb%2Fgitbook-x-prod.appspot.com%2Fo%2Fspaces%252FgEH4FChbCn7eDDqREvdE%252Fuploads%252Fgit-blob-47fa57416a06b698bcce0a4333acdcc14caea6fc%252Fcreate-template-v8.png%3Falt%3Dmedia%26token%3Dfd0e2790-284c-4566-b34e-9bc8149fb122&width=768&dpr=4&quality=100&sign=56118772&sv=1)
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.
![](https://docs.umbraco.com/~gitbook/image?url=https%3A%2F%2F3872888104-files.gitbook.io%2F%7E%2Ffiles%2Fv0%2Fb%2Fgitbook-x-prod.appspot.com%2Fo%2Fspaces%252FgEH4FChbCn7eDDqREvdE%252Fuploads%252Fgit-blob-a18945a38dafccfbedf76e875ca94f329cd9bc2b%252Fallow-template-v8.png%3Falt%3Dmedia%26token%3Da001b2ab-f75f-4ba9-b257-96c306d3bc2f&width=768&dpr=4&quality=100&sign=50b10809&sv=1)
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:
![](https://docs.umbraco.com/~gitbook/image?url=https%3A%2F%2F3872888104-files.gitbook.io%2F%7E%2Ffiles%2Fv0%2Fb%2Fgitbook-x-prod.appspot.com%2Fo%2Fspaces%252FgEH4FChbCn7eDDqREvdE%252Fuploads%252Fgit-blob-cdefa5b56bf169a30aebce93c32a3150e35a3b06%252Finherit-template-v8.png%3Falt%3Dmedia%26token%3D4947f2ac-1008-427f-bdf2-240e84a327c2&width=768&dpr=4&quality=100&sign=67a75ae&sv=1)
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.
![](https://docs.umbraco.com/~gitbook/image?url=https%3A%2F%2F3872888104-files.gitbook.io%2F%7E%2Ffiles%2Fv0%2Fb%2Fgitbook-x-prod.appspot.com%2Fo%2Fspaces%252FgEH4FChbCn7eDDqREvdE%252Fuploads%252Fgit-blob-6cbeb2a8072c86ca3ccc7f61c9ddcd3e26fbbbcc%252Frender-named-sections-v10.png%3Falt%3Dmedia%26token%3D3dc7719e-2e63-42ba-a62b-e549a83ca2f1&width=768&dpr=4&quality=100&sign=c86a21a8&sv=1)
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 right-clicking Partial Views and selecting Create. You can then either create an empty partial view or create a partial view from a snippet.
![](https://docs.umbraco.com/~gitbook/image?url=https%3A%2F%2F3872888104-files.gitbook.io%2F%7E%2Ffiles%2Fv0%2Fb%2Fgitbook-x-prod.appspot.com%2Fo%2Fspaces%252FgEH4FChbCn7eDDqREvdE%252Fuploads%252Fgit-blob-3f51d809f521897eb209a0116ec058a6267b1555%252Fcreate-partial-v8.png%3Falt%3Dmedia%26token%3D77555ea7-fd42-4307-b4aa-2491b4b8aabe&width=768&dpr=4&quality=100&sign=63dccfa2&sv=1)
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