Skip to content

[New Rule] Do not use setTemplate in Block classes #31

Open
@lenaorobei

Description

@lenaorobei

Background

When creating a Block class, a Block class could set its PHTML template in multiple ways: Through XML layout, through a call to $this->setTemplate() and through a variable $_template. The new design of Block classes is to configure them at constructor time, meaning that configuration options (like the template) are added using constructor arguments. This allows for the XML layout to change the template. The template in the Block class is then only defined as a default value, if the XML layout is not overriding the template: This default value is best defined via a protected variable $_template.

Reason

If $this->setTemplate() is used instead, this could lead to potential issues: First of all, setters are deprecated in Block classes (because constructor arguments should be preferred instead). Second, if $this->setTemplate() is added to the constructor after calling upon the parent constructor, it would undo the configuration via XML layout. Simply put: It is outdated and leads to issues quickly.

Implementation

ExtDn SetTemplateInBlockSniff.

Metadata

Metadata

Assignees

No one assigned

    Labels

    need to discussRule requires discussionnew ruleNew feature implementationproposalNew rule proposal

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions