Clever works_

How to create Block and Layout Configuration in Magento 2

Magento 2 BLOCK

Today we will create a Block and .phtml template for our HelloWorld Module. We need this to display data with HTML code. Files with .phtml extension contain PHP variables and static HTML code. We just display data here. Our logic where we process the data is in Blocks. Firstly, we need to create the file Hello.php in app/code/CleverCode/HelloWorld/Block with contents:

It extends from \Magento\Framework\View\Element\Template and contains the sayHello() method to display our text. We can set other methods if needed.

Magento 2 LAYOUT CONFIGURATION

To declare blocks, we need to use the <block> instruction. To set a template for blocks, we can use the template attribute or <argument> instruction. We need to create the file hello_world_index.xml in app/code/CleverCode/HelloWorld/view/frontend/layout with the following contents:

We added a page title and set which .phtml file which will show the content for /hello/world/index URL. The name of our file is very important; it should be created in this format: FrontName_Folder_Under_Controller_className.xml

Next, we need to create the file index.phtml in app/code/CleverCode/HelloWorld/view/frontend/templates with the following contents:

By using the /hello/world/index URL in our browser, we will see:

Clever Code Page

Related works_

BreakAwayDistributing B2B and B2C
selling platform

The SLK Solutions agency responsible for developing an online store hired us to help them with creating a selling platform dedicated to B2B and B2C clients. BreakAwayDistributing – agency operating from 1993 with main office located in Hamilton, Canada offers high quality products

Learn more

Internet flower shop
from SLK Solutions

SLK Solutions is an IT agency operating in the USA, which helps small and large business owners to implement new technologies. They asked us to support them with our experts with the complete implementation of an eCommerce project for their client.

Learn more