How can I improve the script below to be able to add global template parts such as start and end of the page?
<?php
class simpleTemplate {
var $variables;
var $variables_callback;
var $contents;
var $preg;
var $regexps;
// Constructor, initialize default variables
function simpleTemplate(){
// default sub regexp matches
$this->preg['var'] = '[a-zA-Z.\-_]{1,50}';
//INSERT REGEXP
$this->php_start = "\necho '";
$this->php_end = "';\n";
// Main find-replace regexps
$this->regexps = array(
// echo-variable statement: {(var)}
'echo' => array(
'search' => '~\{((?:'.$this->preg['var'].')(?:\.)?)\}~sU',
'replace' => "'.\$this->_get('\\1').'"
)
'include' => array(
'search' => '', // UPDATE
'replace' => "" // UPDATE
)
);
}
// Load a file and convert it into native PHP code
function load($file){
if (!is_readable($file))
return FALSE;
$this->file = $file;
$this->contents = NULL;
if (!isset($this->contents)){
$this->contents = file_get_contents($this->file);
$this->parse();
}
}
// Load a converted template, apply variables and echo the output
function show(){
ob_start();
eval($this->contents);
ob_end_flush();
}
// Main converter, call sub-convertors and perform some cleaning
function parse(){
$this->contents = str_replace("'", "\'", $this->contents);
$this->contents = $this->php_start.$this->contents.$this->php_end;
foreach ($this->regexps as $regexp)
do {
if (isset($contents))
$this->contents = $contents;
$contents = preg_replace($regexp['search'], $regexp['replace'], $this->contents);
} while ($contents != $this->contents);
$this->clean();
}
// Clean trash in generated PHP code
function clean(){
$this->contents = preg_replace("~([^\\\])''\.~", '\1', $this->contents);
$this->contents = str_replace(".''", "", $this->contents);
$this->contents = str_replace("\necho '';\n", "", $this->contents);
$this->contents = str_replace("else {}", "", $this->contents);
// Remove all whitespace from the template output
//$this->contents = preg_replace("~(\s{2,}|\n)~", '', $this->contents);
return;
}
// Get a variable from internal, or an external source
function _get($variableName){
$variable = @eval('return $this->variables[\''.str_replace('.', "']['", $variableName).'\'];');
if (!isset($variable)) {
if (isset($this->variables[$variableName]))
$variable = $this->variables[$variableName];
else {
// Support for an external variable-source
$function_name = $this->variables_function;
if (isset($variables_callback))
$variable = call_user_func($variables_callback, $variableName);
}
}
if (!isset($variable))
return FALSE;
else
return $variable;
}
// Set an internal variable
function _set($variableName, $value){
$this->variables[$variableName] = $value;
return TRUE;
}
}
?>
Örnek. Php sayfa benziyor
<?php
require_once('tpl.php');
$tpl = new simpleTemplate();
$data['user'] = array(
'name' => 'Denis',
'surname' => 'Bobrovnikov'
);
$tpl->variables = $data;
$tpl->load('index.tpl');
$tpl->show();
?>
Ve şablon şudur
My name is {user.name} {user.surname}
Ben şablon dosyasında bunu mümkün olmak istiyorum
{include=header.tpl}
My name is {user.name} {user.surname}
{include=footer.tpl}
burada header ve footer da çözümlenir