03-13-2011, 09:17 AM
This is a simple, yet really cool template system I made in PHP. It doesnt feature much other than loading templates and parsing data, I left it this way in case any of you wanted to develop the script further. It uses OO as you can see.
Some features:
Template.php
ParseTemplate.php
Example code:
Templates:
Outputs:
http://projectevolution.net76.net/tutori...mplatesys/
Enjoy!
Some features:
- Each template can include sub-templates!
- 4 variable types - more can be added easily!
- Each template can be parsed as complete HTML!
Template.php
PHP Code:
<?php
require('ParseTemplate.php');
/*
Represents each template
*/
class Template {
private static $getTemplatesLoaded = array();
private $parser = null;
private $template_data = array();
private $path = '';
/*
(Optional) $template is the name of template to automatically load
Creates Template object
*/
public function __construct($template = '') {
$this->parser = new ParseTemplate();
if ($template == '') {
return;
}
$this->path = getcwd() . '/templates/' . $template . '.template.php';
try {
$this->loadTemplate($template);
} catch (Exception $e) {
die($e->getMessage());
}
}
/*
$template is the name of the template file
Loads the template file and parses data into $template_data
*/
public function loadTemplate($template) {
if (!file_exists($this->path)) {
throw new Exception('Unable to load ' . $template . ' template!');
return false;
}
$this->template_data = array('name' => $template, 'filename' => $template . '.template.php');
$this->template_data['data'] = $this->parser->parseTemplate($this->path);
self::$getTemplatesLoaded[$template] = $this->template_data;
}
/*
$values should be an array containing: template variable name, Template object
Creates sub-template within template
*/
public function setSubTemplate($values) {
if (!is_array($values)) {
return false;
}
$variables = $this->template_data['data'];
for ($i = 0; $i < count($variables); $i++) {
$name = $variables[$i][1];
$type = $variables[$i][0];
if ($name != $values[0] || $type != 'template')
continue;
$this->parser->setVariable($name, $values[1], $type);
}
return $this->parser->getParsedTemplate();
}
/*
$values should be an array containing: template variable name, variable value
(Optional) $echo set to true to return the currently parsed template
Sets template variables
*/
public function setVariables($values, $echo = false) {
if (!is_array($values)) {
return false;
}
$variables = $this->template_data['data'];
for ($i = 0; $i < count($variables); $i++) {
$name = $variables[$i][1];
$type = $variables[$i][0];
if ($name != $values[0])
continue;
$this->parser->setVariable($name, $values[1], $type);
}
if ($echo == true)
return $this->getTemplate();
else
return true;
}
/*
Returns currently parsed template as string
*/
public function getTemplate() {
return $this->parser->getParsedTemplate();
}
/*
Print the template
*/
public function echoTemplate() {
echo $this->getTemplate();
}
/*
Prints all data located from $getTemplatesLoaded array
*/
public static function printAllTemplateData() {
print_r(self::$getTemplatesLoaded);
}
}
?>
ParseTemplate.php
PHP Code:
<?php
/*
Template parsing for each individual template
*/
class ParseTemplate {
private $template = '';
public function __construct() {
}
/*
$path is the path to the template file
Parses template variables and stores them in $variables
*/
public function parseTemplate($path) {
$variables = array();
$matches = array();
$this->template = @file_get_contents($path);
if (empty($this->template))
return $variables;
preg_match_all("#\{(raw|string|int|template)\:([^/]+)\}#", $this->template, $matches, PREG_SET_ORDER);
for ($i = 0; $i < count($matches); $i++) {
$variables[] = array($matches[$i][1], $matches[$i][2]);
}
return $variables;
}
/*
$variable_name is the name of the variable to set in template file
$value is the value to set for the variable
$type is the type of data
Sets variables in the template
*/
public function setVariable($variable_name, $value, $type) {
switch ($type) {
case 'int':
$value = is_numeric($value) ? (int) $value : 0;
$this->template = preg_replace("#\{(int)\:$variable_name\}#", $value, $this->template);
break;
case 'string':
$type = is_string($value) ? $value : strval($value);
$this->template = preg_replace("#\{(string)\:$variable_name\}#", $value, $this->template);
break;
case 'template':
$this->template = preg_replace("#\{(template)\:$variable_name\}#", $value->getTemplate(), $this->template);
break;
default:
$this->template = preg_replace("#\{(raw)\:$variable_name\}#", $value, $this->template);
break;
}
}
/*
Returns this template data
*/
public function getParsedTemplate() {
return $this->template;
}
}
?>
Example code:
PHP Code:
<?php
require('Template.php');
$template = new Template('index');
$sub_template = new Template('index_body');
$sub_sub_template = new Template('index_body_sub');
$sub_sub_template->setVariables(array('hello_idiets', 'Hello Idiets!'));
$sub_template->setSubTemplate(array('index_body_sub', $sub_sub_template));
$sub_template->setVariables(array('hello_world', 'Hello World!'));
$sub_template->setVariables(array('hello_world2', 'Hello World2!'));
$template->setSubTemplate(array('index_body', $sub_template));
$template->echoTemplate();
?>
Templates:
Code:
<html>
<head>
</head>
<body>
<p>my name is john etc</p>
{template:index_body}
<h3>damn</h3>
</body>
</html>
----- index_body.template.php ------
<p>This is from the index body template file!<p>
<h1>{string:hello_world}</h1> <h1>{string:hello_world2}</h1><br /><hr />{template:index_body_sub}<hr />
----- index_body_sub.template.php -----
<p>This is from the index body SUB template file!<p>
<h1>{string:hello_idiets}</h1>
Outputs:
http://projectevolution.net76.net/tutori...mplatesys/
Enjoy!