Developer forum

Forum » Templates » RenderItemList check if results exist from the paragraph template

RenderItemList check if results exist from the paragraph template

Igor Ivanov Dynamicweb Employee
Igor Ivanov
Reply

Developing a custom Swift_ArticleList template I noticed a problem that will come up again and again in the future.

The architecture is that the paragraph template is building the container for the paragraph, this could be a slider for example. It then calls the Dynamicweb.Rendering.RenderItemList method inside this template and passes the necessary settings.

 

This is what is rendered if the item publisher fails to locate items

Inside the ItemPublisher/List/List.cshtml template that is used I can see this line GetBoolean("ItemPublisher:Items.Any");

How do we check if ItemPublisher has returned any items before we render the paragraph, before calling RenderItemList() ?

This is important, because we want to be able to hide the paragraph or show our own message if no results are found

 

 

 

 


Replies

 
Nicolai Pedersen Dynamicweb Employee
Nicolai Pedersen
Reply

RenderItemList is basically just a function that you can call and store in a variable:

Something like this:

var myItems = RenderItemList(new { .. });
if(myItems.length>0){
@myItems
}

Length might be a bad option here as it can be a couple of characters long - but you get the point.

BR Nicolai

 
Igor Ivanov Dynamicweb Employee
Igor Ivanov
Reply

While this works

if(!myItems.ToString().Contains("No items found")){
@myItems
}

Surely there is a more elegant solution to this ?

 

You must be logged in to post in the forum