What are two ways to access the view model class in the template?

An Adobe Commerce developer is using a view model within an existing block:

What are two ways to access the view model class in the template? (Choose two.)
A . $block->getData(‘view_model’)
B . $block->viewModel()
C . $block->getViewHodel()
D . $block->getData(‘viewModel)

Answer: A, D

Explanation:

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:

<referenceBlock name=“blog_posts_list”> <arguments> <argument name=“view_model”

xsi:type=“object”>ExampleObjectModel/ExampleObjectModel</argument> </arguments> </referenceBlock>

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:

<referenceBlock name=“blog_posts_list”> <arguments> <argument name=“viewModel”

xsi:type=“object”>ExampleObjectModel/ExampleObjectModel</argument> </arguments> </referenceBlock>

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.

Latest AD0-E720 Dumps Valid Version with 50 Q&As

Latest And Valid Q&A | Instant Download | Once Fail, Full Refund

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments