Blog: Thinking Out Loud

Musings on (Magento) Life, the (.NET) Universe and Everything (IT)

In magento 1, if you wanted to insert any block on home page, you would open theme’s default.xml file and add cms_index_index action and insert block inside a reference tag.

After release of  magento 2, default.xml is gone. In order to add a block to home page or any other page, there are many ways.
One way we use is create a new module. This is how we are going to proceed.

Before creating a magento 2 module, you should disable magento cache and put magento in developer mode.

Creating the Module

Create the following folders:

app/code/RLTS
app/code/RLTS/Helloworld

The RLTS folder is the module’s namespace, and Helloworld is the module’s name.

Now that we have a module folder, we need to create a module.xml file in the app/code/RLTS/Helloworld/etc folder with the following code:

To register the module, create a registration.php file in the app/code/RLTS/Helloworld folder with the following code:

Open your terminal and go to the Magento 2 root. Run from there the following command:

php bin/magento setup:upgrade

If you want to make sure that the module is installed, you can go to Admin → Stores → Configuration → Advanced → Advanced and check that the module is present in the list or you can open app/etc/config.php and check the array for the ‘RLTS_Helloworld’ key, whose value should be set to 1.

Adding Layout File to Add a Block to Home Page

If you want to add any block to any page, you need to create a layout file in following directory.

app/code/RLTS/Helloworld/view/frontend/layout

The name of layout file will depend on the action of page you want layout to apply. In our case, we want to update home page, so we create a file cms_index_index.xml
Put following code in the file to add a block:

Here you can see we have added a block in content Container of home page

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">