Saturday, November 24, 2018

Renderings/Components in sitecore with an example


 Hi All, welcome back. In this post i will explain about two types of renderings in sitecore.
Lets Move,


What is Renderings /Components?

The name itself Renderings commonly Used to render a page.

The two most common Components in sitecore are

                1.View Rendering
                2.Controller Rendering 


View rendering:

  • This rendering type is a Razor view.
  • Use this when you want to render item content that does not require any significant business or presentation logic(Just display information).
  • Sitecore provides a default controller and model (RenderingModel) for the view. You can customize the model by changing the mvc.getModel pipeline.

Controller Rendering:

  • Use this to render content that relies on external data and/or requires significant business or presentation logic.
  • For this rendering type, you need to supply controller, model and view. 


View Rendering Vs Controller Rendering


View rendering
Controller Rendering

1. Standard Razor View file

2. Receives a default model

3. Should have little or no code

4. Only recommanded for simple scenarios

1.Follows MVC pattern more closely

2.Invokes an action in a controller

3.Suitable for complex renderings.


View Rendering example

In order to use Rendering we need know two things

              1. Rendering definition Item (Created in Content editor/Sitecore Explorer)
              2. Code file(.cshtml)

Step 1:  Create a layout code file (ex.Layout.cshtml)in visual studio and Create a layout in Sitecore Editor and put path that maps the code file that you are created.

Step 2:  Create a another layout code file(view)(ex.sample.cshtml)

Step 3:  Create a View Rendering definition item in content editor




  
Path:      /Sitecore/Layout/Renderings/<your Rendering folder>/your rendering item.
Provide path that maps sample.cshtml file(ex./Views/Layouts/sample.cshtml) in path field.

Step 4: Before rendering this we need to know some terms

 Static Binding and Dynamic binding.

1.Static binding means adding a rendering to a page in a raw way,.The rendering is added directly in the code e.g. in an mvc view:
                     @Html.Sitecore().ViewRendering(Id/Path)

2.Dynamic binding means adding components to the placeholder.Simply,The renderings is attached via the presentation details of an item
                     @Html.Sitecore().Placeholder(placeholderkey)

Step 5: I hope now you clear about binding methods, next create a placeholder definition item in the content editor

Path: Sitecore/Layout/PlaceholderSettings/<your placeholder settings folder>/<placeholder>

In the allowed controls section add your view rendering definition item.



Step 6: In your layout file(layout.cshtml) render your component via placeholder.



Step 7:  Create an item in the content editor
              In Sitecore Editor , Click your Item >Go to Presentation Tab >Final Layout >Assign Layout & add  rendering to placeholder.



Step 8:  All now is ready, run your item and check if it’s works.

Controller Rendering example 


Step 1:  Create a layout code file (ex.Layout.cshtml)in visual studio and Create a layout in Sitecore Editor and put path that maps the code file that you are created.

Step 2:  Create a another layout code file(view)(ex.sample.cshtml).

Step 3: Add a controller (name: example) in your solution and also create a Controller Rendering definition item in content editor and put controller name and action method.






Step 4: Add your business logic in your controller and return some results to sample.cshtml view.

Step 5: Next create a placeholder defintion item in the content editor

Path: Sitecore/Layout/PlaceholderSettings/<your placeholder settings folder>/<placeholder>

In the allowed controls section, add your controller rendering definition item.

Step 6: In your layout file(layout.cshtml) render your component via placeholder.

Step 7 Next,Create a item in the content editor
              In Sitecore Editor , Click your Item >Go to Presentation Tab >Final Layout >Assign Layout & add  rendering to placeholder.

Step 8: All now is ready, run your item and check if it’s works.


Don't forget to share with your friends and also comment me if anything wrong. Happy Coding :)



No comments:

Post a Comment

Check the users Logged into the sitecore backend

Hi, Today I'm going to discuss about " how to check the concurrent users logged into the sitecore backend ? ". For this ...