How To Create Magento 2 Blocks With Blocks Builder

In this post, we will show you how to create magento 2 blocks in 2 ways. The first one is to write code to test out functionality. The second one is to use magento 2 blocks builder. This tool is included in Magento 2 Page Builder extension. Learn more here.

I. How To Create Magento 2 Blocks by writing code

1. Create a module.xml in in app/code/Lof/HelloWorld/etc

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Module/etc/module.xsd">
 <module name="Lof_HelloWorld" setup_version="1.0.0">
 </module>
</config>

2. Create app/code/Lof/HelloWorld/registration.php file

<?php
MagentoFrameworkComponentComponentRegistrar::register(
 MagentoFrameworkComponentComponentRegistrar::MODULE,
 'Lof_HelloWorld',
 __DIR__
 );

3. Create a layout file in the following directory appcodeLofHelloWorldViewfrontendlayoutcms_index_index.xml

The cms_index_index.xml name will make our block only appear on the home page (module_controller_action.xml).

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../../../lib/internal/Magento/Framework/View/Layout/etc/page_configuration.xsd">
 <body>
     <referenceContainer name="content">
         <block class="LofHelloWorldBlockHelloWorld" before='-' template="helloworld.phtml"/>
     </referenceContainer>
 </body>
</page>

4. Lets create a block for our module. Create block file app/code/Lof/HelloWorld/Block/HelloWorld.php

<?php
namespace LofHelloWorldBlock;

/**
* Helloworld Block
*/class Helloworld extends MagentoFrameworkViewElementTemplate
{
 public function getTitle()
 {
     return __("LOF Hello World");
 }
}

5. Create a template file app/code/Lof/HelloWorld/View/frontend/templates/helloworld.phtml

6. Active Lof_HelloWorld extension

We have two ways to active Lof_Helloworld extension

1) Open Command line in folder root of magento and run commands:
php bin/magento setup:upgrade

2) Directly edit file app/etc/config.xml: In the array module, add the element: ‘Lof_Helloworld’ => 1

7. Run the command line refresh cache and check home page
php bin/magento cache:flush

II. How to create block with block builder.

Ves Block Builder is  one of amazing tool that let you to build any type of blocks with ease. This tool included in Magento 2 Page Builder extension. Learn more here:

1. Add block profile
Please go to Admin panel >> Ves page builder >> Add new block profile

2. Design your block

Then, you need to open “design” tab to start designing your own block

– Prefix class: This class is to custom
– Enable container: If you want to enable container, you can select yes and vice versa
– Use backup layout: You can take use of backup layout from select box
– Design in: This funtions lets you create responsive blocks on lagre screen, laptop, tablets, mobile.

– Enable Grid: you can enable/disable grid

3. Manage blocks

For customers who are interested in magento 2 extensions, you can see the list below:

Alena Dao

“Good marketing makes the company look smart. Great marketing makes the customer feel smart.”

Recent Posts

15+ Must-have Magento 2 Free Extensions To Supercharge Your Store In 2025 | Latest updates

Magento 2 is an incredibly flexible and powerful eCommerce platform that can be tailored to…

22 hours ago

What To Consider While Using Magento 2.0 For Your Streaming App?

When it comes to e-Commerce platforms, undoubtedly Magento 2.0 grabs the spotlight. Magento might not…

2 days ago

Black Friday 2024: Hot Deals Collection For Magento 2

Black Friday and Cyber Monday – BFCM 2024 are nearly around the corner, and it’s…

3 days ago

Landofcoder BFCM 2024: Biggest Sale Of The Year – 30% OFF All Magento 2 Extensions

The holiday shopping season is here, and there’s no better time to supercharge your Magento…

5 days ago

Top 10 Progressive Web App Development Companies To Elevate Your Business

In today’s digital landscape, the rapid evolution of advanced web applications, particularly Progressive Web Apps…

1 week ago

Why Your Retail Business Needs Big Data eCommerce In 2025?

As eCommerce booms, big data eCommerce will be all about providing insight into how to…

1 week ago