Scenario 1:
Adding a control to a page using a view rendering:
-
In the Sitecore CMS, first of all lets select a location where we want to add a page – E.g. let’s go to “/sitecore/content/MyProject/Home/Training” and create a page “Exercise 1”
-
Now that the page is defined we need to create the required control(s) to be shown on this page, we will use “View rendering” to create a control.
-
Now let’s go to “/sitecore/layout/Renderings” and select a path where we can create our rendering. E.g. let’s use – “/sitecore/layout/Renderings//Content”
-
Add a “view rendering” named “Static page” with a path – “/Views/eCommerce/Renderings/Content/StaticPage.cshtml”
-
Now let’s go to Visual Studio and add a “view” named “Staticpage.cshtml” in project/path – “MyProject\Views\Renderings\Content” and add some content to the page.
-
Save and publish the view.
-
Now, go back to Sitecore CMS and add this view rendering “Static page” in the Presentation Details of Exercise 1 page.
-
See how to add this view rendering “Static page” in the Presentation Details of the Exercise 1 page:
-
The output is as follows:
Scenario 2:
Adding a control to a page using a view rendering with Model:
-
In the Sitecore CMS, first of all lets select a location where we want to add a page – E.g. let’s go to “/sitecore/content/MyProject/Home/Training” and create a page “Exercise 2”
-
Now that the page is defined we need to create the required control(s) to be shown on this page, we will use “View rendering” to create a control.
-
Now let’s go to “/sitecore/layout/Renderings” and select a path where we can create our rendering. E.g. let’s use – “/sitecore/layout/Renderings//Content”
-
Add a “view rendering” named “Static page With Model” with a path – “/Views/eCommerce/Renderings/Content/StaticPageWithModel.cshtml”
- Please see the change in this view rendering that we have also defined a path to a model – “/sitecore/layout/Models//Objects/Static Page With Model”
- The model is as below. Please see that we have define the ‘Model Type’ as ‘MyProject.Models.ObjectTemplates.StaticPageWithModel,MyProject.Models’
- Now as the type is defined, we need to create the actual template to represent that type. So here is the template named ‘Static Page With Model’ which has few fields/properties as shown in the builder.
- Now as you have created the required rendering, the model and the template, don’t forget to do these 3 things:
- Sync all these newly created Sitecore items to your project.
- Build and deploy the ‘MyProject.TDS.Models’ project.
-
Now let’s go to Visual Studio and add a “view” named “StaticpageWithModel.cshtml” in project/path – “MyProject\Views\Renderings\Content” and add model based content to the page. Please note that this model can be populated/hydrated from any controller e.g. an Angular Controller or a MVC controller or just by inline hydration as shown below.
-
Save and publish the view in Visual Studio.
- Now, go back to Sitecore CMS and add this view rendering “Static page with Model” in the Presentation Details of Exercise 2 page.
-
See how to add this view rendering “Static page with Model” in the Presentation Details of the Exercise 1 page:
-
The output is as follows:
Scenario 3:
Adding a control to a page using a view rendering with Controller:
-
In the Sitecore CMS, first of all lets select a location where we want to add a page – E.g. let’s go to “/sitecore/content/MyProject/Home/Training” and create a page “Exercise 3”
-
Now that the page is defined we need to create the required control(s) to be shown on this page, we will use “Controller rendering” to create a control.
-
Now let’s go to “/sitecore/layout/Renderings” and select a path where we can create our rendering. E.g. let’s use – “/sitecore/layout/Renderings//Content”
-
So let’s create a controller rendering as below.
- Now that we have created a placeholder control, we will use create required models, controllers and views.
- First of all lets go to “/sitecore/templates/MyProject/Object Templates/” to create a template which will define what data we need to show( something like a class). Let’s create a template named ‘StaticPageData’ derived from standard template and we will add fields to it. We added 3 firlds ‘HeaderLine’, ‘SubHeaderLine’ and ‘MessageLine’ to the template.
- Now we will add a content item derived from the ‘StaticPageData’ template ( something like object of a class). Let’s go to ‘/sitecore/content/MyProject/Reusable Content’ and follow step 1 to 8 as in the snapshot below to create ‘StaticPageData’ content and populate the 3 fields
- Now you are ready to sync your changes from Sitecore CMS into your Visual Studio models project.
- “Re-Generate” code for all items as shown above.
- Now add a model named ‘StaticPageModel’ to the “Models” folder of project “MyProject”.
- Then also add a controller named ‘StaticPageController’ to the “controllers” folder of project “MyProject”.
- Now let’s go to Visual Studio and add a “view” named “StaticpageWithController.cshtml” in project/path – “MyProject\Views\Renderings\Content” and add some content to the page.
- Now build the projects and deploy (better to build deploy whole solution and models)
- Now, go back to Sitecore CMS and add the controller rendering “Static page with controller” in the Presentation Details of Exercise 3 page.
-
See how to add this view rendering “Static page with controller” in the Presentation Details of the Exercise 3 page:
-
The output is as follows: