Many of us have faced the issue of repeating content when trying to use same placeholder name, which at times can prove to be less than efficient. Sitecore 9 has filled this gap by introducing support for Dynamic Placeholders in the out-of-the-box installation, which is great news!
In the below example we will reproduce the issue when using a standard Sitecore placeholder:
We have created:
Important Note: None of the above placeholders will allow us to add a rendering inside if we don't create a placeholder setting item for them under /sitecore/layout/Placeholder Settings
.
To place two different Hero Images on the page in the 1 column sublayout, we would perform the following steps:
Now, if we want to reuse the 1 Column sublayout and add it inside the section below the column we've already added:
We will get the same component pre-populated which we added inside the first "1 Column," which is not what we want as we should be able to add any other component in it with a different datasource:
The presentation details look like this:
This restricts us from reusing the 1 Column sublayout because the placeholder used has the same name.
We can solve the above issue either by creating separate placeholders and restricting users to add limited components or by using Dynamic Placeholders:
We can update our sublayouts to use Dynamic Placeholders:
The above DynamicPlaceholder
method can be found in the SitecoreHelper
class which is part of Sitecore.Mvc
assembly.
It has following override parameters:
Experience editor will now allow us to add a different component:
After adding the second hero image, the page now has two 1 column sublayouts inside section
with different hero images:
If we look at the presentation details from the content editor:
Each Hero Image rendering has a different unique placeholder.
We hope that you found this article useful for your future Sitecore development. Enjoy using Sitecore 9!
Subscribe for the latest news and events from our talented team and community network.