Developer forum

Forum » Rapido » Modifying the main tag

Modifying the main tag

Lars Larsen
Lars Larsen
Reply

Hi

I want to add a custom class to the main tag. Therefore I have added this pice of code to Designs\Rapido\MasterBlocks\Custom__Blocks.cshtml

    BlocksPage customMasterBlocksPage = BlocksPage.GetBlockPage("Master");

    var newMasterMainBlock = new Block
    {
        Id = "MasterMain",
        Template = CustomRenderMasterMain(customMasterBlocksPage.GetBlockListById("MasterMain").OrderBy(item => item.SortId).ToList())
    };
    customMasterBlocksPage.ReplaceBlock(newMasterMainBlock);

    @helper CustomRenderMasterMain(List<Block> subBlocks)
    {
        <main class="myCustomClass site dw-mod">
            @RenderBlockList(subBlocks)
        </main>
    }

But I can't make it work. I get this error:

Error executing template "Designs/Rapido/_parsed/ContentPage.parsed.cshtml"
System.ArgumentException: The requested block ID can not be found
Parameter name: MasterHeader

Why doesn't it work? What can I do to make it work?


Replies

 
Nuno Aguiar Dynamicweb Employee
Nuno Aguiar
Reply

Hi Lars,

 

It seems like the MasterMain Id does not exist. Are you sure that:

  • There is not a typo / you got the right Id?
  • It has already been defined when you're requesting it?
  • It is in the right hierarchy of blocks?
  • Is this in the proper Custom__Blocks.cshtml file? (Context)

 

What does customMasterBlocksPage.GetBlocksStructure() return?

 

Best Regards,

Nuno Aguiar

 
Lars Larsen
Lars Larsen
Reply

Hi Nuno

customMasterBlocksPage.GetBlocksStructure() returns this:

  • Root SortId: 10
    • Head SortId: 10
      • HeadMetadata SortId: 10
      • HeadCss SortId: 20
      • HeadManifest SortId: 30
    • Body SortId: 20
      • Master SortId: 10
        • MasterTopSnippets SortId: 10
          • FacebookPixel SortId: 2
          • LoginModal SortId: 10
        • MasterMain SortId: 20
          • MasterHeader SortId: 10
            • MasterDesktopTools SortId: 10
              • MasterDesktopToolsText SortId: 10
              • MasterDesktopToolsNavigation SortId: 20
            • MasterDesktopExtra SortId: 10
              • MasterDesktopLogo SortId: 10
              • MasterSearchBar SortId: 20
              • MasterDesktopActionsMenu SortId: 30
                • MasterDesktopActionsMenuSignIn SortId: 20
                • MasterDesktopActionsMenuLanguageSelector SortId: 40
                • MasterDesktopActionsMenuOrderDraft SortId: 40
                • MasterDesktopActionsMenuMiniCart SortId: 60
                  • MiniCartTrigger SortId: 0
                  • MiniCartLayout SortId: 0
            • MasterDesktopNavigation SortId: 20
              • MasterDesktopMenu SortId: 10
          • MasterPageContent SortId: 20
        • MasterFooter SortId: 30
          • MasterFooterContent SortId: 10
            • MasterFooterColumnOne SortId: 10
            • MasterFooterColumnTwo SortId: 20
            • MasterFooterSocialLinks SortId: 50
            • MasterFooterPayments SortId: 60
            • MasterFooterCopyright SortId: 70
        • MasterReferences SortId: 40
          • MiniCartPageId SortId: 0
          • MasterScriptReferences SortId: 1
          • MasterJavascriptInitializers SortId: 100
        • MasterBottomSnippets SortId: 50
          • AddToCartNotificationScript SortId: 0
          • OrderlinesScriptTemplates SortId: 0
          • MiniCartCounterScriptTemplate SortId: 0
          • MasterSearchScriptTemplates SortId: 1
          • MasterMiniCartTemplates SortId: 1
            • MiniCartOrderLines SortId: 0
              • MiniCartOrderLinesList SortId: 20
            • MiniCartFooter SortId: 50
              • MiniCartSubTotal SortId: 30
              • MiniCartFees SortId: 40
              • MiniCartPoints SortId: 50
              • MiniCartTotal SortId: 60
              • MiniCartDisclaimer SortId: 70
              • MiniCartActions SortId: 80

When looping the list returned by:

customMasterBlocksPage.GetBlockListById("MasterMain").OrderBy(item => item.SortId).ToList()

I get these two id'es: "MasterHeader" and "MasterContentPage" as expected. So I don't think it's a typo.

I have also tried on a clean new Rapido where I inserted the code below into Designs\Rapido\MasterBlocks\Custom__Blocks.cshtml:

@inherits Dynamicweb.Rendering.RazorTemplateBase<Dynamicweb.Rendering.RazorTemplateModel<Dynamicweb.Rendering.Template>>
@using System
@using System.Web
@using System.Collections.Generic
@using Dynamicweb.Rapido.Blocks

@{
    BlocksPage customMasterBlocksPage = BlocksPage.GetBlockPage("Master");

    var newMasterMainBlock = new Block
    {
        Id = "MasterMain",
        Template = CustomRenderMasterMain(customMasterBlocksPage.GetBlockListById("MasterMain").OrderBy(item => item.SortId).ToList())
    };
    customMasterBlocksPage.ReplaceBlock(newMasterMainBlock);
}

@helper CustomRenderMasterMain(List<Block> subBlocks)
{
  <main class="myCustomClass site dw-mod">
    @RenderBlockList(subBlocks)
  </main>
}

And I get the exact same error.

 
Kim Søjborg Pedersen
Reply

Hi Lars

Can you try changing the Id like this?

var newMasterMainBlock = new Block
    {
        Id = "CustomMasterMain",
        Template = CustomRenderMasterMain(customMasterBlocksPage.GetBlockListById("MasterMain").OrderBy(item => item.SortId).ToList())
    };

Maybe "MasterMain" is already defined...

 
Lars Larsen
Lars Larsen
Reply

Hi Kim

If I change the id name from "MasterMain" to "CustomMasterMain" then I don't get the error. But the block "MasterMain" is not modified which must be because 

customMasterBlocksPage.ReplaceBlock(newMasterMainBlock);

does not replace the MasterMain block because now it's trying to replace a block with id "CustomMasterMain". It is exactly the block "MasterMain" I want to replace smiley

 
Lars Larsen
Lars Larsen
Reply

Bump

I need to add a class to the main or body tag in order to distinguish (on the client-side in the css) between two sites using the same Rapido design. Could I do something else than what I try above and which fails?

 

You must be logged in to post in the forum