1

I can post my code. it's working controller and also block file but template File not working.

Sending code here.

 <?xml version="1.0" ?>
<!--
/**
* Module Configuration
*
*@author Magento
-->
<config>
    <modules>
        <Magentostudy_News>
        <version>1.0.0</version>
        </Magentostudy_News>
    </modules>
    <frontend>
        <routers>
            <magentostudy_news>
                <use>standard</use>
                <args>
                <module>Magentostudy_News</module>
                <frontName>news</frontName>
                </args>
            </magentostudy_news>
        </routers>
    </frontend>
    <layout>
            <updates>
            <magentostudy_news>
            <file>news.xml</file>
            </magentostudy_news>
            </updates>
    </layout>

    <global>
         <blocks>
                <news><class>Magentostudy_News_Block</class></news>
         </blocks>

    </global>

</config>

My news.xml file

    <layout version="0.1.0">
<default>
    <reference name="content"></reference>
</default>
<news_index_index>
    <reference name="content">

    <block type="news/news" name="news.slider" template="news/news.phtml"></block>

    </reference>

</news_index_index>
<layout>

This is My controller file

    <?php

class Magentostudy_News_IndexController extends Mage_Core_Controller_Front_Action{

    public function _construct(){


      Mage::app()->loadArea($this->getLayout()->getArea());

        $this->loadLayout();
        $this->renderLayout();


    }

    public function indexAction(){


       $cpBlock = $this->getLayout()->getBlockSingleton('Magentostudy_News_Block_News');

    }

}

?>

This is My Block file

    <?php

class Magentostudy_News_Block_News extends Mage_Core_Block_Template{

    public function _construct()
    {
        parent::_construct();

        $this->setTemplate('news/news.phtml'); 
    }



}

?>

Tell me What type of issue in this file? and how to slove it?

2

I'm not sure what tutorial you are using, but it seem a little overly complicated than it need to be. Take a look at How to create front-end module in Magento

In your controller all you need to do is

<?php
class Company_Web_IndexController extends Mage_Core_Controller_Front_Action
{
    public function indexAction()
    {
      $this->loadLayout();     
      $this->renderLayout();
    }
}

In you DO NOT have a news.xml file then in your controller you could

   $this->loadLayout();       
   $this->getLayout()->getBlock('content')->append($this->getLayout()->createBlock('magentostudy/news_news'));
    $this->renderLayout();
1

You are not loading and rendering layouts in your indexAction(). It should look like this.

<?php
public function indexAction() 
{
    $this->loadLayout();
    //your custom changes should do here
    $this->renderLayout();
}

Without doing this, Magento will not render blocks inside the handle news_index_index.

I am totally not understanding why you are loading and rendering layout through __construct(). I think, you should not do that.

Also, you have already specified your template in layout file. So it is not necessary to define same template for your block through backend. ie via Magentostudy_News_Block_News::_construct(). Remove that part

  • Remove _construct in my block and also set in new code in indexcontrller $block = $this->getLayout()->createBlock( 'Mage_Core_Block_Template', 'Magentostudy_News_Block_News', array('template' => 'news/news.phtml') ); $this->getLayout()->getBlock('content')->append($block); $this->_initLayoutMessages('core/session'); $this->renderLayout(); – coder Aug 28 '14 at 13:32
  • but its not working also change in xml file – coder Aug 28 '14 at 13:34
  • <layout version="0.1.0"> <default> <reference name="content"></reference> <reference name="content"> <block type="news/news" name="news.slider" template="news/news.phtml"></block> </reference> </default> <layout> – coder Aug 28 '14 at 13:35
  • Call to a member function append() on a non-object error is display – coder Aug 28 '14 at 13:36
  • no you dont need to put anything extra in indexAction. JUst loadLayout and renderLayout enough – Rajeev K Tomy Aug 28 '14 at 14:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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