6

I am trying to render components dynamically on a page based on keyword.

For example: Based on type of news I will render all the components related to it on a certain region.

Can any one suggest how to achieve it ? Is there any example code available in default DXA Web application so that i can refer it ?

I am using Web 8 with DXA 1.5

  • do you have any working example? how i can get component data as entity model? – Hussain M Nov 10 '19 at 13:00
8

The recommended way to achieve such dynamic, query-based page assembly in DXA is by creating a custom Controller.

Unfortunately, DXA doesn't support custom Region Controllers yet, but you can create a custom Entity Controller (your own Controller class derived from EntityController).

So, you would have to model the entire dynamic list of Entities (DCPs) as an Entity itself (note it is quite common to model lists as Entities in DXA). You can then create a CT for the list which specifies your custom Controller.

Your custom Controller should override the EnrichModel method. This gives the Controller the possibility to dynamically populate the View Model. It can use the CD Taxonomy API (as mentioned by Nickoli) to retrieve the Component IDs and then use ContentProvider.GetEntityModel to retrieve DXA Entity Models for those IDs.

Finally, you will have to create a View for your list Entity. That can be a very simple View which iterates over the Entity Models populated by your Controller and let them render themselves using @Html.DxaEntity.

There are several examples of this pattern in DXA:

1

What you need to do is use a combination of the Taxonomy API, see here: https://docs.sdl.com/LiveContent/content/en-US/SDL%20Web-v1/GUID-1F443B41-6998-49BB-9852-7E151D73DA16, and the DD4T/DXA API.

First, use the Taxonomy API to do your filtering/retrieval of the items. This returns a list of ComponentPresentation objects which have TCM IDs to the Component and Component Template. Next, feed these IDs into the DD4T/DXA functions to create your models. The class that you should look at is BrokerQuery.cs (here: https://github.com/sdl/dxa-web-application-dotnet/blob/master/Sdl.Web.Tridion/Query/BrokerQuery.cs), which provides some good examples.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.