2

Im trying to pass information from my controller to the template.

My Controller:

    $response= "test"; (variable that I want to pass to PHTML)

    $this->loadLayout( array('default','path_to_test_phtml'));

    $block = $this->getLayout()->createBlock('block_name')
        ->setData('response', $response)
        ->setTemplate('path_to_test.phtml');
    $block->setResponse($response); //magic method
    $this->renderLayout();

My Block:

public function _prepareLayout()
{

    return parent::_prepareLayout();
}

My Template:

<?php
$response = $this->getResponse();
?>
<?php echo $this->response; ?>

My Layout:

<path_to_test_phtml>
        <reference name="content">
            <block type="block/name" name="track_bynumber" template="path_to_test_phtml.phtml"/>
        </reference>
</path_to_test_phtml>

No information "test" appear in the template.. any help appreciated. brgds

2 Answers 2

4
$response = 'value'; //dynamic value to pass in block   

Mage::app()->getLayout()->createBlock('blockname')
    ->setData('area','frontend')
    ->setTemplate('templatepath')
    ->setResponse($response)
    ->toHtml();
4
  • what is the idea to use ->toHtml();? brgds Commented Sep 29, 2015 at 12:26
  • -for output of block Commented Sep 29, 2015 at 12:42
  • 1
    $this->renderLayout(); ?? Commented Sep 29, 2015 at 12:48
  • +1; I needed to get the Mage::app()->getLayout() part outside of a template context. Worked perfectly to load a CMS block. Thanks! Commented May 27, 2016 at 9:47
3

createBlock creates an new instance of your block.
After that it seams you do nothing to it.
renderLayout just renders the blocks instantiated through loadLayout.

I assume you also have an instance of your block declared in the layout files somewhere.

In this case, try:

$block = $this->getLayout()->getBlock('block name here')->setResponse($response)....;
2
  • thanks for your reply. Following your advice I edited the file and now prints "Array" in PHTML instead of "Test" Commented Aug 3, 2014 at 19:51
  • 3
    at least you are getting something now. Commented Aug 3, 2014 at 20:00

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.