How to Use Magento 2 Blocks, Layouts, and Templates

In this guide, I’m going to explain the concepts of Magento 2 blocks, layouts, and templates and how to use them.

The xml file and structure represented by two types of hierarchy of elements that are block and containers are called layout.

The PHTML (PHP) files which are used to add or customize the front page of a store are called templates. Layout and themes are used to build a theme in Magento 2.

The PHP classes that are used to connect or create a link between layout and templates are called Blocks. Here’s an example how the block looks like:

class: The attribute which used to define the class of block for the template file.

name: The attribute which is used to identify a block.

template: Used to define the source and name of PHTML file which is used to customize the front page.

Now let’s implement the blocks, layouts, and templates in Magento 2!

Enable Template Path Hints

Open the Admin panel of your store and navigate to STORES →  Configuration → ADVANCED → Developer. Then unfold the Debug section and select Yes from Enabled Template Path Hints for Storefront drop down. Now just click on Save Config.

enable template hints

After enabling the template hints, go to the homepage where you can see the template hints:

search hint

Here, you can identify the location of the Search Box:


Now you have to create a custom theme to use blocks, layouts, and templates in Magento 2.

Create Directories

Create the directories in the same way as shown below:


Theme Configuration and Registration

To configure the theme, create theme.xml in app/design/frontend/Magenticians/Mytheme and paste the following code in it:

Note: Don’t forget to upload the theme preview image (preview.jpg) in app/design/frontend/Magenticians/Mytheme/media.

To register the theme, create registration.php in app/design/frontend/Magenticians/Mytheme and paste the following code in it:

Run Commands

Launch the SSH terminal and run the following commands in your Magento 2 root directory:

Apply the Custom Theme

Navigate to CONTENT → Configuration from the Admin panel of your store, then click on Edit from your selected option in which you want to apply your custom theme.

edit theme

Now select Mytheme from Applied Theme drop down and click on Save Configuration.

apply theme

Add Layout File

Now create default.xml in app/design/frontend/Magenticians/Mytheme/Magento_Search/layout and paste the following code in it:

Add Template File

Now create customline.phtml in app/design/frontend/Magenticians/Mytheme/Magento_Search/templates and paste the following code in it:

Note: At last, run all the commands again which I have executed before.

Go to the homepage of Magento 2 and you will see the expected output:


Final Words

After following this guide, you should now be able to use Magento 2 Blocks, Layouts, and Templates. Explore more block, layouts, and templates to customize your Magento 2 store according to your need if you want. And if you want to discuss and have any confusions regarding this guide, simply use the comment box below!

About Author

Syed Muneeb Ul Hasan is an expert in PHP and Magento, he prefers to educate users in implementing and learning Magento. When not working, he loves to watch cricket.