I want to pass object as a argument from phtml file how can I do this? As I've checked we can pass string using
$block->setData('var','test') but didn't found for object.
Even if this post is 3 years old I'll answer it in case someone needs the solution.
WARNING: you should never use
getLayout()->createBlock, the correct way would be creating a new
<block> inside the layout.xml. Also, you should never use ObjectManager as done in this answer, but it's the only way that I found working with the
This is the solution (inside your .phtml):
<?php // Get an instance of the class to pass to the block as argument $obj = Bootstrap::getObjectManager()->get(ObjectClassToPassToBlock::class); ... // Create the block passing it the object inside the param "argumentKey" $block = $this->getLayout()->createBlock( Block::class, 'block_name', [ 'data' => ['argumentKey' => $obj] ] ); ?> <?= $block->toHtml(); ?> ...
Doing like this you will create a block with name
block_name and class
Block::class and you will pass it an argument of type
If you want to set the arguments of the block using the
$block->setData instruction you can do like this: