Last active
December 25, 2015 14:09
-
-
Save BlackScorp/6989268 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Template { | |
private $data; | |
private $fileName; | |
public function __construct($filename) { | |
$this->setFileName($filename); | |
} | |
private function setFileName($filename) { | |
if (!is_file($filename)) | |
throw new Exception(sprintf('File "%s" not found', $filename)); | |
$this->fileName = $filename; | |
} | |
public function render() { | |
if($this->data) | |
extract($this->data, EXTR_SKIP); | |
ob_start(); | |
try { | |
include_once $this->fileName; | |
} catch (Exception $e) { | |
ob_end_clean(); | |
throw $e; | |
} | |
return ob_get_clean(); | |
} | |
public function __set($key, | |
$value) { | |
$this->data[$key] = $value; | |
} | |
public function __get($key) { | |
return isset($this->data[$key]) ? $this->data[$key] : null; | |
} | |
public function __toString() { | |
try { | |
return $this->render(); | |
} catch (Exception $e) { | |
throw $e; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment