0

I have created a module with controller and model. But I am now trying to load a custom view where I can stuff all my PHP,css,js code into that PHTML file.

Here is my Block of Code

app\code\local\Soumen\Testmodule\controllers\IndexController.php

IndexController file


          public function indexAction()
          {
              echo "This is default";
           }

          public function sayHelloAction()
          {
              Mage::log('im in Controller');

              $this->loadLayout();
              $this->renderLayout();
             Zend_Debug::dump($this->getLayout()->getUpdate()->getHandles());
          }


Config file

    app\code\local\Soumen\Testmodule\etc\config.xml

<config>


     <frontend>
        <routers>
              <Soumen_Testmodule>
                 <use>standard</use>
                 <args>
                      <module>Soumen_Testmodule</module>
                      <frontName>testmodule</frontName> 
                 </args>
              </Soumen_Testmodule>
        </routers>

        <layout>
            <updates>
                <testmodule>
                    <file>testmodule.xml</file>
                </testmodule>
            </updates>
      </layout>
     </frontend>
</config>



app\design\frontend\base\default\layout\testmodule.xml


<?xml version="1.0"?>
<layout version="0.1.0">
    <testmodule_index_index>
        <reference name="content">
            <block type="testmodule/test" name="test" template="testmodule/test.phtml" />
        </reference>
    </testmodule_index_index>
</layout>`

app\code\local\Soumen\Testmodule\Block\Test.php


class Soumen_Testmodule_Block_Test extends Mage_Core_Block_Template
    {
         public function getContent()
         {
             return "Hello World";
          }
     }

app\design\frontend\base\default\template\testmodule\test.phtml


<?php  echo $this->getContent();?>

Now when I call http://magento/testmodule... Then it displays the dufault page, but should show Hello World . Can Someone help me out , that what wrong I am doing

5
  • Is that the entire config.xml and controller file? What do you see in your browser? Do you have a layout file? Can you post it? As this question stands, there's nowhere near enough information to start on it...
    – tjons
    Jan 11, 2017 at 16:26
  • add your testmodule.xml from layout folder and block code
    – sandip
    Jan 11, 2017 at 16:27
  • <?xml version="1.0"?> <layout version="0.1.0"> <testmodule_index_sayHello> <reference name="content"> <block name="testmodule" template="newpage/content.phtml"/> </reference> </testmodule_index_sayHello> </layout> Jan 11, 2017 at 16:27
  • I am able to see the default page of my theme, but I want to display the contents of my conent.phtml file , where I have added my HTML stuff Jan 11, 2017 at 16:31
  • @SoumenKumarSaha for creating module you need to follow some basic steps.....as i written bellow (by Vishal Thakur). Just edit module name (Excellence_Test) as you want. Jan 11, 2017 at 19:19

2 Answers 2

1

In your testmodule.xml file use this

<testmodule_index_sayHello>
   <reference name="content"> 
       <block type="modulename/block" name="testmodule" template="newpage/content.phtml"/> 
   </reference> 
</testmodule_index_sayHello> 

If you don't have block of your module then you can use core/template as block type

0

There are few changes to make to the existing config.xml file. Since now we are using layouts, we will create our own layout file for our module. Lets name our module’s layout file as test.xml. We will place it in our theme/layout folder.

<?xml version="1.0"?>
<config>
    <modules>
        <Excellence_Test>
            <version>0.1.0</version>
        </Excellence_Test>
    </modules>
    <frontend>
        <routers>
            <test>
                <use>standard</use>
                <args>
                    <module>Excellence_Test</module>
                    <frontName>test</frontName>
                </args>
            </test>
        </routers>
        <layout>  <!-- New Section Added -->
            <updates>
                <test>
                    <file>test.xml</file> <!-- This is name of the layout file for this module -->
                </test>
            </updates>
        </layout>
    </frontend>
    <global>
        <blocks>
            <test>
                <class>Excellence_Test_Block</class>
            </test>
        </blocks>
        <helpers>
            <test>
                <class>Excellence_Test_Helper</class>
            </test>
        </helpers>
    </global>
</config>

Now create the test.xml file in your theme layout folder. Like in my case its app/design/frontend/default/default/layout/test.xml or if you have create your own theme, put the layout file there. Here are the contents of the file.

<?xml version="1.0"?>
<layout version="0.1.0">
    <test_index_index>
        <reference name="content">
            <block type="test/test" name="test" template="test/test.phtml" />
        </reference>
    </test_index_index>
</layout>

The explanation of this layout file is given previously here. So the block we used is “test/test” which equal class Excellence_Test_Block_Test and phtml file used is test/test.phtml So we will create a phtml file in location app/design/frontend/default/default/templates/test/test.phtml

<?php
    echo $this->getContent();
?>

In the file, we are calling a function getContent(); this function is to be declared in the block Test.php block file. As we have seen previous, the $this variable here is actually an object of our block class, which we define as ‘type’ in our layout.xml. Just to confirm you write below code in your phtml file.

<?php echo get_class($this); ?>

The output should be Excellence_Test_Block_Test. Now create the file Test.php at location app/code/local/Excellence/Test/Block/Test.php

<?php
class Excellence_Test_Block_Test extends Mage_Core_Block_Template
{
    public function getContent()
    {
        return "Hello World";
    }
}

Now, to call all these layouts we need to make some changes in the IndexController.php

<?php
class Excellence_Test_IndexController extends Mage_Core_Controller_Front_Action
{
    public function indexAction()
    {
        $this->loadLayout();  //This function read all layout files and loads them in memory
        $this->renderLayout(); //This function processes and displays all layout phtml and php files.
    }
}

Open the url www.your-magento.com/test/ again and it will again print ‘helloword’.

Your Answer

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

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