What are some best practices for passing data from a controller to a view in PHP?

When passing data from a controller to a view in PHP, it is best practice to use a template engine like Twig to separate the logic from the presentation. This helps keep the code clean and maintainable. To pass data, simply assign the variables in the controller and then render the view with the assigned data.

// Controller
$data = [
    'name' => 'John Doe',
    'age' => 30
];

// Render the view with the data
echo $twig->render('index.html', $data);