Skip to content

Add documentation for ThisInTemplateSniff #15

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 23, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions Magento/Sniffs/Templates/ThisInTemplateSniff.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Rule: Do not use `$this` in templates
## Background
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.

## Reasoning
`$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.

## How to fix

Replace `$this` with `$block`. If you use private or protected methods, make them public.