In PHTML templates, the current block is available as $this
and $block
. The alternative with $this
has been deprecated and should not be used anymore.
$this
in templates is a legacy from Magento 1. It still works, however this can change any time, should templates and blocks be further decoupled. That's why for new code you should always use $block
and restrict it to public methods.
Any occurrence of $this
in PHTML files (via file pattern in ruleset.xml) raises a warning.
Replace $this
with $block
. If you use private or protected methods, make them public.
The use of helpers is in general discouraged. For template files, consider using a ViewModel instead.
The use of helpers is in general discouraged therefore any $this->helper(<helper_class>)
code used in PHTML templates should be refactored.
Consider using ViewModel instead.
Typical example of a helper being used in a PHTML:
<?php $_incl = $this->helper(<helper_class>)->...; ?>
Once the ViewModel is created, call it in the PHTML as follow:
<?php $viewModel = $block->getViewModel(); ?>
or
<?php $viewModel = $block->getData('viewModel'); ?>