Magento 2 Set Template in Block File

Magento 2 Set Template in Block File: 2 Best Solutions

by Lily Tran

Hello everyone,

Welcome back to our Magento 2 tutorial!

In this tutorial, we will show you the best ways for Magento 2 set template in block file

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

Why Do You Need To Set Template Magento 2 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.

Magento 2 Set Template in Block File in 2 Ways

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

  • 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.

>>> You may want to know: How to create custom layout in Magento 2

Conclusion

I hope that this is the perfect guide on Magento 2 set template in block file.

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

Next Reading Suggestions

© 2019 BSS Commerce owned by THANH CONG INTER ., JSC. All Rights Reserved.
Business registration certificate no. 0106064469 issued by Hanoi Department of Planning and Investment on 19 December 2019.
Legal Representative: Mr. Nguyen Quang Trung.