Sitecore – Dynamic Placeholders

Dynamic Placeholders come handy when you need repetition of data or placeholders. Lets consider you have one placeholder ‘main’ in the layout. Then to distinguish the page types you have one-col, two-col and three-col renderings. And then finally you have the components/renderings which will fit in the content – lets use the same ones which we created Controller rendering (List of Fruits) and View rendering (static text). Now we want the structure like below:

Main

– one column

  • Controller Rendering

– one column

  • View Rendering

Let’s check if the above scenario is possible using static placeholders.

  1. Go to Main layout in Visual Studio and comment the static binding.
    SRG257
  2. Publish Main.cshtml.
  3. Create new View – name it OneColumn.cshtml and place the code ‘@Sitecore().Placeholder(“section”)’ in it.
    SRG258
  4. Publish OneColumn.cshtml.
  5. Go to Content Editor and Navigate to the Renderings (/sitecore/layout/Renderings).
  6. Right Click >> Insert >> View rendering.
  7. Name it One column and Click OK. Add the path of the recently created View.
    SRG259
  8. Save the changes. Publish the rendering.
  9. Navigate to Main placeholder settings (/sitecore/layout/Placeholder Settings/main).
  10. Remove Controller rendering and View rendering from the Allowed components and Add One column.
    SRG260
  11. Create new placeholder settings with name ‘section’ and add Controller rendering and View rendering in it’s Allowed Controls.
    SRG261
  12. Publish both the placeholder settings.
  13. Navigate to Test Item. Click on Publish tab and Click on Experience Editor.
  14. Delete any renderings if any. So the experience editor will look like:
    SRG262
  15. Click on the center of the box >> Add here >> Click on One Column.
  16. Again click on the center of the box >> Add here >> Click on Controller rendering.
    SRG263
  17. Click on dropdown button next to tree structure which shows the hierarchies of the placeholders used.
    SRG264
  18. Now we have achieved the below structure.

Main

– one column

  • Controller Rendering
  1. To achieve the remaining, we will click on the main placeholder from the tree.
    SRG265
  2. Now again you will be able to see Add here buttons at the top and bottom of the box.
    SRG266
  3. Click on the below Add here button >> One Column.
  4. Add now you will see that we didn’t get option to select the View rendering and data just got duplicated.
    SRG267
  5. This is where Dynamic placeholders are very useful.
  6. Delete the second rendering and Save the changes.
  7. Open Main.cshtml in the Visual studio. Change the placeholder code to ‘@Sitecore().DynamicPlaceholder(“main”)’
    SRG268
  8. Open OneColumn.cshtml and change the code to ‘@Sitecore().DynamicPlaceholder(“section”)’
    SRG269
  9. Publish both the cshtmls.
  10. Reload the Experience Editor.
  11. Delete all the renderings. May be you can switch to Content editor.
  12. Go to The presentation details >> Controls. And remove all the controls.
    SRG270
  13. Click OK and publish the test item.
  14. Switch to experience editor. Click in the section.
    SRG271
  15. Click on Add here >> One Column.
  16. Again click in the middle >> Add here >> Controller Rendering.
    SRG272
  17. Click on Main >> Lower Add here button >> One Column.
    SRG273
  18. Now the data didn’t get duplicated as we used dynamic placeholders.
  19. You click on Add here and select the View rendering.
    SRG274
  20. Publish the item >> Home tab >> Publish button in the upper right corner.
  21. Browse http://learningsitecore/test-item
    SRG275
  22. This completes Dynamic placeholders. So in case you have requirements where you feel the renderings can be reused then we need to use Dynamic renderings. There are certain limitations of using dynamic placeholders that you can read here.

Thank you.. Keep Learning.. Keep Sitecoring 🙂

2 thoughts on “Sitecore – Dynamic Placeholders

  1. Pingback: Sitecore – Placeholders and placeholder settings | Sitecore Dairies

  2. Pingback: Sitecore .Net 10 Certification Notes – Dave Coding

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s