I have this controller:

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

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)


There are a couple of ways to do this.

Assign directly to the Block:




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


Use the Mage registry:

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

and then use it like:

$var = Mage::registry('custom_var');
  • 2
    you have used $block->assign($var) but how would be retrieved in phtml file? – Mohammad Faisal Jan 18 '14 at 11:41
  • You would access via $this->myvar from within the template file – philwinkle Jan 21 '14 at 13:51
  • 1
    I believe it's Mage::register($key, $value, ...). So, Mage::register('custom_var', $var)? – musicliftsme Jan 8 '15 at 17:15
  • 4
    It should be $block->assign('myvar',$var);, shouldn't it? – fritzmg Jun 28 '16 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? – Butterfly Nov 30 '17 at 11:09

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

Then you can get the value in the phtml file like

$value = $this->getSomething();
$value = $this->getData('something');
  • this doesn't work for me :(, I have a controller which returns the product id through AJAX, for quickview of products. – Shathish Sep 16 '13 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 Sep 16 '13 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 Sep 16 '13 at 9:32
  • @shathish maybe you would ask an other question and hope it doesn't get marked as duplicate. – Marius Sep 16 '13 at 9:52

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;


  • 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. – Fabian Blechschmidt Sep 16 '13 at 6:16

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

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


   <h1>You're inside your phtml file...... <?php echo $variable;  //prints "something" ?></h1>
  • 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. – Fabian Schmengler Apr 8 '16 at 15:43

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.


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, privacy policy and cookie policy

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