Sharing some useful tips, solutions and notes for Geeks.

Tuesday, October 31, 2017

Passing custom variable from Magento phtml to block template

For passing variable or data from a controller or phtml file to template block, we can use setData method.

We can then receive it in the corresponding block section and use accordingly.

How to pass:

<?php echo $this->getLayout()->createBlock('page/html/featured')->setData('catid', 112)->setTemplate('page/html/featured.phtml')->toHtml(); ?>

Here see the setData method. It passes custom variable called catid to featured block.

How to receive and utilize:

1. $this->catid
2. $this->getData("catid")

Use any one of the above to receive it and process your requirement.

from cms page

Kudos

No comments: