Magento: Several custom blocks all outputting the same code!
I have a custom module, which has several blocks. If I include these blocks in a CMS page after each other, they work as expected. If I include them through the layout XML files, they all display the source code of the last one called in the XML. A minimum test case (that for me is exhibiting this behavior) follows, along with expected and actual results.
Code
/app/etc/modules/Test_Tester.xml
<?xml version="1.0"?>
<config>
<modules>
<Test_Tester>
<active>true</active>
<codePool>local</codePool>
</Test_Tester>
</modules>
</config>
/app/code/local/Test/Tester/etc/config.xml
<?xml ve开发者_开发问答rsion="1.0"?>
<config>
<modules>
<Test_Tester>
<version>0.1.0</version>
</Test_Tester>
</modules>
<global>
<blocks>
<test_tester>
<class>Test_Tester_Block</class>
</test_tester>
</blocks>
</global>
</config>
/app/code/local/Test/Tester/Block/One.php
<?php
class Test_Tester_Block_One extends Mage_Catalog_Block_Product_List_Upsell
{
protected function _prepareData()
{
echo 'One.php';
//...MORE code here, it's not really relevant though
}
}
/app/code/local/Test/Tester/Block/Two.php
<?php
class Test_Tester_Block_Two extends Mage_Catalog_Block_Product_List_Upsell
{
protected function _prepareData()
{
echo 'Two.php';
//...MORE code here, it's not really relevant though
}
}
/app/design/frontend/INTERFACE/TEMPLATE/layout/page.xml (under
..
<block type="core/text_list" name="testa" as="testa" />
<block type="core/text_list" name="testb" as="testb" />
..
/app/design/frontend/INTERFACE/TEMPLATE/layout/cms.xml (under
<reference name="testa">
<block type="test_tester/one" template="tester/one.phtml"/>
</reference>
<reference name="testb">
<block type="test_tester/two" template="tester/two.phtml"/>
</reference>
/app/design/frontend/INTERFACE/TEMPLATE/template/page/home_template.phtml
<?php echo $this->getChildHtml('testa'); ?>
<?php echo $this->getChildHtml('testb'); ?>
/app/design/frontend/INTERFACE/TEMPLATE/tester/one.phtml
one.phtml
/app/design/frontend/INTERFACE/TEMPLATE/tester/two.phtml
two.phtml
Expected
This should print out (on the homepage, where the blocks are being included):
One.php
one.phtml
Two.php
two.phtml
Actual Output
If I include the blocks within the Homepage CMS page, like so:
{{block type="test_tester/one" template="tester/one.phtml"}}
{{block type="test_tester/two" template="tester/two.phtml"}}
...I get the expected output. However, using the layout as above in the code sample, I get:
Two.php
two.phtml
Two.php
two.phtml
I think I've gone insane - I can't see the bit I'm mucking up.
Try giving your blocks names in the homepage CMS page. Similar errors I've received have been resolved this way. I see that you're trying to wrap the blocks in text lists, but from what I see you never actually identify the blocks from within the CMS page. Try something to this effect instead:
{{block type="test_tester/one" template="tester/one.phtml" name="testa"}}
{{block type="test_tester/two" template="tester/two.phtml" name="testb"}}
If you cannot get away from the parent blocks as containers, you may have to rethink part of your layout. If that doesn't do it either way, let me know and we'll try something else. Hope that helps.
Thanks, Joe
精彩评论