0

I am creating a custom module, everything is working fine but on view page, My phtml file calling first and then header is coming.

I am calling and setting data like this :

IndexController.php

class MagePeople_Mymodule_IndexController extends Mage_Core_Controller_Front_Action
{

    public function indexAction()
     {

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


    public function editMeasurementAction()
    {   
        $customer = Mage::getSingleton('customer/session')->getCustomer();
        $customerId = $customer->getId();

        $proid = $this->getRequest()->getParam('proid');
        $collection = Mage::getModel('mymodule/usermeasurement')->getCollection();
        $collection->addFieldToFilter( 'product_id', $proid );
        $collection->addFieldToFilter( 'user_id', $customerId );
        $collection->getFirstItem()->getData();


        foreach ($collection as $collect) {
            $response[]     = array(
            'proid'         => $collect->getProductId(),
            'stnd_size'     => $collect->getStndSize()
            );
        }

        $this->loadLayout();
        $this->getResponse()
                    ->setBody($this->getLayout()
                            ->createBlock('mymodule/measurescreen')
                            ->setData('response', $collect)
                            ->toHtml());
        $this->renderLayout(); 

    }
} 

Block file function is below :

    protected function _construct()
    {
        $this->setTemplate('mymodule/measurescreen.phtml');     
    }

I am not sure how to call block and set and retrieve data if I follow another approach. Can someone please tell me what is wrong in this and why it is showing like this.

2 Answers 2

0
           $this->loadLayout();  
           $this->getResponse()->setBody($this->getLayout()
                               ->createBlock('mymodule/measurescreen')
                               ->setData('response', $collect)
                               ->toHtml()); 
           $this->renderLayout(); 


           protected function _construct()
           {
               parent::_construct();
               $this->setTemplate('mymodule/measurescreen.phtml');     
           }

Try this code please

3
  • Have tried this already. Not working.
    – Geek
    Commented Aug 17, 2017 at 8:16
  • change to _toHtml() Commented Aug 17, 2017 at 8:25
  • It gives error. doesn't work
    – Geek
    Commented Aug 17, 2017 at 8:46
0

Your code should looks like this:

app/code/local/Company/Module/controllers/IndexController.php

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

app/code/local/Company/Module/etc/config.xml

<config>
        ...
        </global>
        <frontend>
        <layout>
           <updates>
               <company_module>
                   <file>company_module.xml</file> //we will define your phtml in this file
               </company_module>
            </updates>
        </layout>
    </frontend>
</config>

app/design/frontend/{packagename}/{modulename}/layout/company_module.xml

<?xml version="1.0"?>
<layout>
    <rooterName_index_index>
        <reference name="root">
            <action method="setTemplate">
                <template>page/1column.phtml</template> //or 2columns-left.phtml, ...
            </action>
        </reference>
        <reference name="content">
            <block type="block/type" name="my.block.page" template="custom/measurescreen.phtml" />
        </reference>
    </rooterName_index_index>
</layout>

app/design/frontend/{packagename}/{modulename}/template/custom/measurescreen.phtml

<div class="wrapper">Good morning</div>
4
  • it is exactly same as you defined.
    – Geek
    Commented Aug 17, 2017 at 8:22
  • Not at all look a little your controller and the mine, please try my solution if you want to solve your problem
    – PЯINCƎ
    Commented Aug 17, 2017 at 8:25
  • I have edited with complete code of indexcontroller of mine. Please check now. I have done what you mentioned already.
    – Geek
    Commented Aug 17, 2017 at 8:42
  • The problem I am facing that to use setTemplate and setData together. Whatever solution I am trying is either displaying perfect layout or my phtml file's content.
    – Geek
    Commented Aug 17, 2017 at 8:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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