23

I have this controller:

...
public function insertAction() {
    $something = 'testcase';
    $this->loadLayout();
    $this->_title($this->__("the title"));
    $this->renderLayout();
}
...

I would like to access $something in my .phtml document, how do I do this? (or to put it in another way: how can I access $something in my .phtml file)

5 Answers 5

22

There are a couple of ways to do this.

Assign directly to the Block:

$block->assign($var);

or

$block->assign(array('myvar'=>'value','anothervar'=>true));

Then you can access it in the phtml file like this:

$this->myvar

Use the Mage registry:

Mage::register('custom_var', $var);

and then use it like:

$var = Mage::registry('custom_var');
5
  • 2
    you have used $block->assign($var) but how would be retrieved in phtml file? Commented Jan 18, 2014 at 11:41
  • You would access via $this->myvar from within the template file
    – philwinkle
    Commented Jan 21, 2014 at 13:51
  • 1
    I believe it's Mage::register($key, $value, ...). So, Mage::register('custom_var', $var)?
    – laketuna
    Commented Jan 8, 2015 at 17:15
  • 4
    It should be $block->assign('myvar',$var);, shouldn't it?
    – fritzmg
    Commented Jun 28, 2016 at 15:09
  • @philwinkle Does Mage::register('custom_var', $var); write the value in session and this can be accessible from other template file except current action template as well?
    – Anto S
    Commented Nov 30, 2017 at 11:09
19

Your phtml must be rendered by a block. The block must have a name in the layout.
You can do this after calling $this->loadLayout();

$block = Mage::app()->getLayout()->getBlock('block_name_here')
if ($block){//check if block actually exists
   $block->setSomething($something);
}

Then you can get the value in the phtml file like

$value = $this->getSomething();
//or 
$value = $this->getData('something');
4
  • this doesn't work for me :(, I have a controller which returns the product id through AJAX, for quickview of products.
    – Shathish
    Commented Sep 16, 2013 at 5:44
  • @Shathish. This is a different issue. You can rend the response as JSON and use the values in the AJAX response handler.
    – Marius
    Commented Sep 16, 2013 at 8:43
  • Yes I am using JSON to return the block (something.phtml) content and update the div with these details in list page, how can I pass the variable from my controller to something.phtml? actually I do this currently using $block->assign() method, which I feel isn't the right way (as it is tight coupling - correct me if i'm wrong) or shall I post this as a question?
    – Shathish
    Commented Sep 16, 2013 at 9:32
  • @shathish maybe you would ask an other question and hope it doesn't get marked as duplicate.
    – Marius
    Commented Sep 16, 2013 at 9:52
3

In case you people missed there is one more way to get this done

using sessions

Mage::getSingleton('core/session')->setSomeSessionVar($data);// In the Controller
$data = Mage::getSingleton('core/session')->getSomeSessionVar(); // In the View;

source

1
  • Even if you has the data in the session to use it later, I recommend to use $block->setSomething() or $block->assign(). This feels like "too much code" and it should be as much php as possible avoided. Commented Sep 16, 2013 at 6:16
-1

If you're within your block controller.

class module_namespace_Block_example extends Mage_Core_Block_Template 
{
      protected $_var;

     public function __construct(){

         $this->_var = "something"; 


     }
}

Then in your .phtml file

   <?php 
        $variable = $this->_var;
        echo $variable; // prints "something"

   ?>

   <h1>You're inside your phtml file...... <?php echo $variable;  //prints "something" ?></h1>
1
  • What you call "block controller" is just called a "block" in Magento. The question was about controller actions found in the controllers directory of a module. Commented Apr 8, 2016 at 15:43
-4

this is very easy to send data from controller to phtml file.

Step1:- Firstly create the Model class and extends from Varien_Object

class Namespace_Modulename_Model_Modelfilename extends Varien_Object
{

}

step2:- Now Open the contollerfile and put the code in the function.

$name='gaurav';
$this->loadLayout();
Mage::getSingleton('Modulename/Modelfilename')->setData('name',$name);
$this->renderLayout();

Step3:- Open the phtml file and put the code.

echo $name=Mage::getSingleton('Modulename/Modelfilename')->getData('name');

Output:- 'gaurav';

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.