1

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

$block = $result->getLayout()->getBlock('category.description');
$block->setTemplate('Vendor_Module::myTemplate/blank.phtml')
->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?

2 Answers 2

3

You should try this

 echo $this->getLayout()->createBlock("Vendor\Module\Block\Myblock")- 
>setTemplate("My_Module::history-suite.phtml")->toHtml();
3
  • I know that Not what I was looking for! however I've found a solution thanks. Commented Aug 1, 2019 at 11:15
  • please Upvote. If you appreciate. Commented Aug 1, 2019 at 11:18
  • Helpful for others not for me Commented Aug 1, 2019 at 11:19
2

I've found a solution pretty simple.

$block = $result->getLayout()->setData(['test'=> 'Content here'])->getBlock('category.description');
$block->setTemplate('Vendor_Module::myTemplate/blank.phtml')
->toHtml();

Then my blank.phtml:

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

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.