How to Use Magento 2 Layouts, Blocks, and Templates

by Fayyaz Khattak  March 13, 2017

Layouts and templates build the blocks of a theme in Magento 2. Layouts are the XML files that specify the overall structure of a page like the position of header, side columns, and footer, etc. Templates are pieces of code in PHTML (PHP) files which add features and contents that you see on the front page.

Magento 2 Layout

To give you an idea of how layouts and templates look like, you can go and check the Magento 2 default theme files at $MAGENTO2_ROOT/app/design/frontend/Magento/Theme/view/frontend/ or $MAGENTO2_ROOT/vendor/magento/module-theme/view/frontend/.

Blocks are PHP classes which create a link between the layouts and templates. Blocks are defined using <block> element in layout’s XML file:

Now, let’s see its attributes in detail:

class – It is the required attribute and defines the class of block for the template file.

name – This is also the required attribute and is used to identify a block as a reference.

template – This is used to define the link of a PHTML file where we write our HTML or PHP code.

In order to know how Magento 2 layouts, blocks, and templates work, let’s try to add a sample text somewhere on the Magento 2 homepage. We’ll add it next to the search box in the header.

To do this, we need to add a new block below the search box and create a new template PHTML file to output our sample text.

Enable Template Path Hints

First, we are going to find the template path for the search box with the help of Template Path Hints in Magento 2. By turning it on, you will see many red color template paths once you browse the Magento 2 homepage.

Magento 2 Block Template

You can now easily identify that the search box comes from $MAGENTO2_ROOT/vendor/magento/module-search/view/frontend/templates/form.mini.phtml template.

To enable Template Path Hints, go to Magento 2 Admin Panel -> STORES -> Configuration -> ADVANCED -> Developer -> Debug -> Enabled Template Path Hints for Storefront and change it to Yes. Once you are done, hit the Save Config button and now you should be able to see the changes on the frontend.

Create & Register Theme

Now we need to create the required files/folders and register a new Magento 2 theme so that we can accomplish this job. Under the $MAGENTO2_ROOT/app/design/frontend/ directory, create a new folder with your theme Vendor’s name and under your Vendor folder, create a new directory which will act as your Theme folder. I chose Cloudways as Vendor name and Mytheme as the Theme name.

After creating the directory structure, we will now declare and register our theme by adding theme.xml and registration.php so that Magento 2 registers the theme’s existence.

Create the theme.xml file under $MAGENTO2_ROOT/app/design/frontend/Cloudways/Mytheme/ and add the code below to it:

In the code above, we have a <preview_image> element that declares a theme’s image. It’s a thumbnail image in the Magento 2 Admin panel that gives us a preview of how the theme looks like. It is located in $MAGENTO2_ROOT/app/design/frontend/Cloudways/Mytheme/media/preview.jpg. Make sure you have this image in the correct location. If Magento 2 doesn’t find this file, you will get an error.

To register the theme, create the registration.php file in $MAGENTO2_ROOT/app/design/frontend/Cloudways/Mytheme/ and use the following code in it:

After creation of all these files and folders, use the following CLI commands to register the theme in the Magento 2 system:

Apply The Theme

Once you have added your theme files, you can apply it to your Magento 2 store using these simple steps:

  1. Login to your Magento 2 admin panel.
  2. Go to CONTENT -> Configuration.
  3. From the available stores, tap Edit on the one you want to apply the theme.
  4. On the Default Theme section, select your newly created theme in the Applied Theme drop-down.
  5. Hit the Save Configuration
  6. If the cache is enabled, clear the cache and reload the store’s front page to see your applied changes.

Add Layout & Template Files

We have already identified that the search box module is in  $MAGENTO2_ROOT/vendor/magento/module-search/ . We will now look at its layout file which is the default.xml located in $MAGENTO2_ROOT/vendor/magento/module-search/view/frontend/layout/ folder. In default.xml , we have found this code that will help us since we are going to use a concept of extending layout file:

Now we will make a folder named Magento_Search in our theme folder and add the layout and templates folders inside the Magento_Search directory. So our directories will now look like this:

$MAGENTO2_ROOT/app/design/frontend/Cloudways/Mytheme/Magento_Search/layout/

$MAGENTO2_ROOT/app/design/frontend/Cloudways/Mytheme/Magento_Search/templates/

Create a default.xml file inside the layout folder of your theme and add the following code to it:

In the code above, we have defined a new block in the header-wrapper container which extends the Magento\Framework\View\Element\Template block because this block will help us in creating a simple block.

Last but not the least, add the customtext.phtml file in templates folder and insert the following code to it:

Here, echo __(''); function is used for multiple language support in Magento 2, and it should always be used when displaying any string.

Finally, clear your cache and visit your Magento 2 store’s frontend. It should look like this:

Magento 2 Front

Conclusion

This is a basic guide that describes how to use Magento 2 layouts, blocks, and templates. Magento 2 offers an easy way of creating custom themes and front views. You can also extend the current layouts and also override the existing ones. If you would like to ask any question about this topic, feel free to leave a comment.

Start Creating Web Apps on Managed Cloud Servers Now!

Easy Web App Deployment for Agencies, Developers and E-Commerce Industry

About Fayyaz Khattak

Fayyaz is a Magento Community Manager at Cloudways - A Managed Magento Hosting Platform. His objective is to learn & share about PHP & Magento Development in Community. Fayyaz is a food lover and enjoys driving. You can email him at m.fayyaz@cloudways.com

Stay Connected:

You Might Also Like...