How To

How to Use ViewModels in Magento 2

Hello Magento Friends,

In this article, we will focus on the most important functionality, How to Use ViewModels in Magento 2. Did you missed the previous article? No worries! It is right here. How to set NOINDEX NOFOLLOW for Specific Page in Magento 2.

With the release of Magento 2.2, there have been many improvements and one of them is the ViewModel concept. The ViewModel is a class that we insert to phtml, so that required and relevant data could be accessed from it. Avoid unwanted overriding of the block class using View Model. With the use of ViewModel, you do not need to extend the block class or add dependencies to the block. ViewModel allows passing data and additional functionality to the template file.

Let’s take a look at How to Use ViewModels in Magento 2.

Steps to Use ViewModels in Magento 2:

Step 1:  Move to the below path

app\code\Vendor\Extension\view\frontend\layout\custom_layout.xml

And add the code as follows

<block name="custom.block" template="Vendor_Module::custom.phtml">
<arguments> 
 <argument name="viewModel" xsi:type="object">Vendor\Extension\ViewModel\Custom</argument> 
</arguments> 
</block>

Step 2: Now, navigate to the below path

app\code\Vendor\Extension\ViewModel\Custom.php

And add the below-mentioned code

<?php 
namespace Vendor\Extension\ViewModel; 

use Magento\Framework\View\Element\Block\ArgumentInterface; 

class Custom implements ArgumentInterface 
{
 public function getSomething() 
 { 
  return "Hello World"; 
 }
 }

Step 3: Finally, move to the below path

app\code\Vendor\Extension\view\frontend\templates\Custom.phtml

And add the following code

<?php 
   $viewModel = $block->getViewModel();
   echo $viewModel->getSomething();
?>

Conclusion:

This way you can Use ViewModels in Magento 2. If you have any doubts, let me know in the comment part. Share the article with your other developer friends and stay updated with us for more.

Happy Coding!

Click to rate this post!
[Total: 9 Average: 4.3]
Dhiren Vasoya

Dhiren Vasoya is a Director and Co-founder at MageComp, Passionate 🎖️ Certified Magento Developer👨‍💻. He has more than 9 years of experience in Magento Development and completed 850+ projects to solve the most important E-commerce challenges. He is fond❤️ of coding and if he is not busy developing then you can find him at the cricket ground, hitting boundaries.🏏

View Comments

Recent Posts

What are Net Sales? How to Calculate Your Net Sales?

In the world of business, understanding financial metrics is crucial for making informed decisions and…

1 day ago

Magento 2 Extensions Digest April 2024 (New Release & Updates)

Welcome to the MageComp Monthly Digest, where we bring you the latest updates, releases, and…

1 day ago

The ABCs of Geofencing: Definition, Features and Uses

In this era, businesses are always on the lookout for ways to engage with their…

2 days ago

How to Delete Product Variant in a Shopify Remix App using GraphQL Mutations?

Managing a Shopify store efficiently involves keeping your product catalog organized. This includes removing outdated…

3 days ago

6 Innovative Tools Revolutionizing E-Commerce Operations

E-commerce has transformed the way consumers shop for products and services and interact with businesses.…

5 days ago

How Upcoming Cookie Changes Will Affect Your E-commerce Website?

The e-commerce world is constantly in flux. New tech and strategies emerge daily to help…

5 days ago