Post
object for a media card.Post
object to an array that has the following keys:title
description
published
twig
template. Public methods are used as keys in this array, and the method is executed to get the value.title
, description
and published
). These are the keys that our view needs.$context
looks like the following when the view model has been converted to an array (without having to have prepared the card
structure in the Controller):twig
views.toArray()
method.toArray
method on your view model.__construct()
should accept all the data it needs in order to do the transformation.twig
view has the correct data.static
method on your view model that constructs the view model for a specific use case.Post
object. So we can add the following method to our view model:PostTeasersViewModel
which transforms a collection of posts ready for a list view.
And you could have the following named constructor:latestPosts($limit = 3)
- which knows how to get the latest n posts.relatedPosts(Post $post)
- which knows how to get posts related to given post