Layout for Magento module

10,852

so, there is some stuff missing...

  • declare the layout update in the your config.xml:

    <frontend>
        ...
        <layout>
            <updates>
                <helloworld>
                    <file>helloworld.xml</file>
                </helloworld>
            </updates>
        </layout>
        ...
    </frontend>
    
  • create the layout xml file, in app/design/frontend/base/default/layout/helloworld.xml and in it you create a reference to your module/controller/action:

    <?xml version="1.0"?>
    <layout>
        <helloworld_index_index>
            <reference name="content">
                <block type="core/template" name="helloworld" template="helloworld.phtml" />
            </reference>
        </helloworld_index_index>
    </layout>
    
  • create the phtml file you've just set as template in your xml layout file, ie app/design/frontend/base/default/template/helloworld.phtml:

    this is the content of helloworld.phtml
    
  • load and render all this stuff, in your controller's action, replace your echo statement with:

    public function indexAction()
    {
        $this->loadLayout();
        $this->renderLayout();
    }
    
  • disable cache, refresh browser, sit back and enjoy
Share:
10,852
zuzuleinen
Author by

zuzuleinen

Software web developer.

Updated on June 05, 2022

Comments

  • zuzuleinen
    zuzuleinen almost 2 years

    I'm new in Magento and I'm trying to make a layout for a module I've build. I have a simple module and an IndexController which outputs 'Hello World.'(I've use this tutorial).

    Now I want to make a layout for that module and I've used this tutorial but it doesn't work. Can someone point me to a tutorial or can explain me how layouts work in Magento?

    Thx :)

    Here's what I've done so far: I have a package called 'Andrei' and a module 'Hello World'.

    Here is the config.xml for my module:

    <?xml version="1.0" encoding="UTF-8"?>
    <config>    
        <modules>
            <Andrei_Helloworld>
                <version>0.1.0</version>
            </Andrei_Helloworld>
        </modules>
        <frontend>
            <routers>
                <helloworld>
                    <use>standard</use>
                    <args>
                        <module>Andrei_Helloworld</module>
                        <frontName>helloworld</frontName>
                    </args>
                </helloworld>
            </routers>  
        </frontend>
    </config> 
    

    Here is the Andrei_Helloworld module:

    <?xml version="1.0" encoding="UTF-8"?>
    <config>
        <modules>
            <Andrei_Helloworld>
                <active>true</active>
                <codePool>local</codePool>
            </Andrei_Helloworld>
        </modules>
    </config> 
    

    And here is my controller:

    class Andrei_Helloworld_IndexController extends Mage_Core_Controller_Front_Action
    {
        public function indexAction()
        {
            echo 'Hello world';
        }
    }
    

    This is all I've done so far. The module works properly. And I want a layout for my IndexController. Thx :)