{"id":3622,"date":"2022-12-01T13:00:36","date_gmt":"2022-12-01T13:00:36","guid":{"rendered":"https:\/\/www.cynoinfotech.com\/?p=3622"},"modified":"2026-04-16T13:32:11","modified_gmt":"2026-04-16T13:32:11","slug":"how-to-use-view-model-in-magento-2","status":"publish","type":"post","link":"https:\/\/cynoinfotech.com\/blog\/how-to-use-view-model-in-magento-2\/","title":{"rendered":"HOW TO USE VIEW MODEL  IN MAGENTO 2"},"content":{"rendered":"<p>In this blog, we are going through how to use the view model in Magento 2.<\/p>\n<p>ViewModel allows passing data and additional functionality to the template file without using a block, or object manager. Use a view model when product data and customer data are required in the front end.<\/p>\n<p>This is the best functionality to pass data and show it in the front end.<\/p>\n<p>\u21d2 let&#8217;s start, first of all, you create this file \u201c<strong>default.xml<\/strong>\u201d in this path \u201c<strong>app\/code\/vendorname\/ModuleName\/view\/frontend\/layout\/<\/strong>\u201d<\/p>\n<pre class=\"lang:default decode:true\">&lt;?xml version=\"1.0\"?&gt;\n&lt;page xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\" xsi:noNamespaceSchemaLocation=\n\"urn:magento:framework:View\/Layout\/etc\/page_configuration.xsd\"&gt;  \n  &lt;body&gt;\n    &lt;referenceBlock name=\"catalog.topnav\" template=\"VendorName_ModuleName::test.phtml\"&gt;\n       &lt;arguments&gt;\n         &lt;argument name=\"viewModel\" xsi:type=\"object\"&gt;VendorName\\ModuleName\\ViewModel\\Test&lt;\/argument&gt;\n       &lt;\/arguments&gt;\n    &lt;\/referenceBlock&gt;\n  &lt;\/body&gt;\n&lt;\/page&gt;<\/pre>\n<p>\u21d2 Then after you create this file &#8220;<strong>Test.php<\/strong>&#8221; in this path &#8220;<strong>app\/code\/VendorName\/ModuleName\/ViewModel\/<\/strong> &#8220;.<\/p>\n<pre class=\"lang:default decode:true \">&lt;?php\nnamespace VendorName\\ModuleName\\ViewModel;\n\nclass Test implements \\Magento\\Framework\\View\\Element\\Block\\ArgumentInterface\n{\n    \/**\n     * @var \\Magento\\Customer\\Model\\CustomerFactory\n     *\/\n    protected $customerFactory;\n    \/**\n     * @param \\Magento\\Catalog\\Model\\CustomerFactory     $customerFactory\n     *\/\n    public function __construct(\n        \\Magento\\Catalog\\Model\\CustomerFactory $customerFactory\n    ) {\n        $this-&gt;customerFactory = $customerFactory;\n    }\n    public function getCustomerById($id) {\n        $customerObj = $this-&gt;customerFactory-&gt;create()-&gt;load($id);\n        return $customerObj;\n    }\n}<\/pre>\n<p>\u21d2 then after you create a template file &#8220;<strong>test.phtml<\/strong>&#8221; in this path &#8220;<strong>app\\code\\Vendor\\Extension\\view\\frontend\\templates\\<\/strong>&#8220;.<\/p>\n<pre class=\"lang:default decode:true \">&lt;?php \n   $viewModel = $block-&gt;getViewModel();\n   echo $viewModel-&gt;getSomething();\n?&gt;<\/pre>\n<p>\u21d2 I hope you understand this blog. if you have any questions let me know in the comment section<\/p>\n<p>Thank You !!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this blog, we are going through how to use the view model in Magento 2. ViewModel allows passing data and additional functionality to the template file without using a block, or object manager. Use a view model when product data and customer data are required in the front end. This is the best functionality [&hellip;]<\/p>\n","protected":false},"author":670,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-3622","post","type-post","status-publish","format-standard","hentry","category-magento-blog"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>HOW TO USE VIEW MODEL IN MAGENTO 2 - cynoinfotech<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/cynoinfotech.com\/blog\/how-to-use-view-model-in-magento-2\/\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/cynoinfotech.com\\\/blog\\\/how-to-use-view-model-in-magento-2\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/cynoinfotech.com\\\/blog\\\/how-to-use-view-model-in-magento-2\\\/\"},\"author\":{\"name\":\"Arjun Makadiya\",\"@id\":\"https:\\\/\\\/cynoinfotech.com\\\/blog\\\/#\\\/schema\\\/person\\\/f1be8186602d3ebf417cf58baf125f24\"},\"headline\":\"HOW TO USE VIEW MODEL IN MAGENTO 2\",\"datePublished\":\"2022-12-01T13:00:36+00:00\",\"dateModified\":\"2026-04-16T13:32:11+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/cynoinfotech.com\\\/blog\\\/how-to-use-view-model-in-magento-2\\\/\"},\"wordCount\":148,\"publisher\":{\"@id\":\"https:\\\/\\\/cynoinfotech.com\\\/blog\\\/#organization\"},\"articleSection\":[\"Magento blog\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/cynoinfotech.com\\\/blog\\\/how-to-use-view-model-in-magento-2\\\/\",\"url\":\"https:\\\/\\\/cynoinfotech.com\\\/blog\\\/how-to-use-view-model-in-magento-2\\\/\",\"name\":\"HOW TO USE VIEW MODEL IN MAGENTO 2 - cynoinfotech\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/cynoinfotech.com\\\/blog\\\/#website\"},\"datePublished\":\"2022-12-01T13:00:36+00:00\",\"dateModified\":\"2026-04-16T13:32:11+00:00\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/cynoinfotech.com\\\/blog\\\/how-to-use-view-model-in-magento-2\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/cynoinfotech.com\\\/blog\\\/how-to-use-view-model-in-magento-2\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/cynoinfotech.com\\\/blog\\\/how-to-use-view-model-in-magento-2\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/cynoinfotech.com\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"HOW TO USE VIEW MODEL IN MAGENTO 2\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/cynoinfotech.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/cynoinfotech.com\\\/blog\\\/\",\"name\":\"cynoinfotech\",\"description\":\"web design &amp; development\",\"publisher\":{\"@id\":\"https:\\\/\\\/cynoinfotech.com\\\/blog\\\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/cynoinfotech.com\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/cynoinfotech.com\\\/blog\\\/#organization\",\"name\":\"Cynoinfotech\",\"url\":\"https:\\\/\\\/cynoinfotech.com\\\/blog\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/cynoinfotech.com\\\/blog\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/www.cynoinfotech.com\\\/wp-content\\\/uploads\\\/2017\\\/11\\\/final-logo-ci.png\",\"contentUrl\":\"https:\\\/\\\/www.cynoinfotech.com\\\/wp-content\\\/uploads\\\/2017\\\/11\\\/final-logo-ci.png\",\"width\":240,\"height\":70,\"caption\":\"Cynoinfotech\"},\"image\":{\"@id\":\"https:\\\/\\\/cynoinfotech.com\\\/blog\\\/#\\\/schema\\\/logo\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/www.facebook.com\\\/cynoinfotech\\\/\",\"https:\\\/\\\/x.com\\\/cynoinfotech\"]},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/cynoinfotech.com\\\/blog\\\/#\\\/schema\\\/person\\\/f1be8186602d3ebf417cf58baf125f24\",\"name\":\"Arjun Makadiya\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/d1792fe07de4cbcd8b55a0a55e79e3a67f4df3ab63af6b0738833c83cac0eab3?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/d1792fe07de4cbcd8b55a0a55e79e3a67f4df3ab63af6b0738833c83cac0eab3?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/d1792fe07de4cbcd8b55a0a55e79e3a67f4df3ab63af6b0738833c83cac0eab3?s=96&d=mm&r=g\",\"caption\":\"Arjun Makadiya\"},\"description\":\"Arjun Makadiya is a Junior Magento Developer at Cynoifotech. He is passionate about technology. Apart from work he likes to play cricket.\",\"url\":\"https:\\\/\\\/cynoinfotech.com\\\/blog\\\/author\\\/arjun-makadiya\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"HOW TO USE VIEW MODEL IN MAGENTO 2 - cynoinfotech","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/cynoinfotech.com\/blog\/how-to-use-view-model-in-magento-2\/","schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/cynoinfotech.com\/blog\/how-to-use-view-model-in-magento-2\/#article","isPartOf":{"@id":"https:\/\/cynoinfotech.com\/blog\/how-to-use-view-model-in-magento-2\/"},"author":{"name":"Arjun Makadiya","@id":"https:\/\/cynoinfotech.com\/blog\/#\/schema\/person\/f1be8186602d3ebf417cf58baf125f24"},"headline":"HOW TO USE VIEW MODEL IN MAGENTO 2","datePublished":"2022-12-01T13:00:36+00:00","dateModified":"2026-04-16T13:32:11+00:00","mainEntityOfPage":{"@id":"https:\/\/cynoinfotech.com\/blog\/how-to-use-view-model-in-magento-2\/"},"wordCount":148,"publisher":{"@id":"https:\/\/cynoinfotech.com\/blog\/#organization"},"articleSection":["Magento blog"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/cynoinfotech.com\/blog\/how-to-use-view-model-in-magento-2\/","url":"https:\/\/cynoinfotech.com\/blog\/how-to-use-view-model-in-magento-2\/","name":"HOW TO USE VIEW MODEL IN MAGENTO 2 - cynoinfotech","isPartOf":{"@id":"https:\/\/cynoinfotech.com\/blog\/#website"},"datePublished":"2022-12-01T13:00:36+00:00","dateModified":"2026-04-16T13:32:11+00:00","breadcrumb":{"@id":"https:\/\/cynoinfotech.com\/blog\/how-to-use-view-model-in-magento-2\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/cynoinfotech.com\/blog\/how-to-use-view-model-in-magento-2\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/cynoinfotech.com\/blog\/how-to-use-view-model-in-magento-2\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/cynoinfotech.com\/blog\/"},{"@type":"ListItem","position":2,"name":"HOW TO USE VIEW MODEL IN MAGENTO 2"}]},{"@type":"WebSite","@id":"https:\/\/cynoinfotech.com\/blog\/#website","url":"https:\/\/cynoinfotech.com\/blog\/","name":"cynoinfotech","description":"web design &amp; development","publisher":{"@id":"https:\/\/cynoinfotech.com\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/cynoinfotech.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/cynoinfotech.com\/blog\/#organization","name":"Cynoinfotech","url":"https:\/\/cynoinfotech.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/cynoinfotech.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/www.cynoinfotech.com\/wp-content\/uploads\/2017\/11\/final-logo-ci.png","contentUrl":"https:\/\/www.cynoinfotech.com\/wp-content\/uploads\/2017\/11\/final-logo-ci.png","width":240,"height":70,"caption":"Cynoinfotech"},"image":{"@id":"https:\/\/cynoinfotech.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/cynoinfotech\/","https:\/\/x.com\/cynoinfotech"]},{"@type":"Person","@id":"https:\/\/cynoinfotech.com\/blog\/#\/schema\/person\/f1be8186602d3ebf417cf58baf125f24","name":"Arjun Makadiya","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/d1792fe07de4cbcd8b55a0a55e79e3a67f4df3ab63af6b0738833c83cac0eab3?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/d1792fe07de4cbcd8b55a0a55e79e3a67f4df3ab63af6b0738833c83cac0eab3?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/d1792fe07de4cbcd8b55a0a55e79e3a67f4df3ab63af6b0738833c83cac0eab3?s=96&d=mm&r=g","caption":"Arjun Makadiya"},"description":"Arjun Makadiya is a Junior Magento Developer at Cynoifotech. He is passionate about technology. Apart from work he likes to play cricket.","url":"https:\/\/cynoinfotech.com\/blog\/author\/arjun-makadiya\/"}]}},"_links":{"self":[{"href":"https:\/\/cynoinfotech.com\/blog\/wp-json\/wp\/v2\/posts\/3622","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/cynoinfotech.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cynoinfotech.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cynoinfotech.com\/blog\/wp-json\/wp\/v2\/users\/670"}],"replies":[{"embeddable":true,"href":"https:\/\/cynoinfotech.com\/blog\/wp-json\/wp\/v2\/comments?post=3622"}],"version-history":[{"count":5,"href":"https:\/\/cynoinfotech.com\/blog\/wp-json\/wp\/v2\/posts\/3622\/revisions"}],"predecessor-version":[{"id":3629,"href":"https:\/\/cynoinfotech.com\/blog\/wp-json\/wp\/v2\/posts\/3622\/revisions\/3629"}],"wp:attachment":[{"href":"https:\/\/cynoinfotech.com\/blog\/wp-json\/wp\/v2\/media?parent=3622"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cynoinfotech.com\/blog\/wp-json\/wp\/v2\/categories?post=3622"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cynoinfotech.com\/blog\/wp-json\/wp\/v2\/tags?post=3622"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}