Here is my template code
<?php
namespace classes;
/**
* Description of Template
*
* @author Aamir
*/
class Template {
private
$templateVars,
$templateFile
;
protected static
$templateFolder,
$masterTemplate,
$masterInstance,
$placeholder,
$path_dir,
$path_web
;
/**
* Constructor for the template
* @param string $file file path
* @param array $args associative array
*/
public function __construct($file, $args = array()) {
$this->templateFile = self::$path_dir . self::$templateFolder . '/' . $file . '.phtml';
$this->templateVars = $args;
}
/**
* Magic method to get the array element
* @param string $name template variable name to fetch
* @return mixed
*/
public function __get($name) {
if (array_key_exists($name, $this->templateVars)) {
return $this->templateVars[$name];
} else {
return '';
}
}
/**
* Magic method to set template var
* @param string $name
* @param mixed $value
*/
public function __set($name, $value) {
$this->templateVars[$name] = $value;
}
/**
* output the template to a string
* @return string
*/
public function __toString() {
ob_start();
include $this->templateFile;
return ob_get_clean();
}
/**
* Function to initialize the template
* @param string $templatePath Template Path
*/
public static function setTemplate($templatePath) {
self::$templateFolder = $templatePath;
}
/**
* Get current template path
* @return string
*/
public static function getTemplate() {
return self::$templateFolder;
}
/**
* Get current template path from root
* It will print the path. To be used in image and file paths
*/
public static function getPath() {
echo self::$path_web . self::$templateFolder . '/';
}
/**
* Set Master Template for project
*
* @param string $masterTemplate master template file name from template folder
*/
public static function setMaster($masterTemplate) {
self::$masterTemplate = $masterTemplate;
self::$placeholder = new Placeholder();
}
/**
* Set the Paths for root directory, and URL based path
* @param string $PATH_WEB URL Path e.g. http://localhost/project1/abc/
* @param string $PATH_DIR Physical path e.g. /home/userA/www/project1/abc/
*/
public static function setPaths($PATH_WEB, $PATH_DIR) {
self::$path_web = $PATH_WEB;
self::$path_dir = $PATH_DIR;
}
/**
* Get the instance of Placeholder object
* @return object
*/
public static function getMasterPlaceHolder() {
return self::$placeholder;
}
/**
* Output the template to browser
*/
public function render() {
$master = new Template(self::$masterTemplate, array(
'placeholder' => self::$placeholder,
'content' => $this
));
echo $master;
}
}
Placeholder.php used in master template
<?php
namespace classes;
/**
* Description of Placeholder
*
* @author aamir
*/
class Placeholder {
private $templateVars;
public function __get($name) {
if (array_key_exists($name, $this->templateVars)) {
return $this->templateVars[$name];
} else {
return '';
}
}
public function __set($name, $value) {
$this->templateVars[$name] = $value;
}
}
USAGE
code from config.php
/**
* Default Template folder and languages
* setTemplate (Template folder);
*/
classes\template::setTemplate('template/template1');
classes\template::setMaster('master');
define('TEMPLATE_PATH', classes\template::getTemplate() . '/');
Pages.php
<?php
require 'config.php';
$template = new \classes\template('page');
$template->page_content = $pageData;
$template->render(); // will output including master template
// echo $template; // will output only the page template excluding master layout for AJAX out put
Pages.php
To change the contents of Master layout e.g. Change of Page title etc
$placeholder = \classes\template::getMasterPlaceHolder();
$placeholder->pageTitle = $cur_page['page_title'];
$placeholder->cur_page = getParam('p');
$template = new \classes\template('page');
$template->page_content = $pageData;
$template->render(); // will output including master template
// echo $template; // will output only the page template excluding master layout for AJAX out put
page.phtml file inside the template/template1 folder
<div class="left" style="width: 845px;"><?php
echo $this->page_content; // this will render page contents
?>
</div>
master.phtml (we need to define placeholders in the template)
<!DOCTYPE html>
<html>
<head>
<title><?php echo $this->placeholder->pageTitle ?></title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link rel="stylesheet" href="<?php echo TEMPLATE_PATH ?>css/style.css" />
<link rel="stylesheet" href="<?php echo TEMPLATE_PATH ?>css/fluid_grid.css" />
<link type="text/css" href="<?php echo TEMPLATE_PATH ?>css/jquery.jscrollpane.css" rel="stylesheet" media="all" />
<script type="text/javascript" src="<?php echo TEMPLATE_PATH ?>js/jquery-1.7.1.min.js"></script>
<script type="text/javascript" src="<?php echo TEMPLATE_PATH ?>js/jquery.tooltip.pack.js"></script>
<script type="text/javascript" src="<?php echo TEMPLATE_PATH ?>js/jquery.jscrollpane.min.js"></script>
<script type="text/javascript" src="<?php echo TEMPLATE_PATH ?>js/jquery.mousewheel.js"></script>
<script type="text/javascript" src="<?php echo TEMPLATE_PATH ?>js/jquery-ui-1.8.18.custom.min.js"></script>
</head>
<body>
<?php echo $this->placeholder->beforeContent?>
<div class="wrapper">
<?php echo $this->content ?>
</div>
</body>
</html>
So basically, we set the template folder, and set template master file, and then where we have to display content we used $template = new template('templatefile')
(it will add .phtml
auto) and if we want full template including master we use $template->render()
or if we just want to output without master for example for an AJAX request we simply echo it echo $template
.
Please let me know how can I improve it.