Working with View Models
We like Magento view models a lot! In comparison to custom block classes, they are more reusable and composable. You can add an arbitrary number of view models to any template block via layout XML.
But after introducing a bunch of useful view models in Hyvä we got tired of all that seemingly unnecessary XML and thought of a simpler solution:
The View Model Registry
In a Hyvä theme, in every template, a variable
$viewModels is automatically available, introduced by
hyva-themes/magento2-theme-module, similar to the existing
$escaper variables. You can use it to fetch any view model (i.e. any class that implements
It is no longer needed to declare view models in XML!
You can read more about the usage of ViewModels versus Blocks here: Integer-net.com - View Models in Magento 1 and 2.
View Model Cache Tags
View models are often used to provide data to be rendered in a template.
In case you need the cache tags from some entities to be included in the HTTP response for the FPC cache record, you can implement the
Magento\Framework\DataObject\IdentityInterface and return the required cache tags.
They will be included in the HTTP response
X-Magento-Tags header automatically.
For more information have a look at the in depth documentation.