28

In Magento 2, the $this variable no longer refereces to a template's block object. It refers to a template class

 Magento\Framework\View\TemplateEngine\Php

However, this template class has a passthrough __call method

#File: vendor/magento/framework/View/TemplateEngine/Php.php
public function __call($method, $args)
{
    return call_user_func_array([$this->_currentBlock, $method], $args);
}

Which ensure any method calls make it through to the actual block. This also explains why you can't call protected methods from phtml templates.

In addition to this though, every (I think?) template has a variable named $block populated, which also refers to the parent block object. You can see this in use in Magento's list template

#File: vendor/magento/module-catalog/view/frontend/templates/product/list.phtml
//...
$_productCollection = $block->getLoadedProductCollection();

where the $block variable is used, but never explicitly defined.

Other than the differences mentioned above, is there any difference between using one technique over the other? i.e. Do both $block and $this->currentBlock refer to the same object?

32

In a template file, $block and $this->_currentBlock are the same thing.

Take a look at the render method in the Php.php template engine.
Before including the template file, this happens: $this->_currentBlock = $block; then the template file is included using a simple include $fileName;.
This means that inside a template you are still inside the Php template engine. That's why $this works, and that's why $block is defined.

[EDIT]
I just found out that using $this inside templates is discouraged.
The php sniffer with the EcgM2 standards shows a warning when using $this inside templates.
Replacing $this with $block makes the warning go away.

  • So which is the default object in .phtml file $block or $this. – Manish Goswami Jan 25 '19 at 18:54
  • What do you mean by default? – Marius Jan 25 '19 at 21:48
  • What is the default variable used in a template to access the template's rendering class? Is there any difference between of these two to access function ? – Manish Goswami Jan 26 '19 at 4:47
  • It says in ny answer. Both work but it is recommended to use $block – Marius Jan 26 '19 at 6:06
5

As far as I've seen they both are almost the same in the PHP TemplateEngineInterface, but remember that in Magento2 you can create custom template engines. Not like Magento1 in which you just had phtml files.

If you see the TemplateEngineInterface you cannot see any reference to any method calling the currentBlock, so I think the $this->currentBlock approach is PHP template specific.

But in order to be independent from the template engine, $block should be the correct approach.

I mean that, from theoretical point of view, you could create a different PHP based engine that could not rely on phtml files, but maybe on some other PHP structure.

I assume $block as the "right" way for "phtml" mode because it is explicitly passed as context variable.

$block and $this->currentBlock are the same from a technical point of view, but if they explicitely used $block I think we should use it, from a code-styling point of view.

  • But neither is $block part of the interface. I'd say it's safe to assume that this implementation is used when you are inside a phtml file. Other implementations won't use the phtml file. – Fabian Schmengler Feb 24 '16 at 7:08
  • You are right, I've not been clear in my answer to explain what I mean. I modified my post with more details. – Phoenix128_RiccardoT Feb 24 '16 at 7:33

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.