Magento 1 – How to Create a Custom Block in Custom Module

blocksmodule

I have followed a tutorial to create the block. All codes are in here
http://www.pierrefay.com/magento-create-block-44

but I cant seem to connect to the template file, no matter where i put it. i tried the base then tried the default and then tried the rwd. it still does not connect. i changed the default package through admin panel to rwd and it still wont work. any comment or suggestions will be highly helpful. I am stuck in it for days.

Best Answer

You might be missed some of the sections in your module. Double check and verify whether you have the done the followings.

  1. Module activation file.

Create a file in app/etc/modules as Pfay_Test.xml. See below.

<config>
    <modules>
        <Pfay_Test>
            <active>true</active>
            <codePool>local</codePool>
        </Pfay_Test>
    </modules>
</config>
  1. Put your layout and template files under the current activated theme

  2. Clear the cache or disable while you developing the modules. Go to System > Cache Management

I'll put all the content which should work. If you don't have any of the above issues please check you have all the files below

1.) Pfay/Test/etc/config.xml

<config>
    <modules>
        <Pfay_Test>
            <version>0.1.0</version>
        </Pfay_Test>
    </modules>
    <global>
        <blocks>
            <test>
                <class>Pfay_Test_Block</class>
            </test>
        </blocks>
    </global>
    <frontend>
        <routers>
            <test>
                <use>standard</use>
                <args>
                    <module>Pfay_Test</module>
                    <frontName>test</frontName>
                </args>
            </test>
        </routers>
        <layout>
           <updates>
                <test>
                     <file>test.xml</file>
                 </test>
            </updates>
        </layout>
    </frontend>
</config>

2.) Pfay/Test/controllers/IndexController.php

<?php
class Pfay_Test_IndexController extends Mage_Core_Controller_Front_Action
{
    public function indexAction()
    {
        $this->loadLayout ();
        $this->renderLayout ();
    }

    public function mamethodeAction()
    {
        echo 'test mamethode';
    }
}

3.) Pfay/Test/Block/Monblock.php

<?php
class Pfay_Test_Block_Monblock extends Mage_Core_Block_Template
{
    public function methodblock()
    {
        return 'informations about my block !!';
    }
}

4.) app/etc/modules/Pfay_Test.xml

<config>
    <modules>
        <Pfay_Test>
            <active>true</active>
            <codePool>local</codePool>
        </Pfay_Test>
    </modules>
</config>

5.) app/design/frontend/yourpackage/yourtheme/layout/test.xml

<layout version="0.1.0">
    <default>
        <reference name="content">
        </reference>
    </default>
    <test_index_index>
        <reference name="content">
            <block type="test/monblock" name="afficher_monbloc" template="test/afficher.phtml" />
        </reference>
    </test_index_index>
</layout>

6.) app/design/frontend/yourpackage/yourtheme/template/test/afficher.phtml

<h1>Hello</h1>