Exercise: Create a new function within a ViewModel in Magento
Learn how to create a custom ViewModel function in Magento to dynamically display blog post count.
Lesson Content
The idea of ViewModels is to extrapolate all of the logic out of the template. The template is responsible for the presentation of data, not anything else.
Update the hard-coded post count, but instead of passing a list of posts to a count() function in the template, implement this as a custom function on the ViewModel that returns the count.
Note: If you are having trouble seeing the updated changes, try purging the full page cache with:
bin/magento cache:clean full_page
When you have finished, check the Source Code tab for the solution.
Join the discussion!
Comments
Want to comment on this lesson?
Enroll in this course to leave comments.