Skip to content

Commit 3c83c36

Browse files
authored
Merge pull request #6853 from umbraco/cms/templateSections
Update documentation on Templates and Named Sections
2 parents 5e9b367 + b1a60b5 commit 3c83c36

18 files changed

+154
-202
lines changed

Diff for: 14/umbraco-cms/.gitbook.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -130,3 +130,4 @@ redirects:
130130
fundamentals/backoffice/property-editors/built-in-umbraco-property-editors/block-editor/label-property-configuration: reference/umbraco-flavored-markdown.md
131131
customizing/property-editors/build-a-block-editor: fundamentals/backoffice/property-editors/built-in-umbraco-property-editors/block-editor/README.md
132132
tutorials/creating-and-distributing-a-package: extending/packages/creating-a-package.md
133+
fundamentals/design/templates/named-sections: fundamentals/design/templates/README.md

Diff for: 14/umbraco-cms/SUMMARY.md

-1
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,6 @@
103103
* [Design](fundamentals/design/README.md)
104104
* [Templates](fundamentals/design/templates/README.md)
105105
* [Basic Razor Syntax](fundamentals/design/templates/basic-razor-syntax.md)
106-
* [Named Sections](fundamentals/design/templates/named-sections.md)
107106
* [Razor Cheatsheet](fundamentals/design/templates/razor-cheatsheet.md)
108107
* [Rendering Content](fundamentals/design/rendering-content.md)
109108
* [Rendering Media](fundamentals/design/rendering-media.md)

Diff for: 14/umbraco-cms/fundamentals/design/templates/README.md

+76-40
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 enable 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 a `cshtml` file 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 building an Umbraco website you can 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 don'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.
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
40+
41+
A Template can inherit content from a "Master Template". This is done by using the ASP.NET views Layout feature.
2842

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:
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.
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 referred 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 templates 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
@@ -72,17 +99,39 @@ When a page using the textpage template renders, the final HTML will be merged r
7299
</html>
73100
```
74101

75-
## Template Sections
102+
## Named Sections
103+
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 are placed.
76105

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.
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).
78107
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:
108+
The following steps will guide you through defining and using a Named Section:
109+
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+
![Define a named section by giving it a name](images/defined-named-section.png)
116+
117+
The following code will be added to your Template:
80118

81119
```csharp
82-
@RenderSection("SectionName")
120+
@section SectionName {
121+
122+
}
83123
```
84124

85-
For instance, if you want to be able to add HTML to your `<head>` tags write:
125+
5. Add your code between the curly brackets.
126+
6. Save the changes.
127+
7. Open the Master Template.
128+
8. Choose a spot for the section and set the cursor there.
129+
9. Select the **Sections** option.
130+
10. Choose **Render a named section**.
131+
11. Enter the name of the section you want to add.
132+
12. Click **Submit**.
133+
134+
For instance, if you want to be able to add HTML to your `<head>` tags, you would add the tag there:
86135

87136
```csharp
88137
@inherits Umbraco.Web.Mvc.UmbracoViewPage
@@ -93,39 +142,32 @@ For instance, if you want to be able to add HTML to your `<head>` tags write:
93142
<html>
94143
<head>
95144
<title>Title</title>
96-
@RenderSection("Head")
145+
@RenderSection("SectionName", false)
97146
</head>
98147

99148
<body>
100149
</body>
101150
</html>
102151
```
103152

104-
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.
153+
You can decide whether a section should be mandatory or not. Making a section mandatory means that any template using the Master Template is required to have the section defined.
105154

106-
```csharp
107-
@RenderSection("Head", true)
108-
```
155+
{% hint style="info" %}
156+
Keep in mind that whenever a mandatory named section is missing, it will result in errors on your website.
157+
{% endhint %}
109158

110-
![Create partial](../../../../../10/umbraco-cms/fundamentals/design/templates/images/render-named-sections-v10.png)
159+
To make the section mandatory, you have two options:
111160

112-
On your child page template call `@section Head {}` and then type your markup that will be pushed into the Master Template:
161+
* Add `true` to the code tag: `@RenderSection("SectionName", true)`.
162+
* Check the **Section is mandatory** field when using the **Sections** dialog in the backoffice.
113163

114-
```csharp
115-
@section Head {
116-
<style>
117-
body {
118-
background: #ff0000;
119-
}
120-
</style>
121-
}
122-
```
164+
![Create partial](images/render-named-section-mandatory.png)
123165

124166
## Injecting Partial Views
125167

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

128-
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.
170+
Like templates, you can 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 a partial view from a snippet.
129171

130172
![Create partial](images/create-partial.png)
131173

@@ -150,9 +192,3 @@ The created partial view can now be injected into any template by using the `@Ht
150192
### Tutorials
151193

152194
* [Creating a basic website with Umbraco](../../../tutorials/creating-a-basic-website/)
153-
154-
### Umbraco Learning Base
155-
156-
{% embed url="https://www.youtube.com/playlist?ab_channel=UmbracoLearningBase&list=PLgX62vUaGZsFmzmm4iFKeL41CZ5YFw09z" %}
157-
Playlist: Templates in Umbraco
158-
{% endembed %}
Binary file not shown.
Binary file not shown.
Loading
Loading
Binary file not shown.

Diff for: 14/umbraco-cms/fundamentals/design/templates/named-sections.md

-60
This file was deleted.

Diff for: 15/umbraco-cms/.gitbook.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -112,3 +112,4 @@ redirects:
112112
fundamentals/backoffice/property-editors/built-in-umbraco-property-editors/block-editor/label-property-configuration: reference/umbraco-flavored-markdown.md
113113
customizing/property-editors/build-a-block-editor: fundamentals/backoffice/property-editors/built-in-umbraco-property-editors/block-editor/README.md
114114
tutorials/creating-and-distributing-a-package: extending/packages/creating-a-package.md
115+
fundamentals/design/templates/named-sections: fundamentals/design/templates/README.md

Diff for: 15/umbraco-cms/SUMMARY.md

-1
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,6 @@
112112
* [Design](fundamentals/design/README.md)
113113
* [Templates](fundamentals/design/templates/README.md)
114114
* [Basic Razor Syntax](fundamentals/design/templates/basic-razor-syntax.md)
115-
* [Named Sections](fundamentals/design/templates/named-sections.md)
116115
* [Razor Cheatsheet](fundamentals/design/templates/razor-cheatsheet.md)
117116
* [Rendering Content](fundamentals/design/rendering-content.md)
118117
* [Rendering Media](fundamentals/design/rendering-media.md)

0 commit comments

Comments
 (0)