Home >2+ Best Solution To Set The Template Used By A Block In Magento 2

2+ Best Solution To Set The Template Used By A Block In Magento 2

Hello everyone,

Welcome back to our Magento 2 tutorial!

In this tutorial, we will show you the best ways to set the template used by a block.

With this easy-to-follow guide, we hope that you can get more knowledge on your own.

Why Do You Need To Set Templates For Block?

First of all, you need to know the definition of block in Magento 2.

A block is a unit of page output that renders some distinctive content (such as a user interface element or a piece of information) for the end-user.

In Magneto 2, blocks are a foundational building unit for layouts. They are the link between a PHP block class (which contains logic) and a template (which renders content). 

Blocks can have children, and their children can have children, and so on. Using the <arguments/> child node, information will be passed from layout XML files to blocks.

Some examples of blocks are a category list, product listing, product tags and a mini cart.

Blocks use their templates to generate HTML.

That means the template used by a block represents the functionality of the block that the attribute is assigned.

If the attribute is omitted, the block can not render any output unless the block class has the $_template property defined correctly.

In other words, templates define how the content of the blocks is presented on a page.

In most cases, templates do not contain any logic about whether they will or will not be rendered. This is typically handled by the layout files.

In Magento, the default templates are PHTML files.

2 Methods To Set The Template Used By A Block

There are two methods to set the template for a block in Magento 2:

  • Using template properties
  • Using <argument>

For more details, read the content below.

Using template properties

You can use the following code:

<referenceBlock name="blockname" template="Vendor_Module::new-template.phtml" />

For example, we want to create a new template file for the block copyright.

Step 1: Create the new-template.phtml file.

app/code/Vendor/Module/view/frontend/templates/new-template.phtml 

Step 2: Add the XMLcode below in the default.xml file.

<referenceBlock name="copyright" template="Vendor_Module::new-template.phtml" />

Step 3: Flush cache và check the result.

Using <argument>

You can use the following code:

<referenceBlock name="blockname">

   <arguments>

     <argument name="template" 
xsi:type="string">Vendor_Module::new-template.phtml</argument>

   </arguments>

 </referenceBlock>

Using this method, you can get the same result as using the method above.

You can find the link to the new-template.phtml template in the folder module (<module_dir>/view/<area>/templates) or the folder theme (<theme_dir>/<Namespace_Module>/templates).

Note: The template value that is set as block property has higher priority in the process of creating a layout, compared to the value of <argument>. 

That means if a certain block has a template set as its property, then the template value will overwrite the <argument> value of that block.

Conclusion

I hope that this is the perfect guide for setting the template used by a block.

Please Like, Share and Comment to let us know your opinion or any suggestions for this article. 

 

< Previous Post
+ posts