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 the 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.
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/.
Magento Block Types
Magento Blocks are PHP classes which create a link between the layouts and templates. Blocks are defined using <block> element in layout’s XML file:
<block class="Magento\Framework\View\Element\Template" name="customtext" template="Magento_Search::customtext.phtml" />
Now, let’s see Magneto block types 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 Magento 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.
One-Click Hosting Solution to Deploy Magento in Minutes
Get a powerful hosting stack with fully customizable platform to take care of all your hosting worries.
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.
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:
<theme xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Config/etc/theme.xsd"> <title>Cloudways Theme</title> <!-- your theme's name --> <parent>Magento/blank</parent> <!-- the parent theme, in case your theme inherits from an existing theme --> <media> <preview_image>media/preview.jpg</preview_image> <!-- the path to your theme's preview image --> </media> </theme>
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:
<?php /** * Copyright © 2015 Magento. All rights reserved. * See COPYING.txt for license details. */ \Magento\Framework\Component\ComponentRegistrar::register( \Magento\Framework\Component\ComponentRegistrar::THEME, 'frontend/Cloudways/Mytheme', __DIR__ );
After creation of all these files and folders, use the following CLI commands to register the theme in the Magento 2 system:
rm -rf var/di/* var/generation/* var/cache/* var/log/* var/page_cache/* php bin/magento cache:clean php bin/magento cache:flush php bin/magento setup:upgrade php bin/magento setup:di:compile php bin/magento indexer:reindex
Apply The Theme
Once you have added your theme files, you can apply it to your Magento 2 store using these simple steps:
- Login to your Magento 2 admin panel.
- Go to CONTENT -> Configuration.
- From the available stores, tap Edit on the one you want to apply the theme.
- On the Default Theme section, select your newly created theme in the Applied Theme drop-down.
- Hit the Save Configuration
- 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:
<referenceContainer name="header-wrapper"> <block class="Magento\Framework\View\Element\Template" name="top.search" as="topSearch" template="Magento_Search::form.mini.phtml" /> </referenceContainer>
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:
Create a default.xml file inside the layout folder of your theme and add the following code to it:
<?xml version="1.0"?> <!-- /** * Copyright © 2016 Magento. All rights reserved. * See COPYING.txt for license details. */ --> <page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd"> <body> <referenceContainer name="header-wrapper"> <block class="Magento\Framework\View\Element\Template" name="customtext" template="Magento_Search::customtext.phtml" /> </referenceContainer> </body> </page>
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:
<div class="block" style="float: right;margin-bottom: 0.5%;margin-top: 0.5%;"> <?php echo __('This is Custom Text'); ?> </div>
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:
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, in addition to extending current layouts and override existing ones. While working on backend or frontend development, loading issues can become a problem. This is why it is important to opt for an efficient Magento hosting service that guarantees fast response times and loading speed.
If you would like to ask any question about this topic, feel free to leave a comment.
Boost Your Magento Store Performance by 5x Times & Maximize Your Sales
Our fastest Magento hosting can help you in growing your business revenue by 500%
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 email@example.com