Skip to content

Instantly share code, notes, and snippets.

@BlackScorp
Last active December 25, 2015 14:09
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BlackScorp/6989268 to your computer and use it in GitHub Desktop.
Save BlackScorp/6989268 to your computer and use it in GitHub Desktop.
<?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