Pass a variable to a template .phtml block in Magento

Here’s a simple example to pass a variable from Magento (template, controller, etc) file to a block with php code.

Call a block using the createBlock() method:

<?php
echo $this->getLayout()->createBlock('module/template_name')
->setData('key', 'some value')
->setTemplate('module/newblock.phtml')->toHtml(); 

The setData() method in the above code creates a variable, and you can access the variable with below code in the code that handles the block you created:

<?php
echo $this->key;

Search within Codexpedia

Custom Search

Search the entire web

Custom Search