A very easy to use yet powerful and quick template engine. Enables you to seperate your page layout/design from your code. It can handle simple variable replacement and table building using two dimensional arrays and/or MySQL result sets (Meaning that given a single row in the template, this class can build an entire table of data). Performance is excellent. Has support for multiple template files.
Download
View Source (print view)
Rating : 4.7
Views : 317
<?php
/***************************************
** Title........: Template class
** Filename.....: class.template.inc
** Author.......: Richard Heyes
** Version......: 1.13
** Notes........:
** Last changed.: 05/07/2000
** Last change..: You can now have two
** different loops within
** the same html file with
** exactly the same loop code.
***************************************/
class template{
var $var_names = array();
var $files = array();
var $start = '{';
var $end = '}';
/***************************************
** Function to load a template into
** the class.
***************************************/
function load_file($file_id, $filename){
$this->files[$file_id] = fread($fp = fopen($filename, 'r'), filesize($filename));
fclose($fp);
}
/***************************************
** Function to load a template into
** the class.
***************************************/
function set_identifiers($start, $end){
$this->start = $start;
$this->end = $end;
}
/***************************************
** Function to register a variable(s).
***************************************/
function register($file_id, $var_name){
if(is_long(strpos($var_name, ',')) == TRUE){
$var_name = explode(',', $var_name);
for(reset($var_name); $current = current($var_name); next($var_name)) $this->var_names[$file_id][] = $current;
}else{
$this->var_names[$file_id][] = $var_name;
}
}
/***************************************
** Function for reading and parsing the
** html file for normal variables.
***************************************/
function parse($file_id){
if(isset($this->var_names[$file_id]) AND count($this->var_names[$file_id]) > 0){
for($i=0; $i<count($this->var_names[$file_id]); $i++){
$temp_var = $this->var_names[$file_id][$i];
global $$temp_var;
$this->files[$file_id] = str_replace($this->start.$temp_var.$this->end, $$temp_var, $this->files[$file_id]);
}
}
}
/***************************************
** Function for parsing an array.
***************************************/
function parse_loop($file_id, $array_name){
global $$array_name;
$loop_code = '';
$file = explode(chr(10), $this->files[$file_id]);
$start_pos = strpos(strtolower($this->files[$file_id]), '<loop name="'.$array_name.'">') + strlen('<loop name="'.$array_name.'">');
$end_pos = strpos(strtolower($this->files[$file_id]), '</loop name="'.$array_name.'">');
$loop_code = substr($this->files[$file_id], $start_pos, $end_pos-$start_pos);
$start_tag = substr($this->files[$file_id], strpos(strtolower($this->files[$file_id]), '<loop name="'.$array_name.'">'),strlen('<loop name="'.$array_name.'">'));
$end_tag = substr($this->files[$file_id], strpos(strtolower($this->files[$file_id]), '</loop name="'.$array_name.'">'),strlen('</loop name="'.$array_name.'">'));
if($loop_code != ''){
$new_code = '';
for($i=0; $i<count($$array_name); $i++){
$temp_code = $loop_code;
while(list($key,) = each(${$array_name}[$i])){
$temp_code = str_replace($this->start.$key.$this->end,${$array_name}[$i][$key], $temp_code);
}
$new_code .= $temp_code;
}
$this->files[$file_id] = str_replace($start_tag.$loop_code.$end_tag, $new_code, $this->files[$file_id]);
}
}
/***************************************
** Function for parsing an sql result
** set.
***************************************/
function parse_sql($file_id, $result_name){
global $$result_name;
$loop_code = '';
$start_pos = strpos(strtolower($this->files[$file_id]), '<loop name="'.$result_name.'">') + strlen('<loop name="'.$result_name.'">');
$end_pos = strpos(strtolower($this->files[$file_id]), '</loop name="'.$result_name.'">');
$loop_code = substr($this->files[$file_id], $start_pos, $end_pos-$start_pos);
$start_tag = substr($this->files[$file_id], strpos(strtolower($this->files[$file_id]), '<loop name="'.$result_name.'">'),strlen('<loop name="'.$result_name.'">'));
$end_tag = substr($this->files[$file_id], strpos(strtolower($this->files[$file_id]), '</loop name="'.$result_name.'">'),strlen('</loop name="'.$result_name.'">'));
if($loop_code != ''){
$new_code = '';
$field_names = array();
for($i=0; $i<mysql_num_fields($$result_name); $i++) $field_names[] = mysql_field_name($$result_name,$i);
while($row_data = mysql_fetch_array($$result_name, MYSQL_ASSOC)){
$temp_code = $loop_code;
for($i=0; $i<count($field_names); $i++){
$temp_code = str_replace($this->start.$field_names[$i].$this->end, $row_data[$field_names[$i]], $temp_code);
}
$new_code.= $temp_code;
}
$this->files[$file_id] = str_replace($start_tag.$loop_code.$end_tag, $new_code, $this->files[$file_id]);
}
}
/***************************************
** Function for printing the resulting
** file(s).
***************************************/
function print_file($file_id){
if(is_long(strpos($file_id, ',')) == TRUE){
$file_id = explode(',', $file_id);
for(reset($file_id); $current = current($file_id); next($file_id)) echo $this->files[$current];
}else{
echo $this->files[$file_id];
}
}
/***************************************
** Function for returning the resulting
** file(s).
***************************************/
function return_file($file_id){
$ret = '';
if(is_long(strpos($file_id, ',')) == TRUE){
$file_id = explode(',', $file_id);
for(reset($file_id); $current = current($file_id); next($file_id)) $ret .= $this->files[$current];
}else{
$ret .= $this->files[$file_id];
}
return $ret;
}
} // End of class
?>
php script" class="neww" target="_b, how to use this code, php_tools, yahoo_status_shopping_cart.php, php file upload, shopping_cart and user=0--, drop down, mysql_commit, forum, shopping_cart', backup, updateoraddimagesonpages, rtttl to 8 bit source code, stefan_hornea, rent_and_0=1_union_select, how to run php, php_script"_class="neww"_target="_blank"_title="im_neuen, embed_snippets, farah_aead, sql incection, antoine anik