3

I want to pass product as variable from list.phtml to another template file which i will called there using below code

echo $this->getLayout()->createBlock("Magento\Framework\View\Element\Template")->setTemplate("Test_Module::Test/view/config.phtml")->toHtml();

I've found that for Magento 1 we can use below code.

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

And we can access of this variable to that template using below syntax

echo $this->key;

I've tried same for Magento 2 using below code but no luck.

echo $this->getLayout()->createBlock("Magento\Framework\View\Element\Template")->setData('key', 'd')->setTemplate("Test_Module::Test/view/config.phtml")->toHtml(); 

I can see blank page when tried to get value using below code.

echo $this->key;

Anyone have tried similar to it?

7

I tried this it's worked for me

<?php echo $this->getLayout()->createBlock("Magento\Framework\View\Element\Template")->setKey('dfd')->setTemplate("Test_Module::Test/view/config.phtml")->toHtml(); ?>

to get value

$block->getKey();

also change your method will work too

 echo $this->key;

to

echo $this->getKey();
| improve this answer | |
  • Its work but i've problem if block is different rather than Magento\Framework\View\Element\Template. I have another methods on block which i called there. – Kul Apr 26 '17 at 10:52
  • 1
    @Kul it doesnot matter just use $this->getKey() in your block it will work too. – Qaisar Satti Apr 26 '17 at 10:54
  • Great Its work. I've another issue with block that's why it doesn't work first time. – Kul Apr 26 '17 at 11:27

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.