Try our conversational search powered by Generative AI!

How to implement some dynamic content between blocks?

Vote:
 

In one of my CMS page, I have to add 2 blocks (say A and B) and some dynamic content between them. The content should come from the child pages (e.g. Main page is like product list page and child pages are product page and main page is supposed to list all 3 products in it).If there are 3 children, the page will contain 3 sections between A and B. How to implement this? Can I use block for such content as well? Or should I go with making changes in page view for such content between A and B?

#202190
Edited, Mar 19, 2019 10:28
Vote:
 

For that scenario I'd generally go for a block which lists the child products. You can then add that between your two other blocks. If you want to require that listing block on the page you could add some validation and/or create the block when the page is created and insert it into the contentarea.

If you didn't want to use a block, you could either use separate content areas (one for above the listing, 1 for below it) or you could render the contents of the content area yourself so you could inject the listing at whichever point you want. I'd use a block as described in the paragraph above though.

#202193
Edited, Mar 19, 2019 11:00
Vote:
 

How can the new block retrieve child pages? Should I list all pages in same block (as they are different sections)? And aren't the contentarea and block same? 

#202194
Mar 19, 2019 11:23
Vote:
 

How can the new block retrieve child pages? Should I list all pages in same block (as they are different sections)? And aren't the contentarea and block same? 

#202195
Mar 19, 2019 11:23
Vote:
 

How can the new block retrieve child pages? Should I list all pages in same block (as they are different sections)? And aren't the contentarea and block same? 

#202196
Mar 19, 2019 11:23
Vote:
 

Depending on how you want to approach this you could either create a single block which is aware of the page on which it's been placed or you can create a separate instance of the block for each usage of it in which case you would want to have a contentreference property to define the list root (the PageListBlock in the alloy sample site uses this approach). Using a separate instance for each page would work well if you are going to create the block programatically in the "for this page" folder and add it to the contentarea when the page is created.

If you want to create a single, location-aware block, you'll need something like the following in your block controller:

var pageRouteHelper = EPiServer.ServiceLocation.ServiceLocator.Current.GetInstance<IPageRouteHelper>();
var currentPage = pageRouteHelper.Page;

In this example I've used service locator for the sake of keeping it concise. In the real world you should use constructor injection in your controller to get the instance of IPageRouteHelper.

Once you've got access to the parent page either from a property or the PageRouteHelper you can get the child pages using IContentLoader (again, use constructor injection rather than servicelocator):

var contentLoader =  EPiServer.ServiceLocation.ServiceLocator.Current.GetInstance<IContentLoader>();
var childPages = contentLoader.GetChildren<PageData>(currentPage.ContentLink);

It's then up to you how you display them.

#202202
Mar 19, 2019 12:28
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.