1
votes

I have made a custom module in Magento. I am now trying to make it load a custom layout/template. I am sure it is something simple I am missing. Please take a look at my code and tell me what I am doing wrong.

My Package is called "Ben" and my Module "Distribution"

Firstly in "App/etc/modules" i let magento know about my module "Ben_Distribution.xml"

<?xml version="1.0"?>
 <config>
    <modules>
        <Ben_Distribution>
            <active>true</active>
            <codePool>local</codePool>
        </Ben_Distribution>
    </modules>    
</config>

Then in "app/code/local/Ben/Distribution/controllers" i put my controller "IndexController.php"

<?php
class Ben_Distribution_IndexController extends Mage_Core_Controller_Front_Action
{
    public function indexAction()
    {
        //echo 'Hello World';
        $this->loadLayout();    
        $this->renderLayout();
    }
}
?>

Then in "App/code/local/Ben/Distribution/etc" i put my config file "config.xml"

<config>
    <modules>
        <Ben_Distribution>
            <version>0.1.0</version>
        </Ben_Distribution>
    </modules>   

    <frontend>
        <routers>
            <distribution>
                <use>standard</use>
                <args>
                    <module>Ben_Distribution</module>
                    <frontName>distribution</frontName>
                </args>
            </distribution>
        </routers>
        <layout>
            <updates>
                <distribution>
                    <file>distribution.xml</file>
                </distribution>
            </updates>
        </layout>                   
    </frontend>    
</config>

Then in "App/Design/Frontend/Packagename/themename/template/ben/" i put "distribution.phtml" containing some HTML to display

The finally i tried to make the layout.xml file so in "App/Design/Frontend/Packagename/themename/layout/ben/" i put "distribution.xml"

<?xml version="1.0"?>
<layout version="0.1.0">
    <distribution_index_index>
        <reference name="content">
            <block type="core/template" name="distribution" template="distribution.phtml"/>
        </reference>
    </distribution_index_index>
</layout>

The final result is that my page loads at www.url.co.uk/distribution/ as expected. When i used the echo statement in the controller it loads fine (a blank page with "Hello World". but when i switch to load/render the layout, i get the default website layout and none on the content in my .phtml file. So my layout.xml isnt being loaded in.

Thanks

ADDITIONAL INFO I had my .phtml template loading my using this code in the controller, but as this loaded the standard "3 column" page layout and i needed a 1 column layout i have tried to do it using the layout.xml file instead.

<?php 
class Ben_Distribution_IndexController extends Mage_Core_Controller_Front_Action
{
    public function indexAction()
    {
        //Get current layout state
        $this->loadLayout();   

        $block = $this->getLayout()->createBlock(
            'Mage_Core_Block_Template',
            'ben.distribution',
            array(
                'template' => 'ben/distribution.phtml'
            )
        );

        $this->getLayout()->getBlock('content')->append($block);

        $this->_initLayoutMessages('core/session');

        $this->renderLayout();
    }
}
?>
1

1 Answers

1
votes

I found my own simple mistake. Sorry to waste your time.

<block type="core/template" name="distribution" template="distribution.phtml"/>

should be

<block type="core/template" name="distribution" template="ben/distribution.phtml"/>