How does the MVC pattern relate to creating HTML templates with OOP in PHP?

The MVC pattern helps in separating concerns in a web application by dividing it into three interconnected components: Model, View, and Controller. When creating HTML templates with OOP in PHP, the View component of MVC is responsible for handling the presentation logic and generating the HTML output. By using OOP principles like inheritance and encapsulation, we can create reusable and maintainable HTML templates in PHP.

// Example of creating an HTML template using OOP in PHP

class HTMLTemplate {
    private $title;
    private $content;

    public function __construct($title, $content) {
        $this->title = $title;
        $this->content = $content;
    }

    public function render() {
        echo "<!DOCTYPE html>";
        echo "<html>";
        echo "<head><title>{$this->title}</title></head>";
        echo "<body>{$this->content}</body>";
        echo "</html>";
    }
}

// Create a new HTML template
$template = new HTMLTemplate("Welcome", "<h1>Hello, World!</h1>");

// Render the HTML template
$template->render();