Saturday Sitecore Learning - Sublayouts

Sublayouts/Renderings

In the previous post, we had looked into Basic layout, which holds common level HTML tags, and placeholders for components. Now let’s look into what are components, and how to create them.

Components are called as Sublayouts in ASP.NET web-forms context, and Renderings in ASP.NET MVC context. They are independent of the page items, so that they can be placed on any number of pages, any number of times.

In Sitecore tree, Sublayouts and Renderings reside under Layout/Sublayouts, and Layout/Renderings nodes respectively.

Placeholders

Before creating components, let’s briefly look about Placeholders. Placeholders are identified by a key, and used for placing the components in certain position on the page. Components are added to the page, using the key. In run time, Sitecore uses this placeholder key, to render components on to the page.

To add a placeholder, open the previously created BasicLayout.aspx file, and add a placeholder key as below,

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<sc:placeholder key="center" runat="server" />
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="BasicLayout.aspx.cs"
Inherits="ScDemo.layouts.BasicLayout" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>HelloWorld Sitecore!!!</title>
</head>
<body>
<form id="form1" runat="server">
<div>
Welcome to Sitecore.
</div>
<div>
<sc:placeholder key="content" runat="server" />
</div>
</form>
</body>
</html>

How to create a Sublayout

Step 1:
Login to Sitecore, add a Demo Sublayout folder under Sublayouts, to better organize the components,


Step 2:
Add a Demo Sublayout under Demo folder,





Step 3:
Open VS solution, add Sublayouts folder, and add DemoSublayout user control.


Step 4:
Add some sample text, to the user control as below,

1
2
3
4
5
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="DemoSublayout.ascx.cs"
Inherits="ScDemo.layouts.DemoSublayout" %>
<div>
This is a sample sublayout
</div>

Step 5:
Build and publish the code, so that code moves to deploy location,

Step 6:
Again login to Sitecore, open the DemoSublayout item, and verify file path in Path field.

Step 7
Now goto Home item presentation details, and delete the existing components. Add the DemoSublayout component with content placeholder key.





Step 8
Publish Sitecore items, and load the home page,


Happy Learning :)

Share