Post by Eric on Mar 5, 2006 2:18:47 GMT
class Template_Controller
{
/* Contains template variables that are assigned to the template */
var $temp_vars = array();
/* Path to the file files */
var $file_dir;
/* Delimeters for variables */
var $var_dels = array();
/* File extension */
var $file_ext = ".tpl";
/* Delimeters for files */
var $file_dels = array();
/* The variable to store what the page has in/on it. */
var $page_output = "";
/* Base file */
var $base = "main";
/*
* Constructor for class, sets up base information.
* argument files - takes in the location of files
*/
function Template_Controller($files)
{
$this -> file_dir = $files;
$this -> var_dels["start"] = $this -> escape('[[');
$this -> var_dels["end"] = $this -> escape(']]');
$this -> file_dels["start"] = $this -> escape('{{');
$this -> file_dels["end"] = $this -> escape('}}');
}
/* Escapes all things as to make the regexp available */
function escape($val)
{
return preg_replace('/(.)/', "\\\\$1", $val);
}
/* Adds a variable to $temp_vars */
function add_var($key, $val)
{
$this -> temp_vars[$key] = $val;
}
/*
* The main parser.
* Coordinates all other points to create the page
*/
function parser($file)
{
if(file_exists($this -> file_dir . $file . $this -> file_ext))
{
$temp = file_get_contents($this -> file_dir . $file . $this -> file_ext);
$temp = $this -> var_parse($temp);
$temp = $this -> temp_parse($temp);
return $temp;
}
return 0;
}
/* Outputs the page to the screen */
function write_page($file = "")
{
if($file == "")
$file = $this -> base;
$this -> page_output = $this -> parser($file);
if(!$this -> page_output)
print "Error in writing page. {$this -> file_dir}{$file}{$this -> file_ext} does not exist.";
else
print $this -> page_output;
}
/* Replaces variables with their values */
function var_parse($temp)
{
while(preg_match("/" . $this -> var_dels["start"] . "(\w+)" . $this -> var_dels["end"] . "/",
$temp, $matches))
{
$temp = str_replace($matches[0], $this -> temp_vars[$matches[1]], $temp);
}
return $temp;
}
/* Replaces template statements with template content */
function temp_parse($temp)
{
$holder = "";
while(preg_match("/" . $this -> file_dels["start"] . "(\w+)" . $this -> file_dels["end"] . "/",
$temp, $matches))
{
$holder = $this -> parser($matches[1]);
if(!$holder)
$holder = "Error in adding file. File {$this -> file_dir}{$file}{$this -> file_ext} does not exist.";
$temp = str_replace($matches[0], $holder, $temp);
}
return $temp;
}
}
Made this for a PHP project that I'm working on, eventually I realized that it would just be easier to use Smarty.
{
/* Contains template variables that are assigned to the template */
var $temp_vars = array();
/* Path to the file files */
var $file_dir;
/* Delimeters for variables */
var $var_dels = array();
/* File extension */
var $file_ext = ".tpl";
/* Delimeters for files */
var $file_dels = array();
/* The variable to store what the page has in/on it. */
var $page_output = "";
/* Base file */
var $base = "main";
/*
* Constructor for class, sets up base information.
* argument files - takes in the location of files
*/
function Template_Controller($files)
{
$this -> file_dir = $files;
$this -> var_dels["start"] = $this -> escape('[[');
$this -> var_dels["end"] = $this -> escape(']]');
$this -> file_dels["start"] = $this -> escape('{{');
$this -> file_dels["end"] = $this -> escape('}}');
}
/* Escapes all things as to make the regexp available */
function escape($val)
{
return preg_replace('/(.)/', "\\\\$1", $val);
}
/* Adds a variable to $temp_vars */
function add_var($key, $val)
{
$this -> temp_vars[$key] = $val;
}
/*
* The main parser.
* Coordinates all other points to create the page
*/
function parser($file)
{
if(file_exists($this -> file_dir . $file . $this -> file_ext))
{
$temp = file_get_contents($this -> file_dir . $file . $this -> file_ext);
$temp = $this -> var_parse($temp);
$temp = $this -> temp_parse($temp);
return $temp;
}
return 0;
}
/* Outputs the page to the screen */
function write_page($file = "")
{
if($file == "")
$file = $this -> base;
$this -> page_output = $this -> parser($file);
if(!$this -> page_output)
print "Error in writing page. {$this -> file_dir}{$file}{$this -> file_ext} does not exist.";
else
print $this -> page_output;
}
/* Replaces variables with their values */
function var_parse($temp)
{
while(preg_match("/" . $this -> var_dels["start"] . "(\w+)" . $this -> var_dels["end"] . "/",
$temp, $matches))
{
$temp = str_replace($matches[0], $this -> temp_vars[$matches[1]], $temp);
}
return $temp;
}
/* Replaces template statements with template content */
function temp_parse($temp)
{
$holder = "";
while(preg_match("/" . $this -> file_dels["start"] . "(\w+)" . $this -> file_dels["end"] . "/",
$temp, $matches))
{
$holder = $this -> parser($matches[1]);
if(!$holder)
$holder = "Error in adding file. File {$this -> file_dir}{$file}{$this -> file_ext} does not exist.";
$temp = str_replace($matches[0], $holder, $temp);
}
return $temp;
}
}
Made this for a PHP project that I'm working on, eventually I realized that it would just be easier to use Smarty.