How does the display function in the Template Class work to output HTML files?
The display function in the Template Class works by taking in a file path to an HTML template, replacing placeholders with dynamic content, and then outputting the final HTML file. To use the display function, you need to first create an instance of the Template Class, set any dynamic content using setter methods, and then call the display function with the file path of the HTML template.
class Template {
private $data = [];
public function set($key, $value) {
$this->data[$key] = $value;
}
public function display($templateFile) {
ob_start();
extract($this->data);
include($templateFile);
$output = ob_get_clean();
echo $output;
}
}
// Example usage
$template = new Template();
$template->set('title', 'Welcome to my website');
$template->set('content', 'This is some dynamic content');
$template->display('template.html');
Keywords
Related Questions
- What are the security implications of using unset() on variables in PHP, especially in relation to user input?
- What are common methods of identification used in Austria and Switzerland, and how are they structured?
- What potential pitfalls should be considered when combining text input from HTML forms with image creation in PHP?