Get value from <block> in page.xml

Get value from <block> in page.xml

October 12, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Get value from <block> in page.xml

Passing Config Values to Magento Blocks

In Magento, you often need to use the same block multiple times on a single page but with different data loaded from the database. This requires passing configuration values from your XML layout to your blocks so they can decide what data to load. Let’s explore how to do this properly.

Transferring Config Values from Page.xml

You’re already on the right track by using a block and passing a config value (SimpleMenuInstanceRef) in your page.xml:

<block template="simplemenu/leftMenuTemplate.phtml" type="simplemenu/standard" name="leftMenu" as="leftMenu" translate="label">
    <label>Left menu header</label>
    <action method="setSimpleMenuInstanceRef"><SimpleMenuInstanceRef>4</SimpleMenuInstanceRef></action>
</block>

This works because you’re setting a local variable (SimpleMenuInstanceRef) in the block, which can then be accessed using $this->getSimpleMenuInstanceRef() in your template file.

Accessing Config Values in the Block

Now, you want to access this value in your block’s _construct method. The issue is that at the time of the constructor call, the block hasn’t been initialized yet, so it doesn’t have access to any local variables set from XML layout files.

The solution is to use a different approach to pass data to your blocks:

  1. Create a new class that extends the block you’re using.
  2. In this new class’s _construct method, call the parent constructor and then load your data based on the config value.

Here’s an example:

class MyCustomBlock extends Mage_Core_Block_Template
{
    public function _construct()
    {
        parent::_construct();

        // Load your custom block data here
        $instanceRef = (int) $this->getData('SimpleMenuInstanceRef');
        if ($instanceRef) {
            // Load data based on the instanceRef value
            // ...
        }
    }
}

Then, in your page.xml layout file, you can use this new class instead:

<block type="namespace_module/my_custom_block" name="leftMenu" as="leftMenu" template="simplemenu/leftMenuTemplate.phtml">
    <action method="setSimpleMenuInstanceRef"><SimpleMenuInstanceRef>4</SimpleMenuInstanceRef></action>
</block>

Conclusion

In Magento, when you need to use the same block with different data on a single page, it’s best to create a new class that extends the original block and override the _construct method.

This allows you to access configuration values set in your XML layout files within the context of your block code. Just remember to call the parent constructor first before performing any custom logic.

For more detailed guides on Magento development and best practices, check out our Magento Development Category at LaravelCompany.

Happy coding!

Database Themes Configuration Errors

Our network