Skip to content

Magento2.Templates.HelperInTemplate Rule #121

Closed
@diazwatson

Description

@diazwatson

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'); ?>

Metadata

Metadata

Assignees

No one assigned

    Labels

    acceptedNew rule is acceptedproposalNew rule proposal

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions