How to add bare HTML to a block e.g:

$block = $result->getLayout()->getBlock('category.description');
->toHtml('<p>My Content</p>');

Is there a way to dynamically add content to a block perhaps without use of templates or any other way appreciate?


You should try this

 echo $this->getLayout()->createBlock("Vendor\Module\Block\Myblock")- 
| improve this answer | |
  • I know that Not what I was looking for! however I've found a solution thanks. – Juliano Vargas Aug 1 '19 at 11:15
  • please Upvote. If you appreciate. – KHIMAJI VALUKIYA Aug 1 '19 at 11:18
  • Helpful for others not for me – Juliano Vargas Aug 1 '19 at 11:19

I've found a solution pretty simple.

$block = $result->getLayout()->setData(['test'=> 'Content here'])->getBlock('category.description');

Then my blank.phtml:

<?php print_r($this->getData()) ?>

It prints: Array ( [test] => Content here [module_name] => Magento_Catalog )

| improve this answer | |

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.