The hidden truth about Magento 2 Theme Development from Scratch
A theme governs how elements and the complete website are presented to the user. The Magento Theme Development process is said to be the most essential factor that is widely used for maintaining a consistent design and tone format across the entire store. With this theme development you can improve the visibility of your brand and storefront by combining various custom templates, designs, styles, or images available with the Magento 2 management board.
But first, let's go over the fundamentals.
What Exactly Is a Magento Theme?
Magento theme development services are said to be the collection of layouts that together provide the real design for your pages. The theme is a component of your store in Magento 2.
What Is the Distinction Between Default and Custom Themes?
Magento 2 is an excellent platform which comes with two amazing default themes: Luma and Blank themes.
Luma serves as a demo theme where you may get an idea of how a Magento site works by incorporating various ready-made Luma and Blank themes. It also helps you to truly stand out, about how to build your own customized design.
Here is a list of the nine stages you must do to magento theme customization.
- Developing a Magento theme folder
- Declare your theme
- Install the Composer package
- the registration.php file
- Making static files and folders
- Create catalogue product images
- Designate Theme Logo
- Basic design elements
- Types and norms of layout files
So your app/design/frontend/theme.xml file should look something like this:
<theme xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:no NamespaceSchemaLocation="../../../../lib/internal/Magento/Framework/Config/etc/theme.xsd">
Theme Folder structure:
│ ├── etc/
│ │ ├── view.xml
│ ├── web/
│ │ ├── images
│ │ │ ├── logo.svg
│ ├── registration.php
│ ├── theme.xml
│ ├── composer.json
<Vendor> is theme vendor. e.g: Magecaptain
<theme> is theme name. e.g: simple
Ok, let’s go
1. Developing a Magento theme folder
For creating a magento 2 theme folder first :
- Go to app/design/frontend
- And create a vendor folder app/design/frontend/<vendor> e.g: app/design/frontend/Mageplaza
- Then build a theme folder app/design/frontend/<vendor>/<theme> e.g: app/design/frontend/Mageplaza/simple
│ │ ├──simple/
│ │ │ ├── ...
│ │ │ ├── …
2. Declare your theme
Now we have a file folder app/design/frontend/Magecaptain/simple , now make a file named theme.xml that describe the basic facts about theme such as: Name, parent theme, showing image.
<theme xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Config/etc/theme.xsd">
<title>Mageplaza Simple</title> <!-- your theme's name -->
<parent>Magento/blank</parent> <!-- the parent theme, in case your theme inherits from an existing theme -->
<preview_image>media/preview.jpg</preview_image> <!-- the path to your theme's preview image -->
3. Composer package
Composer is another outstanding tool for writing in PHP. It lets you to announce the archives your plan depends on and it will manage (install/update) them for you.
In order to allocate your magento 2 theme customization as a package, add a composer.json file to the theme manual and then register the package on the packaging server.
4. registration.php file
Now you can easily add the next content to register the theme to your Magento 2
* Copyright © 2015 Magento. All rights reserved.
* See COPYING.txt for license details.
You should change Magecaptain, simple to your vendor, theme name.
5. Creating static files, folders
The structure is as follows:
│ ├── css/
│ │ ├── source/
│ ├── fonts/
│ ├── images/
│ ├── js/
When developing a Magento 2 theme or extension, if you alter any files in the app/design/area>/Mageplaza/simple/web folder, you must also update the static folders pub/static and var/view preprocessed. Otherwise, there is no change on the frontend.
6. Set up catalogue product images
There is a file called etc/view.xml in the theme structure I explained above. This file is a configuration file. This file is necessary for the Magento 2 theme but optional if it is present in the parent theme.
Create a folder etc and a file view.xml in app/design/area>/Mageplaza/simple/. You can copy the view.xml file to the parent theme, for example, Blank theme app/design/frontend/Magento/blank/etc/view.xml.
Let's go ahead and edit the image setup for the catalogue product grid page.
<image id="category_page_grid" type="small_image">
Image properties in view.xml are configured in the element scope:
Image properties are defined for each image type via the id and type attributes of the image> element:
<image id="unique_image_id" type="image_type">
<width>100</width> <!-- Image width in px -->
<height>100</height> <!-- Image height in px -->
7. Declare Theme Logo
Theme dir>/web/images/logo.svg is the default file format in Magento 2, but you can modify it in your theme to other file formats like png or jpg, but you must declare it.
The logo should be 300x300px in size, and you should open the file theme dir>/Magento Theme/layout/default.xml.
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<argument name="logo_file" xsi:type="string">images/custom_logo.png</argument>
<argument name="logo_img_width" xsi:type="number">300</argument>
<argument name="logo_img_height" xsi:type="number">300</argument>
8. Basic layout elements
The essential elements of Magento page design are blocks and containers.
A container exists just to assign content structure to a page. A container contains no additional content other than the content of included elements. Containers include the header, left column, main column, and footer.
9. Layout files types and conventions
Module and theme layout files
The following phrases are used to differentiate between layouts given by various application components:
- Module-provided base layouts: Layout files. Traditional location:
- Files for page settings and generic layout: <module dir>/view/frontend/layout
- Files for page layout: module dir>/view/frontend/page layout
- Theme layouts: Theme-provided layout files. Traditional location:
- Files for page configuration and generic layout: theme dir>/Namespace> Module>/layout
- Files for page layout: theme dir>/Namespace> Module>/page layout
Create a theme extending file
Rather than duplicating significant page layout or page settings code and then editing what you want to change, the Magento system merely involves the production of an expanded layout file with the needed changes.
Do the following include an extending page configuration or generic layout file?
The post discusses some of the most important things that will assist you in building a Magento 2 theme from the ground up. So, by adhering to the aforementioned guidelines, you may quickly build the theme. The article also covers some of the fundamental distinctions that will aid you in better understanding Magento.