I’m trying to set a variable in my local.xml file for my custom block:
<layout>
<!-- ... -->
<page_homepage>
<!-- ... -->
<reference name="root">
<!-- ... -->
<block type="core/template" name="home_page_sections" template="page/homepage/sections.phtml">
<block type="layout/carousel" name="featured_carousel">
<action method="setData">
<name>filter_attribute</name>
<value>is_featured_product</value>
</action>
</block>
</block>
</reference>
</page_homepage>
</layout>
But I am not getting the data on the other end in my controller:
class Foo_Layout_Block_Carousel extends Mage_Core_Block_Template
{
public function __construct()
{
parent::__construct();
$filterAttribute = $this->getFilterAttribute(); // Nothing
$filterAttribute = $this->getData('filter_attribute'); // Nada
// Alright, fine, what DO I have?!
var_dump($this->getData()); // array(0) {} ... Argh!
}
}
From all my searching I’ve found that this really should work, but since it does not, I have a feeling I’m missing something obvious. Here is my layout module’s configuration (I’m using a single module to define a homepage and any other blocks I need for the site):
<?xml version="1.0"?>
<config>
<modules>
<Foo_Layout>
<version>0.1.0</version>
</Foo_Layout>
</modules>
<global>
<page>
<layouts>
<foo_homepage translate="label">
<label>Homepage</label>
<template>page/homepage.phtml</template>
<layout_handle>page_homepage</layout_handle>
</foo_homepage>
</layouts>
</page>
<blocks>
<layout>
<class>Foo_Layout_Block</class>
</layout>
</blocks>
</global>
</config>
When the layout rendering code encounters this
It immediately instantiates the block. That means the block’s
__constructmethod is called before yoursetDatamethod is called. So, at the time of construction, no data has been set, which is why your calls tovar_dumpreturn an empty array.Also, immediately after being created, the block is added to the layout
When this happens, the block’s
_prepareLayoutmethod is called.So, this means that any data set in your layout update xml is still not available, even in
_prepareLayout. Once the system is done creating the block, it moves on to the next XML node.and calls the
setDatamethod. Now your block has its data set.Try defining a
_beforeToHtmlmethod on your block and checking for data there. (Assuming your block is being rendered)