5

I am new to stackoverflow but i will be very open with my doubt and questions which i want to ask.

I am trying to create a custom block in magento. I am newbie to magento and learning it to become a advanced developer of magento.

I have tried the following things:

  1. I have created a directory Magentotutorial in magento/app/code/local/Magentotutorial.

  2. I have made a basic structure in it which are five directory in Magentotutorial. So the location is magento/app/code/local/Magentotutorial/World

Directories are :

magento/app/code/local/Magentotutorial/World/controllers,
magento/app/code/local/Magentotutorial/World/sql,
magento/app/code/local/Magentotutorial/World/Model,
magento/app/code/local/Magentotutorial/World/Helper,
magento/app/code/local/Magentotutorial/World/Block,
magento/app/code/local/Magentotutorial/World/etc.
  1. My config.xml goes into magento/app/code/local/Magentotutorial/World/etc/config.xml

config.xml file contains the following code:

<?xml version="1.0" encoding="UTF-8"?>
<config>
    <modules>
        <Magentotutorial_World>
            <version>0.1.0</version>
        </Magentotutorial_World>
    </modules>
    <global>
        <blocks>
            <Magentotutorial_World>
                <class>Magentotutorial_World_Block</class>
            </Magentotutorial_World>
        </blocks>
    </global>
    <frontend>
        <layout>
            <updates>
                <Magentotutorial_World>
                    <file>test.xml</file>
                </Magentotutorial_World>
            </updates>
        </layout>
    </frontend>
</config>

To activate my module in magento i have created xml file.

Magentotutorial_World.xml:

<?xml version="1.0" encoding="UTF-8"?>
<config>
    <modules>
        <Magentotutorial_World>
            <active>true</active>
            <codePool>local</codePool>
        </Magentotutorial_World>
    </modules>
</config>
  1. In my Block directory which is in magento/app/code/local/Magentotutorial/World/Block i have creted a file named example.php

example.php has the following code:

<?php
class Magentotutorial_World_Block_Example extends Mage_Core_Block_Template
{

}

5. Now i have layout and template in my magento directory atmagento/app/design/frontend/rwd/default/layout In my layout file i have made test.xml file which has the following code:

<?xml version="1.0" encoding="UTF-8"?>
<layout>
    <default>
            <block type="magentotutorial_world/example" name="newreferenceBlock" template="test/example.phtml" />
    </default>
</layout>
  1. I have made a example.phtml file in test folder which is at the location magento/app/design/frontend/rwd/default/template/test/example.phtml

example.phtml has the following code:

<html>
<body>
    <h1 style="background-color:yellow">Hello New Reference!</h1>
</body>
</html>
  1. Now in page.xml i have put this code

    <default translate="label" module="page">
        <label>All Pages</label>
    
    
    
        <block type="page/html" name="root" output="toHtml" template="page/3columns.phtml">
    
            <block type="magentotutorial_world/example" output="toHtml" name="newReferenceBlock" template="test/example.phtml" />
    
            <block type="page/html_head" name="head" as="head">
                <action method="addJs"><script>prototype/prototype.js</script></action>
                <action method="addJs"><script>lib/ccard.js</script></action>
                <action method="addJs"><script>prototype/validation.js</script></action>
                <action method="addJs"><script>scriptaculous/builder.js</script></action>
                <action method="addJs"><script>scriptaculous/effects.js</script></action>
                <action method="addJs"><script>scriptaculous/dragdrop.js</script></action>
                <action method="addJs"><script>scriptaculous/controls.js</script></action>
                <action method="addJs"><script>scriptaculous/slider.js</script></action>
                <action method="addJs"><script>varien/js.js</script></action>
                <action method="addJs"><script>varien/form.js</script></action>
                <action method="addJs"><script>mage/translate.js</script></action>
                <action method="addJs"><script>mage/cookies.js</script></action>
    

Over here i have put out my code which is

<block type="magentotutorial_world/example" output="toHtml" name="newReferenceBlock" template="test/example.phtml" />
  1. Now i want to render this template to any layout of my page. I have here tried it for 2columns-right.phtml file.

in this file i have used getChildHtml.

<div class="col-left sidebar"><?php echo $this->getChildHtml('newreferenceBlock'); ?></div>

The whole file is

<?php
/**
 * Magento
 *
 * NOTICE OF LICENSE
 *
 * This source file is subject to the Academic Free License (AFL 3.0)
 * that is bundled with this package in the file LICENSE_AFL.txt.
 * It is also available through the world-wide-web at this URL:
 * http://opensource.org/licenses/afl-3.0.php
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@magentocommerce.com so we can send you a copy immediately.
 *
 * DISCLAIMER
 *
 * Do not edit or add to this file if you wish to upgrade Magento to newer
 * versions in the future. If you wish to customize Magento for your
 * needs please refer to http://www.magentocommerce.com for more information.
 *
 * @category    design
 * @package     rwd_default
 * @copyright   Copyright (c) 2014 Magento Inc. (http://www.magentocommerce.com)
 * @license     http://opensource.org/licenses/afl-3.0.php  Academic Free License (AFL 3.0)
 */
?>
<?php
/**
 * Template for Mage_Page_Block_Html
 */
?>

<!DOCTYPE html>

<!--[if lt IE 7 ]> <html lang="en" id="top" class="no-js ie6"> <![endif]-->
<!--[if IE 7 ]>    <html lang="en" id="top" class="no-js ie7"> <![endif]-->
<!--[if IE 8 ]>    <html lang="en" id="top" class="no-js ie8"> <![endif]-->
<!--[if IE 9 ]>    <html lang="en" id="top" class="no-js ie9"> <![endif]-->
<!--[if (gt IE 9)|!(IE)]><!--> <html lang="en" id="top" class="no-js"> <!--<![endif]-->

<head>
<?php echo $this->getChildHtml('head') ?>
</head>
<body<?php echo $this->getBodyClass()?' class="'.$this->getBodyClass().'"':'' ?>>
<?php echo $this->getChildHtml('after_body_start') ?>
<div class="wrapper">
    <?php echo $this->getChildHtml('global_notices') ?>
    <div class="page">
        <?php echo $this->getChildHtml('header') ?>
        <div class="main-container col2-left-layout">
            <div class="main">
                <?php echo $this->getChildHtml('breadcrumbs') ?>
                <?php // This left_first block will display above the main content on smaller viewports ?>
                <?php if ($_leftContent = $this->getChildHtml('left_first')): ?>
                <div class="col-left sidebar col-left-first"><?php echo $_leftContent;   ?></div>
                <?php endif; ?>
                <div class="col-main">
                    <?php echo $this->getChildHtml('global_messages') ?>
                    <?php echo $this->getChildHtml('content') ?>
                </div>
                <div class="col-left sidebar"><?php echo $this->getChildHtml('left') ?></div>
            </div>
<div class="col-left sidebar"><?php echo $this->getChildHtml('newreferenceBlock') ?></div>
        </div>
        <?php echo $this->getChildHtml('footer_before') ?>
        <?php echo $this->getChildHtml('footer') ?>
        <?php echo $this->getChildHtml('global_cookie_notice') ?>
        <?php echo $this->getChildHtml('before_body_end') ?>
    </div>
</div>
<?php echo $this->getAbsoluteFooter() ?>
</body>
</html>

Now I am Not able to render this block on any of my pages. Basically i want to add this template on my index page.

What is going wrong in my code?? I also want to know that is my block type ="magentotutorial_world" is right??

What if i want to revert changes in my test.xml and want to make changes in my page.xml file. How to do that???

Kindly help me out.

Thanks and regards.

Jay Desai
  • 475
  • 3
  • 10
  • 27

2 Answers2

2

You mistyped block set in magento/app/code/local/Magentotutorial/World/etc/config.xml. Change Magentotutorial_World to lowercased magentotutorial_world:

<?xml version="1.0" encoding="UTF-8"?>
<config>
    <modules>
        <Magentotutorial_World>
            <version>0.1.0</version>
        </Magentotutorial_World>
    </modules>
    <global>
        <blocks>
            <magentotutorial_world>
                <class>Magentotutorial_World_Block</class>
            </magentotutorial_world>
        </blocks>
    </global>
    <frontend>
        <layout>
            <updates>
                <Magentotutorial_World>
                    <file>test.xml</file>
                </Magentotutorial_World>
            </updates>
        </layout>
    </frontend>
</config>
P0ZiTR0N
  • 612
  • 4
  • 9
0

I think your block type should be

<block type="world/example" output="toHtml" name="newReferenceBlock" template="test/example.phtml" />

Correct me if i was wrong.

Navin Bista
  • 1,988
  • 2
  • 21
  • 37