In layout when I create a block I can set custom arguments, for example:
<block class="Company\Module\Block\Hello" name="block_name" template="test.phtml">
<arguments>
<argument name="my_arg" xsi:type="string">testvalue</argument>
</arguments>
</block>
I can later retrieve the argument like this inside the block:
$arg = $this->getMyArg();
When I create a block programatically (according to this method: link) I can set custom arguments like this:
$block = $this->frameworkViewLayout
->createBlock(
"Company\Module\Block\Hello",
"block_name",
['my_arg' => 'testvalue']
)
->setData('area', 'frontend')
->setTemplate($template)
->toHtml();
The block is correctly created and it works perfectly. But I'm not sure how I can later retrieve the argument inside block's class. I tried to do it like this:
$arg = $this->getMyArg();
or
$arg = $this->getData('my_arg');
But it returns nothing. What did I miss?
4 Answers 4
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').
-
What if arguments in array How to convert if argument is in array like <block class="Company\Module\Block\Hello" name="block_name" template="test.phtml"><arguments> <argument name="jsLayout" xsi:type="array"> <item name="components" xsi:type="array"> <item name="msp-recaptcha" xsi:type="array"><item name="component" xsi:type="string">MSP_ReCaptcha/js/reCaptcha</item> <item name="zone" xsi:type="string">create</item> </item> </item> </argument> </arguments>paras sakariya– paras sakariya2019年08月27日 13:19:14 +00:00Commented Aug 27, 2019 at 13:19
-
I have used your code but I am getting only body part not header and footer callJinesh– Jinesh2019年09月24日 06:21:22 +00:00Commented Sep 24, 2019 at 6:21
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();
note when sending 'data' array with arguments like mentioned by Jackie here:
$block = $this->frameworkViewLayout
->createBlock(
"Company\Module\Block\Hello",
"block_name",
[
'data' => [
'my_arg' => 'testvalue'
]
]
)
->setData('area', 'frontend')
->setTemplate($template)
->toHtml();
avoid injecting $data variable in block definition (__construct method)
-
I'm sorry but i don't understand what you mean. Can you explain?zitix– zitix2017年06月27日 10:32:46 +00:00Commented Jun 27, 2017 at 10:32
I'm quite new to Magento 2, but try using $my_arg or $block->getMyArg().
You should avoid using $this, you should use $block.
Calling $this will activate a proxy method to $block class, but I've had problems with magic methods like getters.
-
I'm using it inside block class (
Company\Module\Block\Hello.php) so I need to use$this. Variable$blockdoesn't exist there so if I try to use it I will get errorNotice: Undefined variable: block.zitix– zitix2016年03月23日 19:42:32 +00:00Commented Mar 23, 2016 at 19:42 -
Oh, sorry, I understood you were using in a template file.Phoenix128_RiccardoT– Phoenix128_RiccardoT2016年03月23日 19:43:46 +00:00Commented Mar 23, 2016 at 19:43
-
Did you try using $my_arg as parameter in you constructor and checking it?Phoenix128_RiccardoT– Phoenix128_RiccardoT2016年03月23日 19:49:40 +00:00Commented Mar 23, 2016 at 19:49
-
@zitix How does that possible, in your xml it clearly says
template="test.phtml". So you are using it in the template, correct me if I am wrongDuke– Duke2020年06月18日 04:50:50 +00:00Commented Jun 18, 2020 at 4:50