Created
January 5, 2017 05:57
-
-
Save yareally/04333d5fa1b0774ac77c5fdcc36d3cd7 to your computer and use it in GitHub Desktop.
This file contains 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 | |
namespace ka\core; | |
use \ka\config as cfg; | |
/** | |
* Templating class | |
* | |
*/ | |
class Template | |
{ | |
/** | |
* @var array Holds variables to be used inside views | |
*/ | |
protected $templateVars = array( | |
'pageTitle' => '', | |
'page' => '', | |
'domain' => '', | |
'header' => '', | |
'footer' => '', | |
); | |
//protected $controller; | |
/** | |
* @var string View file to use | |
*/ | |
public $viewFile; | |
public $viewDir; | |
/** | |
* @var string Default view extension | |
*/ | |
public $viewExtension = '.phtml'; | |
/** | |
* Class constructor | |
* | |
* @param string Path to views directory if other than default (set in config) | |
*/ | |
public function __construct($viewDir = null) | |
{ | |
//$this->viewDir = $viewDir ? $viewDir : cfg\BASE_DIR . cfg\VIEWS_DIR; | |
/**/ | |
if (!empty($viewDir)) { | |
$this->viewDir = $viewDir; | |
} else { | |
$this->viewDir = cfg\BASE_DIR . cfg\VIEWS_DIR; | |
}/**/ | |
$this->templateVars['domain'] = cfg\DOMAIN_NAME != '' ? cfg\DOMAIN_NAME : ''; | |
} | |
/** | |
* Set variables for use inside views | |
*/ | |
public function __set($name, $value) | |
{ | |
$this->templateVars[$name] = $value; | |
} | |
/** | |
* Provide ability to retrieve template variables | |
* | |
* @return mixed | |
*/ | |
public function __get($name) | |
{ | |
if ( array_key_exists($name, $this->templateVars) ) { | |
//if ( isset($this->templateVars[$name]) ) { | |
return $this->templateVars[$name]; | |
} | |
/**/ | |
// If a request for a non-existent variable then throw an error | |
// @TODO convert to exception? | |
$trace = debug_backtrace(); | |
trigger_error( | |
'Undefined property via __get(): ' . __CLASS__ . '::$' . $name . ' in ' . $trace[0]['file'] . ' on line ' . $trace[0]['line'], | |
E_USER_NOTICE | |
); | |
return null;/**/ | |
} | |
/** | |
* Template render function | |
* | |
* @return string | |
*/ | |
protected function render() | |
{ | |
extract($this->templateVars);//, EXTR_REFS); | |
$menu = $this->renderFile(cfg\BASE_DIR . cfg\LAYOUT_DIR . 'menu.phtml'); | |
$content = $this->renderFile($this->viewDir . $this->viewFile . $this->viewExtension); // @TODO FIX ME FIX ME FIX ME, FILE INCLUDE | |
// Using output buffers to capture the output of the included files | |
ob_start(); | |
if(!isset($noLayout) || $noLayout === false) { | |
include cfg\BASE_DIR . cfg\LAYOUT_DIR . 'layout.phtml'; | |
} | |
else { | |
echo $content; | |
} | |
return ob_get_clean(); | |
} | |
/** | |
* Render a single file | |
* @return string | |
*/ | |
protected function renderFile($theFile) | |
{ | |
extract($this->templateVars);//, EXTR_REFS); | |
if(isset($_POST)) { extract($_POST); }//, EXTR_REFS); | |
ob_start(); | |
include $theFile; | |
return ob_get_clean(); | |
} | |
public function __toString() | |
{ | |
return $this->render(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment