|
| 1 | +--- |
| 2 | +title: View models |
| 3 | +contributor_name: Space 48 |
| 4 | +contributor_link: https://www.space48.com/ |
| 5 | +group: php-developer-guide |
| 6 | +--- |
| 7 | + |
| 8 | +A view model is an abstraction of the view exposing public properties and commands. It allows developers to offload features and business logic from block classes into separate classes that are easier to maintain, test, and reuse. |
| 9 | + |
| 10 | +## When to use view models |
| 11 | + |
| 12 | +Use this approach anytime you need to inject functionality into template files and your code does not need to be backwards compatible with Magento 2.1. |
| 13 | + |
| 14 | +{: .bs-callout-info } |
| 15 | +View models are available in Magento 2.2 onwards. If your code must be compatible with older versions of Magento, consider adding your logic to blocks. For more information about backward compatibility, see [Backward compatibility]({{ page.baseurl }}/contributor-guide/backward-compatible-development/). |
| 16 | + |
| 17 | +## How to write view models |
| 18 | + |
| 19 | +The following example shows how to add functionality to a core template with custom logic using a view model in the `cart/item/default.phtml` template, which is located in the `Magento/Checkout/view/frontend/layout/checkout_cart_item_renderers.xml` file: |
| 20 | + |
| 21 | +```xml |
| 22 | +<?xml version="1.0"?> |
| 23 | +<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd"> |
| 24 | +<body> |
| 25 | + <referenceBlock name="checkout.cart.item.renderers.default"> |
| 26 | + <arguments> |
| 27 | + <argument name="viewModel" xsi:type="object">Vendor\CustomModule\ViewModel\MyClass</argument> |
| 28 | + </arguments> |
| 29 | + </referenceBlock> |
| 30 | +</body> |
| 31 | +``` |
| 32 | + |
| 33 | +You must implement the right interface in your `viewModel` class (for example `ArgumentInterface`): |
| 34 | + |
| 35 | +```php |
| 36 | +namespace Vendor\CustomModule\ViewModel; |
| 37 | + |
| 38 | +class MyClass implements \Magento\Framework\View\Element\Block\ArgumentInterface |
| 39 | +{ |
| 40 | + public function getTitle() |
| 41 | + { |
| 42 | + return 'Hello World' |
| 43 | + { |
| 44 | +} |
| 45 | +``` |
| 46 | + |
| 47 | +Finally, in the `cart/item/default.phtml` template, you can access the public methods of your view model: |
| 48 | + |
| 49 | +```html |
| 50 | +<?php |
| 51 | + |
| 52 | +/** @var $viewModel \Vendor\CustomModule\ViewModel\MyClass */ |
| 53 | + |
| 54 | +$viewModel = $block->getViewModel(); |
| 55 | + |
| 56 | +?> |
| 57 | +<h1><?= $block->escapeHtml($viewModel->getTitle()); ?></h1> |
| 58 | + |
| 59 | +``` |
| 60 | + |
| 61 | +## Examples of View models in Magento |
| 62 | + |
| 63 | +- [Magento_Catalog]({{ site.mage2bloburl }}/{{ page.guide_version }}/app/code/Magento/Catalog/view/frontend/layout/catalog_product_view.xml#L34){:target="_blank"} |
| 64 | +This `viewModel` is used to inject breadcrumb JSON with HTML-escaped names into the template file. |
0 commit comments