Add new Block in the Controller and set this on the first position

Add new Block in the Controller and set this on the first position

September 19, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Add new Block in the Controller and set this on the first position

Setting a CMS Block as First in Content Structure

Are you struggling to position your new block at the beginning of your content structure? Let me guide you through this common challenge in Magento development.

Understanding Your Current Setup

You’ve already made significant progress by adding a new CMS block to your controller:

$this->loadLayout();
$cmsBlock = $this->getLayout()->createBlock('cms/block')->setBlockId('cms_block_fail');
$this->getLayout()->getBlock('content')->append($cmsBlock);
$this->renderLayout();

Great job! This code loads the layout, creates a new CMS block with ID cms_block_fail, appends it to the content block, and then renders the layout.

Analyzing Your Layout.xml

In your layout.xml file, you’ve correctly set up another content block within the “content” structure:

<reference name="content">
    <block type="contactus/form" name="contact_us" template="contactus/contactus.phtml"/>
</reference>

You want to insert the CMS block before this contact_us block.

The Challenge with Before/After Parameters

In your question, you mentioned using the before and after parameters in the layout.xml. Yes, you’re on the right track. The before="-" parameter would theoretically place the new block at the end of the “content” structure.

However, you found that this method isn’t working as expected. This is a common issue due to Magento’s complex layout system.

Manipulating Layout in Controller

Let’s explore how to achieve your goal using PHP within the controller:

  1. Loading and Setting the Block: You’ve already done this successfully.

  2. Manipulating the Content Block: To place the CMS block at the beginning, you can use the prepend method instead of append:

$this->getLayout()->getBlock('content')->prepend($cmsBlock);

The prepend method places the block at the start of the content structure.

  1. Rendering the Layout: Finally, don’t forget to render the layout:
$this->renderLayout();

Alternative Approach: Using XML Directly

If you prefer a more straightforward approach, consider updating your layout.xml file directly:

  1. First, remove the existing “contact” reference from your XML.

  2. Then, add a new CMS block with an ID and a before parameter set to "content":

<reference name="content">
    <block type="cms/block" name="cms_block_success" before="content"/>
</reference>

This will place the CMS block with the ID cms_block_success at the beginning of your content structure.

Conclusion and Best Practices

Remember, Magento’s layout system can be complex. Always ensure you’re working in a development environment when making significant changes to avoid impacting live sites.

If you find yourself frequently adjusting layouts, consider looking into more advanced techniques like custom layout processor classes or layout handles for better control and organization. For a comprehensive guide on Magento 2 theme development, check out our blog post at Laravel Company.

Don’t hesitate to experiment with both the PHP approach within your controller and the XML direct manipulation method. The key is understanding how each works so you can choose the right tool for the job.

Happy coding!

Themes Configuration Errors

Our network