How can the use of a Template Engine in PHP help with formatting and displaying data in a web application?
Using a Template Engine in PHP can help separate the presentation layer from the business logic, making it easier to manage and update the design of a web application. By using templates, developers can create reusable components for displaying data in a consistent format across multiple pages. This can improve code readability, maintainability, and overall efficiency in web development.
// Example of using a Template Engine (Twig) in PHP
require_once 'vendor/autoload.php';
$loader = new \Twig\Loader\FilesystemLoader('templates');
$twig = new \Twig\Environment($loader);
$data = [
'title' => 'Welcome to My Website',
'content' => 'This is some sample content to display on the page.',
'items' => ['Item 1', 'Item 2', 'Item 3']
];
$template = $twig->load('index.html');
echo $template->render($data);