Magento 2 Templates: Use `$block` or `$this`?

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.


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.