Repository navigation
Improve View Models documentation with best practices and clarification #423
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
mohaelmrabet
wants to merge
4
commits into
AdobeDocs:main
Choose a base branch
from
mohaelmrabet:patch-1
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
ec75980
Improve View Models documentation with best practices and clarification
mohaelmrabet 9e286b5
Merge branch 'main' into patch-1
mohaelmrabet e5df1aa
Merge branch 'main' into patch-1
mohaelmrabet 5ab3706
Merge branch 'main' into patch-1
dshevtsov File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -23,7 +23,7 @@ View models are available in Adobe Commerce and Magento Open Source 2.2 and late | |||||||||
|
|
||||||||||
| The use of helpers in templates is discouraged. It is recommended to use view models instead. | ||||||||||
|
|
||||||||||
| ## How to write view models | ||||||||||
| ## Create and use view models | ||||||||||
|
|
||||||||||
| View models can be used by passing the view model class as an argument to a template's block in the page layout configuration file. In the following example snippet, `MyNewViewModel` is the view model class of the ExampleCorp_Catalog module passed as an argument to a block. | ||||||||||
|
|
||||||||||
|
|
@@ -72,8 +72,9 @@ $viewModel = $block->getViewModel(); | |||||||||
| ``` | ||||||||||
|
|
||||||||||
| ## Examples | ||||||||||
| Here are several examples of how view models are used in Adobe Commerce. | ||||||||||
|
|
||||||||||
| [Theme](https://github.com/magento/magento2/blob/2.3.3/app/code/Magento/Theme/view/frontend/layout/default.xml#L43-L45 "view_model definition"). This `view_model` is injected into a template to return the target store redirect url. | ||||||||||
| [Theme](https://github.com/magento/magento2/blob/2.3.3/app/code/Magento/Theme/view/frontend/layout/default.xml#L43-L45 "view_model definition"). In this example, the `view_model` is injected into a template to return the target store redirect URL. | ||||||||||
|
|
||||||||||
| The following is an example of view model usage within the `Magento/Catalog/view/frontend/layout/catalog_product_view.xml` layout file. | ||||||||||
|
|
||||||||||
|
|
@@ -144,3 +145,55 @@ $postArray = $viewModel->getPostData( | |||||||||
| ['product' => $_item->getEntityId()] | ||||||||||
| ); | ||||||||||
| ``` | ||||||||||
|
|
||||||||||
| ## Additional concepts and best practices | ||||||||||
|
|
||||||||||
| ### Difference between blocks and view models | ||||||||||
|
|
||||||||||
| Blocks prepare data for rendering and interact with the layout system. | ||||||||||
| View models expose data or operations directly to templates without layout responsibilities. | ||||||||||
|
|
||||||||||
| Use a block when layout manipulation is required. | ||||||||||
| Use a view model when the template needs business logic or computed values. | ||||||||||
|
|
||||||||||
| ### Single Responsibility Principle (SRP) | ||||||||||
|
|
||||||||||
| View models help enforce SRP by moving business logic out of blocks. | ||||||||||
| This keeps classes smaller, easier to test, and more maintainable. | ||||||||||
|
|
||||||||||
| ### Reusability | ||||||||||
|
|
||||||||||
| A view model can be injected into multiple templates or blocks. | ||||||||||
| This avoids duplicated logic and ensures consistent behavior across the storefront. | ||||||||||
|
|
||||||||||
| ### Using multiple view models | ||||||||||
|
|
||||||||||
| You can provide several view models to the same block: | ||||||||||
|
|
||||||||||
| ```xml | ||||||||||
| <arguments> | ||||||||||
| <argument name="product_view_model" xsi:type="object">Vendor\Module\ViewModel\ProductData</argument> | ||||||||||
| <argument name="utility_view_model" xsi:type="object">Vendor\Module\ViewModel\Utility</argument> | ||||||||||
| </arguments> | ||||||||||
| ``` | ||||||||||
| Retrieve them in the template using $block->getViewModel() or custom accessors. | ||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
| ### Cache and performance | ||||||||||
|
|
||||||||||
| View models do not manage cache identities. | ||||||||||
| If a view model depends on cached entities, ensure the block implements | ||||||||||
| `\Magento\Framework\DataObject\IdentityInterface` to prevent stale output. | ||||||||||
|
|
||||||||||
| ### Naming conventions | ||||||||||
|
|
||||||||||
| - Store view models in the `ViewModel` namespace. | ||||||||||
| - Use descriptive names such as `PrepareData`, `Formatter`, or `Resolver`. | ||||||||||
| - Keep methods small and focused on template-specific data. | ||||||||||
|
|
||||||||||
| ### Checklist | ||||||||||
|
|
||||||||||
| 1. Implement `\Magento\Framework\View\Element\Block\ArgumentInterface`. | ||||||||||
| 2. Inject dependencies in the constructor. | ||||||||||
| 3. Add only the logic required by the template. | ||||||||||
| 4. Register the view model in the layout XML. | ||||||||||
| 5. Access it using `$block->getViewModel()`. | ||||||||||
| 6. Use `$escaper` for proper HTML escaping. | ||||||||||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.