Skip to content

Commit 9e51f2a

Browse files
committed
Starting to rewrite the TEmplates article
1 parent 32e28a7 commit 9e51f2a

File tree

1 file changed

+60
-24
lines changed
  • 14/umbraco-cms/fundamentals/design/templates

1 file changed

+60
-24
lines changed

14/umbraco-cms/fundamentals/design/templates/README.md

+60-24
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,46 @@
11
---
2-
description: Templating in Umbraco including inheriting from master template
2+
description: Templating in Umbraco builds on the concept of Razor Views from ASP.NET MVC.
33
---
44

55
# Templates
66

7-
_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._
7+
Templates are the files that control the look and feel of the frontend of your Umbraco websites. Building on the concept of MVC Razor Views, template files enables you to structure your websites using HTML, CSS and JavaScript. When tied to a Document Type, templates are used to render your Umbraco content on the frontend.
88

9-
## Creating your first Template
9+
You can manage and work with your templates directly from the Settings section in the Umbraco backoffice. Each Template can also be found as `cshtml` files in the `Views` folder in your project directory.
1010

11-
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:
11+
## Creating Templates
12+
13+
When you are building an Umbraco website you can choose to automatically generate Templates when you create a new Document Type. This will ensure the connection between the two and you can jump straight from defining the content to structuring it.
14+
15+
Choose the option called **[Document Type with Template](../../data/defining-content/README.md)** when you create a new Document Type to automatically create a Template as well.
16+
17+
In some cases, you might want to create independent Templates that doesn't have a direct connection to a Document Type. You can follow the steps below to create a new blank Template:
1218

1319
1. Go to the **Settings** section inside the Umbraco backoffice.
1420
2. Click **...** next to the **Templates** folder.
1521
3. Choose **Create**.
1622
4. Enter a template name.
17-
5. Click the **Save** button. You will now see the default template markup in the backoffice template editor.
23+
5. Click the **Save** button.
24+
25+
You will now see the default template markup in the backoffice template editor.
1826

1927
![Created template](images/create-template.png)
2028

2129
## Allowing a Template on a Document Type
2230

23-
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.
31+
To use a Template on your content, you must first allow it on the content Document Type type.
32+
33+
1. Open the Document Type you want to use the template.
34+
2. Open the Templates Workspace View.
35+
3. Select your Template under the **Allowed Templates** section.
2436

2537
![Allowing template](images/allow-template.png)
2638

27-
## Inheriting a Master Template
39+
## Inheriting a Template
2840

29-
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:
41+
A Template can inherit content from what is called a "Master Template". This is done by using the ASP.NET views Layout feature.
42+
43+
Let's say you have a Template called **MainView**, containing the following HTML:
3044

3145
```csharp
3246
@inherits Umbraco.Cms.Web.Common.Views.UmbracoViewPage
@@ -42,21 +56,34 @@ A template can inherit content from a master template by using the ASP.NET views
4256
</html>
4357
```
4458

45-
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**:
59+
This file contains the structural HTML tags for your website but not much else.
60+
61+
By using the Template as the "Master Template" on your other Templates, you can ensure that they inherit the same structural HTML.
62+
63+
Follow these steps to use a Template file as a Master Template:
64+
65+
1. Open one of your Template files.
66+
2. Select the **Master template: No master** button above the editor.
67+
3. Select the Template that should be defined as the Master Template.
68+
4. Click **Choose**.
4669

4770
![Inherit template](images/inherit-template.png)
4871

49-
This changes the `Layout`value in the template markup, so **textpage** looks like this:
72+
Alternatively, you can manually change the value of the `Layout` variable in the Template using the name of the Template file.
73+
74+
The updated markup will look something like the snippet below and the Template is now refered to as a *Child Template*:
5075

5176
```csharp
5277
@inherits Umbraco.Web.Mvc.UmbracoViewPage
5378
@{
54-
Layout = "MasterView.cshtml";
79+
Layout = "MainView.cshtml";
5580
}
5681
<p>My content</p>
5782
```
5883

59-
When a page using the textpage template renders, the final HTML will be merged replacing the `@renderBody()` placeholder, and produce the following:
84+
When a page that uses a Template with a Master Template defined is rendered, the HTML of the two template is merged.
85+
86+
The code from the Template replaces the `@RenderBody()` tag in the Master Template. Following the examples above, the final HTML will look like the code in the snippet below:
6087

6188
```csharp
6289
@inherits Umbraco.Web.Mvc.UmbracoViewPage
@@ -74,9 +101,28 @@ When a page using the textpage template renders, the final HTML will be merged r
74101

75102
## Template Sections
76103

77-
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.
104+
Template Sections give you added flexibility when building your templates. Use the Template Section together with a Master Template setup, to decide where sections of content is placed.
105+
106+
If a Child Template needs to add code to the `<head>` tag a Section must be defined and then used in the Master Template. This is made possible by [Named Sections](https://www.youtube.com/watch?v=lrnJwglbGUA).
107+
108+
The following steps will guide you through defining and using a Named Section:
78109

79-
You can do this by using [named sections](https://www.youtube.com/watch?v=lrnJwglbGUA). Add named sections to your master template with the following code:
110+
1. Open your Template.
111+
2. Select the **Sections** option.
112+
3. Choose **Define a named section**.
113+
4. Give the section a name and click **Submit**.
114+
115+
On your child page template call `@section Head {}` and then type your markup that will be pushed into the Master Template:
116+
117+
```csharp
118+
@section Head {
119+
<style>
120+
body {
121+
background: #ff0000;
122+
}
123+
</style>
124+
}
125+
```
80126

81127
```csharp
82128
@RenderSection("SectionName")
@@ -109,17 +155,7 @@ By default, when rendering a named section, the section is not mandatory. To mak
109155

110156
![Create partial](../../../../../10/umbraco-cms/fundamentals/design/templates/images/render-named-sections-v10.png)
111157

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

114-
```csharp
115-
@section Head {
116-
<style>
117-
body {
118-
background: #ff0000;
119-
}
120-
</style>
121-
}
122-
```
123159

124160
## Injecting Partial Views
125161

0 commit comments

Comments
 (0)