I have used two template for homepage(template inside another template) I am calling product template inside category template. Now my case scenario is

1. I am declaring one array in category template

2. I want to pass this empty array to product template.

3. Add elements in that array in product template.

4. Pass the array back to category template.

How can i do that.

Let me know if there is any confusion.

  • 1
    You can add these templates inside one block and then you can write your logic in a single block class, You can access the function from both the templates. – Mitro Aug 6 '19 at 10:23
  • Can u give me example through code? – Nafsss Aug 6 '19 at 10:32
  • Can you tell me your templates rendering code So that I can have an idea what you actually want to do? – Mitro Aug 6 '19 at 10:36
  • Is there any way to access variable declared in one phtml file into another? – Nafsss Aug 6 '19 at 10:58

Try this it's worked for me.

<?php echo $this->getLayout()->createBlock("Magento\Framework\View\Element\Template")->setKey('test')->setTemplate("Vendor_Module::test.phtml")->toHtml(); ?>

to get value use

echo $this->getKey();
| improve this answer | |

As per your actual requirement, in first phtml file add below code :


echo $this->getLayout()->createBlock("Vendor\Modulename\Block\Display")->setData('var1', $arr)->setTemplate("Vendor_Modulename::test2.phtml")->toHtml();

In second phtml i.e. test2.phtml add



foreach($catIds as $catId){ 

        foreach ($_productCollection as $_product){ 
                if (!in_array($currentId, $arr)){ 
                        echo $_product->getData("name");echo "<br/>"; 


In app/code/Vendor/Modulename/Block/Display.php add below code:

    protected $_productCollectionFactory;
    protected $_categoryFactory;

    public function __construct(
        \Magento\Catalog\Model\CategoryFactory $categoryFactory,
        \Magento\Framework\View\Element\Template\Context $context
    ) {
        $this->_categoryFactory = $categoryFactory;
        $this->_productCollectionFactory = $productCollectionFactory;
    public function setCustomValue($value) 
        $categoryId = $value; 
        $category = $this->_categoryFactory->create()->load($categoryId); 
        $collection = $this->_productCollectionFactory->create(); 
        $collection->addAttributeToFilter('visibility', \Magento\Catalog\Model\Product\Visibility::VISIBILITY_BOTH); 
        $collection->addAttributeToFilter('status', \Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED); 
        return $collection; 
| improve this answer | |
  • what if i want to fetch this value in another phtml? – Nafsss Aug 6 '19 at 11:44
  • To fetch value in phtml, use this code: print_r($block->getData("var1")); – Mona Mehta Aug 6 '19 at 11:49
  • Thanx. This worked. now how can i append more values in this array in other phtml itself. inshort how can i append in this $block->getData("var1") – Nafsss Aug 6 '19 at 11:59
  • Do you mean that you want to pass this value again to another phtml? If so, then you can directly pass the variable to required phtml – Mona Mehta Aug 6 '19 at 12:02
  • i want to append more elements in this array and then again pass this array back to the first phtml file – Nafsss Aug 6 '19 at 12:03

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.