Template - Page rendering class - Updated
Posted: Mon Jan 28, 2008 9:51 am
Is this a proper way to do it?
EDIT: updated code again.
Code: Select all
<?php
class Page {
private $pageVars = array();
private $serverroot;
private $siteInfoLocation;
private $templateLocation;
public function __construct($serverroot) {
$this->serverroot = $serverroot;
$this->siteInfoLocation = $this->serverroot . 'siteinfo/';
$this->templateLocation = $this->serverroot . 'templates/';
$this->pageVars['body'] = '';
$this->pageVars['name'] = '';
$this->pageVars['title'] = '';
$this->pageVars['template'] = 'default';
$this->pageVars['navbar'] = array();
$this->pageVars['keywords'] = $this->loadfile('keywords');
$this->pageVars['description'] = $this->loadfile('description');
$this->pageVars['footer'] = $this->loadfile('footer');
}
public function __set($var, $value) {
//Return false if they are trying to set a nonexistant variable.
if(!isset($this->pageVars[$var])) {
return false;
}
//Return false if they are trying to set a variable to the wrong type.
if((is_string($this->pageVars[$var]) != is_string($value)) &&
(is_array($this->pageVars[$var]) != is_array($value))) {
return false;
}
$this->pageVars[$var] = $value;
}
private function removeSlashes($var) {
$var = preg_replace('/\\//', ':', $var);
return $var;
}
private function loadFile($file) {
$file = $this->removeSlashes($file);
$filepath = $this->siteInfoLocation . $file . '.txt';
$filesize = filesize($filepath);
if($filesize > 0) {
$data = file_get_contents($filepath);
} else {
$data = '';
}
return $data;
}
public function renderPage() {
$pageVars = $this->pageVars;
$pageVars['template'] = $this->removeSlashes($pageVars['template']);
$templatefile = $this->templateLocation . $pageVars['template'] . '.php';
require_once($templatefile);
}
}
?>