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?

4 Answers 4

22

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

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> Aug 27, 2019 at 13:19
  • I have used your code but I am getting only body part not header and footer call
    – Jinesh
    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();
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)

1
  • I'm sorry but i don't understand what you mean. Can you explain?
    – zitix
    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.

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.
    – zitix
    Mar 23, 2016 at 19:42
  • Oh, sorry, I understood you were using in a template file. Mar 23, 2016 at 19:43
  • Did you try using $my_arg as parameter in you constructor and checking it? 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 Jun 18, 2020 at 4:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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