4

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.

2
  • Could you add your config.xml, controller.php and layout.xml. It may be a issue with your (<routeurfrontend_index_index>) route Commented Feb 16, 2015 at 23:16
  • @beginner : Just to have a curiosity : why did you unmark tick from my answer ? and put tick on sukeshini's answer ? Commented Feb 23, 2015 at 4:53

2 Answers 2

7

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>

6
  • my default package is rwd, the xml is not connecting to the template. this is the path to the template. public_html/dev/app/design/frontend/rwd/default/template/test. am i doing anything wrong ? my layout is in public_html/dev/app/design/frontend/rwd/default/layout.
    – beginner
    Commented Feb 17, 2015 at 1:17
  • 1
    New layout and template files belong in base/default. Always and forever.
    – benmarks
    Commented Feb 17, 2015 at 1:24
  • Go to ` System > Configuration > Design ` and verify your current theme. And ensure that you have some text in your template file and the front name is correct
    – Sukeshini
    Commented Feb 17, 2015 at 3:14
  • i did, its in rwd. i have other modules created by someone else and they all work.
    – beginner
    Commented Feb 17, 2015 at 5:21
  • Can you provide your codes?
    – Sukeshini
    Commented Feb 17, 2015 at 6:20
7

So you just want to create your own custom block. Then follow these steps. I am going to call this module as Rkt_CustomBlock.

First of all, these are the files that are avilable in this extension

rootdir
|________ app
        |________ etc
        |           - Rkt_CustomBlock.xml
        |________ code/local/Rkt/CustomBlock
        |       |________ etc
        |       |           - config.xml
        |       |________ controllers
        |       |           - IndexController.php
        |       |           
        |       |________ Block
        |                   - Helloworld.php
        |               
        |________ design/frontend/rwd/default
                |________ layout
                |           - rkt_customblock.xml
                |________ template/rkt_customblock
                            - helloworld.phtml

First of you need to carefully construct this file structure. Now we can go into the codes.

Module Activation File

File : app\etc\modules/Rkt_CustomBlock.xml

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

This file is used to activate our custom module. Here it does two things. First it makes our module enabled (<active>true</active>) and secondly defines where our module is going to reside. According to the definition, our module is going to reside in local codepool.

Module Configuration File

file : app\code\local\Rkt/CustomBlock/etc/config.xml

<config>
    <modules>
        <Rkt_CustomBlock>
            <version>1.0.0</version>
        </Rkt_CustomBlock>
    </modules>
    <frontend>
        <routers>
            <rkt_customblock>
                <use>standard</use>
                <args>
                    <module>Rkt_CustomBlock</module>
                    <frontName>helloworld</frontName>
                </args>
            </rkt_customblock>
        </routers>
        <layout>
            <updates>
                <rkt_customblock>
                    <file>rkt_customblock.xml</file>
                </rkt_customblock>
            </updates>
        </layout>
    </frontend>
    <global>
        <blocks>
            <rkt_customblock>
                <class>Rkt_CustomBlock_Block</class>
            </rkt_customblock>
        </blocks>
    </global>
</config>

This file is used to configure our module. <modules> node is used to define version of our module. Here our module version is 1.0.0 This section is not important for us. Since we only deal with blocks. So this part is not necessary in our case. But it is a good practice to add this section in our module.

<frontend> section is used to declare a unique router and a frontend layout xml file for our extension. Here we set router <frontName> as helloworld. This means any url which is going to follow www.yourdomain.com/helloworld/... format is going to use our module. We declared a layout xml file for our extension only to add layout updates of our own custom extension.

<global> section is used to declare our block section. The code inside this node two things here. First it set a unique identifier for extension's block. Here it is rkt_customblock (in general namespace_modulename). Beware.. it should be unique. Also it defines where exactly extension is going to hold block classes. According to the defintion, it will be at Rkt_CustomBlock_Block. Magento parse this value to the location app\code\local/Rkt/CustomBlock/Block to find our custom block classes.

Controller of our Extension

File : app\code\local\Rkt/CustomBlock/controllers/IndexController.php

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

First our controller extends from the class Mage_Core_Controller_Front_Action. This is very important. Every frontend controller classes should extend from this core class.

Now as you can see our controller holds only one method indexAction() and it just loads the layout and then render those layout. Those two simple, humble lines of codes do a lot and I dont want to explain all those things here. You just want to know, those codes are responsible for constituting a magento page. Generally almost every controller action method hold these two line of codes.

Define Custom Block

File : app\code\local\Rkt/CustomBlock/Block/Helloworld.php

<?php
class Rkt_CustomBlock_Block_Helloworld extends Mage_Core_Block_Template
{
    public function getText()
    {
        return "I am inside `" . get_class(). "` block and it is my first custom block ever !";
    }
}

Helloworld is our custom block. This block is going to show in frontend. You can see that our block extends from Mage_Core_Block_Template. This is very important, because that class provides us a lot of template related functionalities.

Secondly, you can see that, I defined a custom method getText(). The usage of this method will be discussed later.

Layout Update XML file

File : app\design\frontend\rwd\default\layout\rkt_customblock.xml

<layout>
    <rkt_customblock_index_index>
        <reference name="root">
            <action method="setTemplate"><template>page/1column.phtml</template></action>
        </reference>
        <reference name="content">
            <block type="rkt_customblock/helloworld" name="myblock.helloworld" template="rkt_customblock/helloworld.phtml" />
        </reference>
    </rkt_customblock_index_index>
</layout>

<rkt_customblock_index_index /> is known as layout handle. More specifically we can call this layout handle as controller action layout handle. This is because, the layout definition inside this layouthandle will get included in the layout only when a specific controller action is get executed. In the above case, this layout handle will get processed only when indexAction() method present in IndexController.php of Rkt_CustomBlock.

root block is used to set a page template for the page. We included our custom block inside content block. (root and content blocks are core blocks which are used in magento layout construction. These blocks has specific usages).

This is how our block is defined inside our module

 <block type="rkt_customblock/helloworld" name="myblock.helloworld" template="rkt_customblock/helloworld.phtml" />

type : this is used to recognize the block class. Here rkt_customblock/helloworld stands for the block Rkt_CustomBlock_Block_Helloworld.

name : name of the block. It should be unique.

template : this is the template file which is going to use by the block.

Define Template

File : app\design\frontend\rwd\default\template\rkt_customblock/helloworld.phtml

<h1>Hello World !</h1>
<p>Hello... Can you see me ? If yes I just want to say</p>
<h4><?php echo $this->getText(); ?></h4> 

Here you can see that, we are calling to the method getText() which is defined inside your custom block. This means that, helloworld.phtml file is actually an instance of Rkt_CustomBlock_Block_Helloworld.

Now, go and clear the cache and load the page www.yourdomain.com/helloworld. You will find an output like this.

Hello World !

Hello... Can you see me ? If yes I just want to say

I am inside Rkt_CustomBlock_Block_Helloworld block and it is my first custom block ever !

Note 1: This module is based on Magento 1.9 and later. For all lower version, you can use base/default package theme for your layout instead of rwd/default.

You can quickly refer this small extension here. Let me know if you have any doubts !!

5
  • Thank you so much it worked. can you also extend it to access a database table if possible
    – beginner
    Commented Feb 17, 2015 at 5:44
  • @beginner: It is too broad.. There are lot of tutorials available online. You can follow that. If you have specific doubt, feel free to ask another question here. If I write a post about database integration in a custom extension, then I will let you know. Happy to help you. :-) Commented Feb 17, 2015 at 5:57
  • lets say i have a database name client and its primary key is scid. i want to display clients information in a page. it needs to be sorted by the scid.
    – beginner
    Commented Feb 17, 2015 at 8:21
  • heres an example, pierrefay.com/magento-model-database-tutorial-54, i want to get access to the table and put the database entry in a form. update edit load and delete.
    – beginner
    Commented Feb 18, 2015 at 0:10
  • @beginner : I got your point. Try those free available blogs right now. I am currently busy. Creating and explaining magneto model structure require a lot of time. If you find any problems while doing this, feel free to come here and post another question. But remember question should be specific. Commented Feb 18, 2015 at 1:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.