Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Magento2.Templates.HelperInTemplate Rule #121

Copy link
Copy link
Closed
@diazwatson

Description

@diazwatson
Issue body actions

After the work done in #108 I kept wondering:

  • Why we include this rule in ThisInTemplateSniff?
  • Why not create a separate HelperInTemplateSniff to check for the use of helpers in PHTML files?
  • Should we also explain how to create a ViewModel or is enough mentioning how to use it in the template?
  • How about adding a link to the DevDocs to explain how to create the viewModel?

This is a proposal to answer those questions.

Rule

The use of helpers is in general discouraged. For PHTML, consider using ViewModel instead.

Reason

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.

Implementation

Typical example of a helper being used in a PHTML:

<?php $_incl = $block->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'); ?>
ihor-sviziev

Metadata

Metadata

Assignees

No one assigned

    Labels

    acceptedNew rule is acceptedNew rule is acceptedproposalNew rule proposalNew rule proposal

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      Morty Proxy This is a proxified and sanitized view of the page, visit original site.