0

I have created a custom module. In that I have a custom controller and an action

public function reviewAction()
{

}

Now when I hit the url www.somesite.com/modulename/controllername/review then it is running properly. Now I had to create a custom page for this url. What I did is added this code in my module config.xml file

  <layout>
        <updates>
            <mcreview>
                <file>mcreview.xml</file>
            </mcreview>
        </updates>
    </layout>

and then created mcreview.xml file at

/app/design/frontend/base/default/layout/mcreview.xml

Added following code in above file

<mcreview_index_review>
    <reference name="root">
        <action method="setTemplate"><template>page/1column.phtml</template></action>
    </reference>
    <reference name="content">
        <block type="mcreview/data" name="magecomp.mcreview.data" template="mcreview/review.phtml" />
    </reference>
</mcreview_index_review>

and also created review.phtml file. Then in my function added the following code

 public function reviewAction()
  {
     $block = $this->getLayout()->createBlock('core/template')
      ->setTemplate('mcreview/review.phtml');
    $this->getResponse()->setBody($block->toHtml());
    $this->loadLayout();
    $this->renderLayout();
  }

Now when I load the url the content of my review.phtml file comes at top, even above the header. It should come in the body of the page. Please any one can tell me what I am missing or what should I add in my code so that my review.phtml file contents are shown in the body of the page after head section.

0

If you use the layout (xml) to define the content of the page, you don't have to create programmatically your block in the controller action.

your controller action function should be

  public function reviewAction()
  {
    $this->loadLayout();
    $this->renderLayout();
  }

Is the xml that define which blocks will be rendered in the page.

edit after comments.

In your controller you are creating the block based on type "core/template" in the xml you set the block type to "mcreview/data". try to set your block type in xml to :

<block type="core/template" name="magecomp.mcreview.data" template="mcreview/review.phtml" />

after this test if you need a custom block type you need to define your custom block class


5
  • Now the content of phtml file is neither shown at the top nor anywhere else Jan 16 '18 at 12:28
  • do you see a blank page or the theme one without content ?
    – Giuseppe
    Jan 16 '18 at 13:51
  • The theme one with header and footer, only body is missing. In place of body I want to show the content of my .phtml file. But my .phtml file content is showing above header. If any doubts then please ask. Jan 16 '18 at 13:55
  • I've edited my answer to add the block type part.
    – Giuseppe
    Jan 16 '18 at 16:01
  • changing block type to core/template solved my problem. Jan 17 '18 at 6:17
0

Since you declared your block in content it should be appear in content but your are missing in a controller, it should be like this:

public function reviewAction()
{
    $this->loadLayout();
    $this->renderLayout();
}

Now if you want to add some block wthout the xml declaration, you can do this:

public function reviewAction()
{
    $this->loadLayout();
    $block = $this->getLayout()->createBlock('Mage_Core_Block_Template', 'block_name1')->setTemplate('mcreview/review.phtml');
    $this->getLayout()->getBlock('content')->append($block);
    $this->renderLayout();
}

But the first solution is the best one and it should work !

Debug: To check if your page/1column.phtml is missing or not, you can try this:

public function reviewAction()
{
    $this->loadLayout();
    $this->renderLayout();
}

xml

<mcreview_index_review>
    <reference name="root">
        <action method="setTemplate"><template>page/1column.phtml</template></action>
        <block type="mcreview/data" name="magecomp.mcreview.data" template="mcreview/review.phtml" />
    </reference>
</mcreview_index_review>

1column.phtml

<?php echo $this->getBlockHtml('magecomp.mcreview.data'); ?>

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.