Developer forum

Forum » Rapido » How do I replace a block which lives within a foreach?

How do I replace a block which lives within a foreach?

Peter Leleulya
Reply

I would like to replace Block detailsCategoryFields on page eCom/Product/Blocks/Fields.cshtml and its renderer RenderProductCategoryFields.

But when I make a replacement Block in eCom/Product/Blocks/Custom_Blocks.cshtml I don't have the LoopItemVariables of the surrounding foreach available ...
How am I supposed to make this work?

        foreach (LoopItem categoryGroup in GetLoop("ProductCategories"))
        {
                Block detailsCategoryFields = new Block() 
                {
                    Name = categoryFieldsLayout != "MainInformation" ? categoryGroup.GetString("Ecom:Product.Category.Name": "", 
                    Id = ToPascalCase(categoryGroup.GetString("Ecom:Product.Category.Name")),
                    SortId = 40,
                    Template = RenderProductSection(categoryFieldsLayoutcategoryFieldsViewcategoryGroup.GetString("Ecom:Product.Category.Name"), RenderProductCategoryFields(categoryGroup.GetLoop("ProductCategoryFields"), categoryFieldsView)),
                    Design = new Design
                    {
                        Size = "12",
                        RenderType = RenderType.Column,
                        HidePadding = true
                    }
                };
 
                productFieldsPage.Add(categoryFieldsLayoutdetailsCategoryFields);
            }

Replies

 
Adrian Ursu Dynamicweb Employee
Adrian Ursu
Reply

Hi Peter,

I guess it only works if you know the CategoryName and you create a Block specifically for that category.

And before that, you use Linq to filter the ProductCategories Loop.

At least that's how I would do it.

Adrian

 
Peter Leleulya
Reply

Thanks Adrian,

Indeed this works if you want to replace 1 instance of that Block within the loop ... 
But what if you want this change for all?

You would have to replace the entire loop, but doing so you have to change the template, wouldn't you?
I'm struggling with keeping templates as they are to prevent loss/crashed during future updates ...

 
Adrian Ursu Dynamicweb Employee
Adrian Ursu
Reply

Hi Peter,

You can replace all.

You can duplicate the existing logic in a CustomBlock.cshtml.

At the end of the loop, instead of productFieldPage.Add you can use productFieldPage.Replace

And that will replace the existing Rapido blocks with your custom ones.

There is some documentation here: https://doc.dynamicweb.com/rapido/development/blocks/example-7-replacing-a-block

I could replace an existing block in both MasterPage and ProductDetails page. It took a bit of poking around but it eventually worked.

Adrian

 
Adrian Ursu Dynamicweb Employee
Adrian Ursu
Reply

Hi Peter,

Not sure if you sorted it out already.

I have had the same issue and I came up with this code.

Maybe it will be useful for somebody else if you already figured it out:

if (categoryFieldsLayout != "hide")
{
foreach (LoopItem categoryGroup in GetLoop("ProductCategories"))
{
bool hasFields = categoryGroup.GetLoop("ProductCategoryFields").FirstOrDefault(cf => !string.IsNullOrEmpty(cf.GetString("Ecom:Product.CategoryField.Value"))) != null;
 
if (hasFields)
{
 
 
productFieldsPageCustom.RemoveBlockById(ToPascalCase(categoryGroup.GetString("Ecom:Product.Category.Name")));
}
}
}

Adrian

 

You must be logged in to post in the forum