All the layouts reside at /sitecore/layout/Layouts
There are a few ways to create a new layout in Sitecore. The first way would be inside the instance. Simply navigate to the content editor, then to the Layout section. Expand this section and open the “Layouts” section inside. Create or navigate to a folder here and right-click the folder to add a new Layout item. You’ll need to provide a path to a corresponding razor view (.cshtml) file, which you should create in a project somewhere in your solution.
Let’s create one layout in Visual Studio and then create the same in Sitecore.
- In the Views folder, add a View – Main.cshtml
- Enter the HTML whatever you wish to – It should whole HTML (Head + Body).
- Publish the View.
- Note the path of the Main.chtml file – (/Views/Main.cshtml)
- Now go to Content Editor and navigate to Layouts (/sitecore/layout/Layouts).
- Right Click >> Insert >> Layout
- Name it – Main Layout and select the Locations as Layouts.
- Change the value in the path field to “/Views/Main.cshtml”
- Publish the layout.
- Now we are ready with the layout. Let’s assign it to one of the Item and check.
- Navigate to “/sitecore/content/Home/Test Item”.
- Click on Presentation Tab >> Details.
- Click Edit in Layout Details pop up.
- Select the Main Layout and Click OK.
- Again click OK and Publish the Test Item.
- Now browse http://learningsitecore/test-item
- This is how we create the layouts. It is recommended that we shouldn’t have more than one layout per device. Maximum 2 are allowed in case we are giving an option for end user to print the web page.
To create a new layout through Sitecore Rocks:
- Right-click a “Views” folder in your visual studio project (in the VS solution explorer, NOT the Sitecore Explorer).
- Choose Add -> New item…
- Select Sitecore -> MVC -> Sitecore View Layout.
- If the layout is created successfully and if your project is correctly connected to the Sitecore instance, Sitecore Rocks will ask you to create an item in the Sitecore Content Tree. Add the layout item in the Layouts folder (sitecore/layout/Layouts) in Master DB. This process will also automatically create a view file to link to the layout item.
- The layout is successfully created.
- Publish this view and Publish the layout item from the Sitecore. And you can use the layout.
If you do encounter an error when creating a layout, please refer to our “Error when creating a Layout – TemplateWizard” article.
Now that the layout is created, it can be used to display content. Layouts don’t have code-behinds with the ability to execute business logic, which is what renderings are for. However, any item in your content tree can be given a corresponding layout, and that layout can display that item’s information through the use of field renderers (like @Html.Sitecore().Fields(“title”)).
In this blog we understood, what is Layout and the ways of creating it. In the next blog we will understand how to create the Renderings and assigning it to the layout.
Thank you.. Keep Learning.. Keep Sitecoring.. 🙂