Quick Tip: Convenience Hacks for Passing Data to Views
In MVC based architectures, working with template engines is an inevitable part of the development routine. It usually goes like this: we prepare and pass the data to the view. In the view, we print them based on our layout design.
Here is a basic example of how it works using Twig.
<?php // Twig initialization // Preparing data $user = 'user data'; $posts = 'posts'; $comments = 'comments'; $twig->render('author.page', [ 'user' => $user, 'posts' => $posts, 'comments' => $comments, ]); // ...
There are times, however, when the number of variables might be much higher than this: ten or more. In that case, we’ll have a tall list of variables (as an associative array), being passed to the respective template. It gets messy and unreadable quickly.
In this post, we’re going to talk about a trick for passing the defined variables to the view in a somewhat more convenient way: we can use the built-in compact() function to handpick the data we need:
Continue reading %Quick Tip: Convenience Hacks for Passing Data to Views%