Magento Stack Exchange is a question and answer site for users of the Magento e-Commerce platform. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question
up vote 11 down vote accepted

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.

share|improve this answer

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.

share|improve this answer
    
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. – fschmengler Feb 24 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. – RiccardoT Feb 24 at 7:33

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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