What are the benefits of using Template Inheritance in PHP for managing HTML content?
When managing HTML content in PHP, it can become cumbersome to repeat the same HTML code across multiple pages. Template Inheritance in PHP allows you to create a base template that contains the common HTML structure and placeholders for dynamic content. This makes it easier to maintain consistency and make changes across all pages by simply updating the base template.
// base_template.php
<!DOCTYPE html>
<html>
<head>
<title><?php echo $title; ?></title>
</head>
<body>
<?php echo $content; ?>
</body>
</html>
// page1.php
<?php
$title = "Page 1";
ob_start();
?>
<h1>Welcome to Page 1</h1>
<p>This is the content of Page 1.</p>
<?php
$content = ob_get_clean();
include 'base_template.php';
?>
// page2.php
<?php
$title = "Page 2";
ob_start();
?>
<h1>Welcome to Page 2</h1>
<p>This is the content of Page 2.</p>
<?php
$content = ob_get_clean();
include 'base_template.php';
?>