I'm trying to add custom block module to homepage but it does not load my block.phtml file. Here are my files:
app/etc/modules/MG_Block.xml:
<config>
<modules>
<MG_MyBlock>
<active>true</active>
<codePool>local</codePool>
</MG_MyBlock>
</modules>
</config>
app/code/local/MG/MyBlock/etc/config.xml:
<config>
<modules>
<MG_MyBlock>
<version>0.1.0</version>
</MG_MyBlock>
</modules>
<global>
<blocks>
<mg_myblock>
<class>MG_MyBlock_Block</class>
</mg_myblock>
</blocks>
</global>
</config>`
app/code/local/MG/MyBlock/Block/Products.php:
class MG_MyBlock_Block_Products extends Mage_Core_Block_Abstract{
public function index(){
return "hello world";
}
}
app/design/frontend/rwd/default/template/mg/products.phtml:
<?php echo $this->index(); ?>
app/design/frontend/rwd/default/layout/local.xml:
<layout version="0.1.0">
<default>
<reference name="content">
<block type="mg_myblock/products" output="toHtml" name="myblock" template="mg/products.phtml" after="-" />
</reference>
</default>
</layout>
When I use var_dump in _construct() function in my block it shows var_dump but it does not load template file.
2 Answers 2
First add "module.xml" in layout tag in "config.xml".
<config>
<modules>
<MG_MyBlock>
<version>0.1.0</version>
</MG_MyBlock>
</modules>
<global>
<blocks>
<mg_myblock>
<class>MG_MyBlock_Block</class>
</mg_myblock>
</blocks>
</global>
<frontend>
<routers>
<myblock>
<use>standard</use>
<args>
<module>myblock</module>
<frontName>formtest</frontName>
</args>
</myblock>
</routers>
<layout>
<updates>
<mymodule>
<file>mymodule.xml</file>
</mymodule>
</updates>
</layout>
</frontend>
</config>
Then define "module.xml".
<layout version="0.1.0">
<formtest_myblock_index>
<reference name="content">
<block type="mg_myblock/products" output="toHtml" name="myblock" template="mg/products.phtml" after="-" />
</reference>
</formtest_myblock_index>
</layout>
Try with this.
Rename the file MG_Block.xml in app/etc/modules/ to MG_MyBlock.xml
Mage_Core_Block_Templateinstead ofMage_Core_Block_Abstractand it works.