Mini Shell
<?php
class View {
private $file; // Filename associated to view
private $domain; // View domain
private $content; // View content
private $controller; // Calling controller name
public function __construct($action, $controller = "") {
$this->controller = $controller;
$file = "views/";
if ($controller != "") {
$file = $file . $controller . "/";
}
$this->file = $file . $action . ".php";
}
// Generate and display view
public function generate($data) {
$content = $this->generateFile($this->file, $data);
$view = $this->generateFile('views/pageTemplate.php',
array('domain' => $this->domain, 'content' => $content));
echo $view;
}
// Generate a view file and return result
private function generateFile($file, $data) {
if (file_exists($file)) {
extract($data);
ob_start();
require $file;
return ob_get_clean();
}
else {
throw new ErrorException("Vue '$file' introuvable",0,E_USER_ERROR);
}
}
// Sanitize content from any HTML special chars
private function clean($value) {
return htmlspecialchars($value, ENT_QUOTES, 'UTF-8', false);
}
}