Finish web interface for admin
[infodrom/musiikki-web.git] / class / template.class.php
1 <?php
2
3 class Template {
4     const DIR = 'templates';
5
6     protected $template;
7     protected $templatePath;
8
9     public static function html($path, array $data=array())
10     {
11         $template = new Template($path);
12         return $template->render($data);
13     }
14
15     public function __construct($template)
16     {
17         $this->template = $template;
18         $this->templatePath = __DIR__ . '/../' . static::DIR . '/' . $this->template . '.phtml';
19
20         if (!is_readable($this->templatePath))
21             throw new Exception("Template {$this->template} not found");
22     }
23
24     public function render(array $data=array())
25     {
26         foreach ($data as $key => $value)
27             $$key = $value;
28
29         ob_start(); // Output Buffer einschalten
30         include($this->templatePath);
31         $renderedText = ob_get_contents();
32         ob_end_clean(); // Output Buffer beenden
33
34         return $renderedText;
35     }
36 }