12

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?

asked Mar 23, 2016 at 19:02

4 Answers 4

23

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').

zhartaunik
3,8664 gold badges25 silver badges54 bronze badges
answered Mar 24, 2016 at 0:25
2
  • 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> Commented Aug 27, 2019 at 13:19
  • I have used your code but I am getting only body part not header and footer call Commented Sep 24, 2019 at 6:21
4

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();
answered May 11, 2018 at 13:27
1

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)

zhartaunik
3,8664 gold badges25 silver badges54 bronze badges
answered May 4, 2017 at 13:29
1
  • I'm sorry but i don't understand what you mean. Can you explain? Commented Jun 27, 2017 at 10:32
0

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.

answered Mar 23, 2016 at 19:13
4
  • I'm using it inside block class (Company\Module\Block\Hello.php) so I need to use $this. Variable $block doesn't exist there so if I try to use it I will get error Notice: Undefined variable: block. Commented Mar 23, 2016 at 19:42
  • Oh, sorry, I understood you were using in a template file. Commented Mar 23, 2016 at 19:43
  • Did you try using $my_arg as parameter in you constructor and checking it? Commented 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 wrong Commented Jun 18, 2020 at 4:50

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.