Set and get block arguments programatically
If you look further in the code, in the \Magento\Framework\View\Layout\Generator\Block
class, the createBlock
function only adds data from the $arguements['data']
element. So, I think you should change your code to this:
$block = $this->frameworkViewLayout
->createBlock(
"Company\Module\Block\Hello",
"block_name",
[
'data' => [
'my_arg' => 'testvalue'
]
]
)
->setData('area', 'frontend')
->setTemplate($template)
->toHtml();
Then you could use getMyArg()
or getData('my_arg')
.
You can assign directly any variable like,
$block = $this->_layout
->createBlock('Magento\Framework\View\Element\Template')
->setTemplate('Company_Namespace::index.phtml')
->setResponse('response')
->toHtml();
return $block;
and to get variable value in temlate file,
echo $block->getResponse();