Search code examples
magentoblockphp

Pass a variable to a template .phtml block in Magento


This code is write in market.phtml

<?php echo $this->getLayout()->createBlock('core/template')->setData('vendorId',$vendor->getCustomerId())->setTemplate('marketplace/vendors/badge.phtml')->toHtml();?>

In Badge.php

echo $this->vendorId;

But my output is null. Is this correct way to pass data to block?


Solution

  • You need to change your variable like this and check it

    <?php echo $this->getLayout()->createBlock('core/template')->setVendorId($vendor->getCustomerId())->setTemplate('marketplace/vendors/badge.phtml')->toHtml();?>
    

    Now you can access this vendor ID variable in badge.phtml file like this :

    <?php echo $this->getVendorId();?>