This is a change from Magento 1.x where a template can access the block by the $this
variable.
Let’s compare, for example, the standard catalog/product/list
templates.
Magento 1.x:
Magento 2:
The $this
variable is also available, but now it is a reference to a \Magento\Framework\View\TemplateEngine\Php
instance
As you can see, a template also has the following local variables:
-
$fileName
— its own file name
-
$tmpBlock
— the parent block
A template has also has the local variables extracted from a $dictionary
, it is set from the block’s _viewVars
property
You can make a local variable available in a template by calling the \Magento\Framework\View\Element\Template::assign
method: