diff --git a/Magento2/Sniffs/Templates/ThisInTemplateSniff.md b/Magento2/Sniffs/Templates/ThisInTemplateSniff.md index c214a726..071fada6 100644 --- a/Magento2/Sniffs/Templates/ThisInTemplateSniff.md +++ b/Magento2/Sniffs/Templates/ThisInTemplateSniff.md @@ -6,8 +6,32 @@ In PHTML templates, the current block is available as `$this` and `$block`. The `$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. ## How it works -Any occurence of `$this` in PHTML files (via file pattern in ruleset.xml) raises a warning. +Any occurrence of `$this` in PHTML files (via file pattern in ruleset.xml) raises a warning. ## How to fix -Replace `$this` with `$block`. If you use private or protected methods, make them public. \ No newline at end of file +Replace `$this` with `$block`. If you use private or protected methods, make them public. + +--- + +## Reasoning +The use of helpers is in general discouraged therefore any `$this->helper()` code used in PHTML templates should be refactored. + +Consider using ViewModel instead. + +## How to fix + +Typical example of a helper being used in a PHTML: +```html +helper()->...; ?> +``` + +Once the ViewModel is created, call it in the PHTML as follow: + +```html +getViewModel(); ?> +``` +or +```html +getData('viewModel'); ?> +```