0
votes

I use Magento 1.9 on Ubuntu 14.04. Then, I try to create a module for magento. But the template is not loaded/rendered, it just output Block1_construct since I echo it from Block constructor but the layout is not red (I set the background color from template file). Please tell me where am I wrong ?

Here is my code :

app/code/local/Ags/Module1/etc/config.xml

<config>
    <modules>
        <Ags_Module1>
            <version>0.1.0</version>
        </Ags_Module1>
    </modules>
    <global>
        <blocks>
            <blockgroup>
                <class>Ags_Module1_Block</class>
            </blockgroup>
        </blocks>
    </global>
    <frontend>
        <routers>
            <route1>
                <use>standard</use>
                <args>
                    <module>Ags_Module1</module>
                    <frontName>frontname1</frontName>
                </args>
            </route1>
        </routers>
        <layout>
            <updates>
                <route1>
                    <file>ags/module1.xml</file>
                </route1>
            </updates>
        </layout>
    </frontend>
</config>

app/code/local/Ags/Module1/controllers/IndexController.php

class Ags_Module1_IndexController extends Mage_Core_Controller_Front_Action
{
    public function indexAction()
    {
        $this->loadLayout();
        $this->renderLayout();
    }
}

app/code/local/Ags/Module1/Block/Block1.php

class Ags_Module1_Block_Block1 extends Mage_Core_Block_Template
{
    public function __construct()
    {
        parent::__construct();
        //echo, so we know the block is called
        echo 'Block1_construct';
    }

    public function function1()
    {
        //echo, so we know the block function is called
        echo 'Block1_function1';
        return 'Block1 function1';
    }
}

app/design/frontend/base/default/layout/ags/module1.xml

<layout version="0.1.0">
    <route1_index_index>
        <!-- blockgroup === 'Ags_Module1_Block' -->
        <block type="blockgroup/block1" name="root" output="toHtml" template="ags/page1.phtml"></block>
    </route1_index_index>
</layout>

app/design/frontend/base/default/template/ags/page1.phtml

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Hello World</title>
    <style type="text/css">
        body {
            background-color: red;
        }
    </style>
</head>
<body>
<?php echo $this->function1(); ?>
</body>
</html>
2

2 Answers

0
votes

Problems are with your block name and reference in layout file( ags/module1.xml).

<layout version="0.1.0">
    <route1_index_index>
        <reference name="root">
            <action method="setTemplate"><template>page/1column.phtml</template></action>
            <!-- Mark root page block that template is applied -->
            <action method="setIsHandle"><applied>1</applied></action>
        </reference>
        <reference name="content">
            <block type="blockgroup/block1" name="ags_page" output="toHtml" template="ags/page1.phtml"></block>
        </reference>
    </route1_index_index>
</layout>
0
votes

try to update this file with below code app/design/frontend/base/default/layout/ags/module1.xml

<layout version="0.1.0">
<frontname1_index_index>

    <block type="blockgroup/block1" name="root" output="toHtml" template="ags/page1.phtml"></block>
</frontname1_index_index>