Layout and Blocks in Magento 2.x
In this blog post, we will see how to add layout file and block to your module
In the previous blog, we created an ManishJoy_HelloWorld module. Using the same module, we will display “Hello World” again but this time using layout/block with a proper page structure.
Now, let’s start with the steps:
Step 1: Controller
In the previous World.php controller file, we had simply displayed “Hello World” on an empty page without any CSS/Stylying getting applied to it. We will remove that and use the layout system of Magento.
The new code for World.php controller would be:
Here we have injected ‘PageFactory’ into the controller’s constructor and which is used further to initialize the layout in execute().
Dependency Injection, is one of the major changes in Magento 2 and will discuss it in detail later. For now, just use this code directly.
Step 2: Layout
Now we will setup the layout file for our action.
In Magento 2 all layout files of a module are located at “view/frontend/layout” folder of a module and for each route we need to create a different layout file. In our case the file would be:
ManishJoy/HelloWorld/view/frontend/layout/manishjoy_hello_world.xml
The content of this file would be:
Magento 2 also supports a layout file named default.xml
, which gets loaded for all of the actions of any module/action.
Step 3: Block
In the previous step, we defined our layout with a keyword “class”, which is our block class. Now, let’s create this class at this path: ManishJoy/HelloWorld/Block/Main.php
The code for this file would be:
This is a simple and empty block with no functions as we are just doing the basic operations here.
Step 4: Template
Now that our block file is set, next we need to set the template file.
Templates for a module are located in “view/frontend/templates” directory so we need to create the file at this path optionally
ManishJoy/HelloWorld/“view/frontend/templates/content.phtml
Let’s now add “Hello World” to this page:
Now you are all set, now you can open the URL http://yourbaseurl.com/manishjoy/hello/world to test it.
You will see “Hello World” again but with full page layout (header, footer etc.)
Note: __() is the i18n
(multiple language/localization) function for magento2. We should always use this and never directly display a string.
Step 5: Passing data from Block to Template
In step 3, we created a very basic block and in step 4, we simply had displayed a static string “Hello World”. But in real world, we need to fetch data etc. and show it in the template. But, We cannot create functions in template files to do such operations. Now let’s see how can we pass data from Block to template (phtml) files.
As you can see we have used this $this->setText('Hello World');
to send data from block.
Now in the template file, you need to update the code like this to fetch this data and display:
Liked the post, share with others: