To access a view model within an existing block, the developer can use either of the following ways:
$block->getData(‘view_model’): This method will return the view model object that is assigned to the argument name “view_model” in the layout XML file. For example:
<arguments> <argument name=“view_model” xsi:type=“object”>ExampleObjectModel/ExampleObjectModel</argument> </arguments>
In the template file, the developer can access the view model object by using:
$block->getData(‘view_model’)
$block->getData(‘viewModel’): This method will return the view model object that is assigned to the argument name “viewModel” in the layout XML file. For example:
<arguments> <argument name=“viewModel” xsi:type=“object”>ExampleObjectModel/ExampleObjectModel</argument> </arguments>
In the template file, the developer can access the view model object by using:
$block->getData(‘viewModel’)
The following methods are not valid and will not work:
$block->viewModel(): This method does not exist and will cause an error.
$block->getViewHodel(): This method is misspelled and will cause an error.
Submit