Добър пример. Ети и моят:
<?php
/*
****************************************************************************
* template class *
* Version 3 *
* *
* Copyright (C) 2004 - 2005 by Roumen Georgiev - edembg@edemarts.com *
* *
* Author: *
* Roumen Georgiev, Sofia, Bulgaria, edembg@edemarts.com *
* *
****************************************************************************
*/
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
/*
Sintaxis
variables - <!--%VarName%--> or <!--%VarName[index]%-->
constants - [!--%ConstantName%--!]
if construct - {?if expression?} Action {?/if?}
{?if $c == 1?}PPPPP{?/if?}<br> {?if !$c?}NNNNN{?/if?}
loops {!loop indexName $VarName!} Action {!/loop!}
{!loop i $d!}
{?if {*index*}%2?} 1 {?/if?}{?if !{*index*}%2?} 0 {?/if?}<br>
<!--%d[i]%-->
{!/loop!}
*/
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
class template
{
var $vars = array(), $template, $content;
var $indent = 0, $indent_str = "";
var $loop_stack, $loop_point;
function template($template)
{
$this->template = $template;
}
function set_indent($indent)
{
$this->indent = $indent;
$this->indent_str = "";
for ($i = 0; $i < $indent; $i++)
{
$this->indent_str .= "\t";
}
}
function inc_indent()
{
$this->indent_str .= "\t";
}
function dec_indent()
{
$this->indent_str = substr($this->indent_str, 1);
}
function tbl_row_beg($prop = "")
{
$code = $this->indent_str."<tr";
if ($prop != "") $code .= ' '.$prop;
$code .= ">\n";
return ($code);
}
function tbl_row_end()
{
$code = $this->indent_str."</tr>\n";
return ($code);
}
function table($values, $property, $row_prop = array())
{
$code = "";
$j = 0;
$i = 0;
foreach ($values as $bval)
{
$this->dec_indent();
if (sizeof($row_prop) == 0) $code .= $this->tbl_row_beg();
else {
$code .= $this->tbl_row_beg($row_prop[$j]);
$j++;
if ($j == sizeof($row_prop)) $j = 0;
}
$this->inc_indent();
foreach ($bval as $sval)
{
$code .= $this->indent_str.'<td';
if ($property[$i] != '') $code .= ' '.$property[$i];
$code .= '>'.$sval."</td>\n";
$i++;
if ($i == sizeof($property)) $i = 0;
}
$this->dec_indent();
$code .= $this->tbl_row_end();
$this->inc_indent();
}
return ($code);
}
function option($content, $selected)
{
$out = "";
foreach ($content as $key => $val)
{
$out .= $this->indent_str.'<option value="'.$key.'"';
if (in_array($key, $selected)) $out .= ' selected="selected"';
$out .= '>'.$val.'</option>'."\n";
}
return ($out);
}
function select($name, $size, $params, $content, $selected)
{
$ret = $this->indent_str.'<select name="'.$name.'" size="'.$size.'"';
if ($params != "") $ret .= " ".$params;
$ret .= ">\n";
$this->inc_indent();
$ret .= $this->option($content, $selected);
$this->dec_indent();
$ret .= "</select>\n";
return ($ret);
}
function image($path, $alt,$height, $width, $border = 0, $align = "", $end = "")
{
$image = $this->indent_str.'<img src="'.$path.'" alt="'.$alt.'" height="'.$height.'" width="'.$width.'" border="'.$border.'"';
if ($align != "") $image .= ' align="'.$align.'"';
$image .= ' />'.$end;
return($image);
}
function link($URL, $label, $class = "", $title = "", $target = "", $end = "")
{
$javascript_event= (strtolower($label)=="delete") ? true : false;
$res = $this->indent_str.'<a href="'.$URL.'"';
if ($title != "") $res .= ' title="'.$title.'"';
if ($target != "") $res .= ' target="'.$target.'"';
if ($class != "") $res .= ' class="'.$class.'"';
$res .= ($javascript_event) ? " onClick='if (!confirm(\"Are you sure?\")){ return false;}'" : "";
$res .= '>'.$label.'</a>'.$end;
return ($res);
}
function checkbox($name, $value, $checked = false, $end = "")
{
$res = '<input type="checkbox" name="'.$name.'" value="'.$value.'"';
if ($checked) $res .= ' checked';
$res .= '>'.$end;
return ($res);
}
function input($type, $name, $value = "", $size = "", $extra = "", $end = "")
{
$res = '<input type="'.$type.'" name="'.$name.'"';
if ($value != "") $res .= ' value="'.$value.'"';
if ($size != "") $res .= ' size="'.$size.'"';
if ($extra != "") $res .= ' '.$extra;
$res .= '>'.$end;
return ($res);
}
function var_set($name, $value)
{
$this->vars[$name] = $value;
}
function mk_eval($str)
{
// $obuf = "\$ev = (";
$obuf = "(";
$buf = explode(" ", $str);
foreach ($buf as $val)
{
if (false === strpos($val, "$")) $obuf .= $val;
else {
$val = str_replace("$", "", $val);
if ($pos = strpos($val, "["))
{
$c = substr($val, 0, $pos);
$d = substr($val, $pos);
$ev = " \$this->vars[".$c."]".$d;
} else $ev = " \$this->vars[".$val."]";
$obuf .= $ev." ";
}
}
$obuf .= ") ? true : false;";
//print $obuf."<br>\n";
return ($obuf);
}
function compile()
{
$this->content = "";
$stack = array();
$cur_bool = $wr = true;
$fp = fopen($this->template, 'r');
if (!$fp) return("Error opening template");
while(!FEOF($fp))
{
$this->content .= fread($fp, 4096);
}
fclose($fp);
//++++++++++++++++++++++++++++++++++++++++++++++++++
$this->loop_stack = array();
$this->loop_point = 0;
$buf1 = preg_split("[({!loop)|(/loop!})]", $this->content);
$this->content = "";
foreach ($buf1 as $val)
{
$pos = strpos($val, "!}");
$r_pos = strpos($val, "{!");
if ($this->loop_point == 0 && !$pos && false === $r_pos) $this->content .= $val;
else {
if (!$pos && false === $r_pos) $this->l_add($val);
else {
if ($pos)$this->l_push($val);
if (false !== $r_pos)
{
if (!$pos) $this->l_add($val);
$this->l_pop();
} else if (!$pos)$this->l_add($val);
}
}
}
//++++++++++++++++++++++++++++++++++++++++++++++++++
//print $this->content;
$buf1 = preg_split("[({\?if)|(/if\?})]", $this->content);
//print_r($buf1);
$this->content = "";
//$count = 0;
foreach ($buf1 as $val)
{
//$count++;
//print "<br>".$count."<br>\n";
//if ($count > 9) print $val."<br>\n";
//print "<br>$count<br>------------<br>\nlen = ".strlen($val)."<br>".($cur_bool?"yes":"no")."<br>\n";
//var_dump($stack);
$pos = strpos($val, "?}");
$r_pos = strpos($val, "{?");
//if ($r_pos == strlen($val) - 2) $r_pos = false;
if ($cur_bool && false === $pos && false === $r_pos) $this->content .= $val;
else {
//print "<br>\n".$val."<br>\n";
if ($cur_bool)
{
if ($pos)
{
$sw = $this->mk_eval(trim(substr($val, 0, $pos)));
//var_dump($sw);
//print "<br>\n";
//print "\$sw = ".$sw."<br>\n";
eval("\$sw = ".$sw);
//if ($sw) print "true<br>\n";
//else print "false<br>\n";
if ($sw)
{
if (false === $r_pos)
{
//print "Push1 - $val - $pos - $r_pos - ".strlen($val).($cur_bool ? " v" : " h")."<br>\n";
array_push($stack, $sw);
$cur_bool = $sw;
$this->content .= substr($val, $pos + 2);
} else {
//print "Out after Push1<br>\n";
$this->content .= str_replace("{?", "", substr($val, $pos + 2));
}
$wr = false;
} else {
if (false === $r_pos)
{
//print "Push2 - $val - $pos - $r_pos - ".strlen($val).($cur_bool ? " v" : " h")."<br>\n";
// array_push($stack, $sw);
array_push($stack, $cur_bool);
$cur_bool = $sw;
}
$wr = false;
}
}
if ($wr)
{
if (false !== $r_pos)
{
if (sizeof($stack) > 0)
{
//print "Pop1 - <br>\n";
$cur_bool = array_pop($stack);
}
else $cur_bool = true;
$this->content .= substr($val, 0, $r_pos);
} else {
$this->content .= $val;
}
} else $wr = true;
} else {
//print "pos = ".$pos.", r_pos = ".$r_pos.", N r_pos".strpos($val, "?}")."<br>\n";
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//var_dump($stack); print "<br>\n";
if (false !== $pos)
{
if (false === $r_pos)
{
//print "Push3 - $val - $pos - $r_pos - ".strlen($val).($cur_bool ? " v" : " h")."<br>\n";
array_push($stack, $cur_bool);
}
} else {
if (false !== $r_pos)
{
if (sizeof($stack) > 0)
{
//print "Pop2 - $val - $pos - $r_pos - ".strlen($val).($cur_bool ? " v" : " h")."<br>\n";
$cur_bool = array_pop($stack);
}
else $cur_bool = true;
}
}
/*
if (false !== $pos)
{
if (false === $r_pos)
{
print "Push3 - $val - $pos - $r_pos - ".strlen($val).($cur_bool ? " v" : " h")."<br>\n";
array_push($stack, $cur_bool);
}
}
if (false !== $r_pos)
{
if (sizeof($stack) > 0)
{
print "Pop2 - $val - $pos - $r_pos - ".strlen($val).($cur_bool ? " v" : " h")."<br>\n";
$cur_bool = array_pop($stack);
}
else $cur_bool = true;
}
*/
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
}
}
}
//++++++++++++++++++++++++++++++++++++++++++++++++++
//print $this->content;
$buf1 = explode("<!--%", $this->content);
$this->content = "";
foreach ($buf1 as $value)
{
if (!strpos($value, "%-->")) $this->content .= $value;
else {
$buf2 = explode("%-->", $value);
if ($pos = strpos($buf2[0], "["))
{
$c = substr($buf2[0], 0, $pos);
$d = substr($buf2[0], $pos);
$ev = "\$ev = \$this->vars['".$c."']".$d.";";
} else $ev = "\$ev = \$this->vars['".$buf2[0]."'];";
//print $ev;
eval($ev);
$this->content .= $ev.$buf2[1];
}
}
$buf1 = explode("[!--%", $this->content);
$this->content = "";
foreach ($buf1 as $value)
{
if (!strpos($value, "%--!]")) $this->content .= $value;
else {
$buf2 = explode("%--!]", $value);
$ev = "\$ev = @constant(\"".$buf2[0]."\");";
eval($ev);
$this->content .= $ev.$buf2[1];
}
}
return ($this->content);
}
function l_push($val)
{
$this->loop_stack[$this->loop_point] = str_replace("{!", "",$val);
$this->loop_point++;
}
function l_pop()
{
$this->loop_point--;
$buf = explode("!}", $this->loop_stack[$this->loop_point]);
$buf1 = explode(" ", trim($buf[0]));
$leter = trim($buf1[0]);
/*
$ev = "\$count = @sizeof(\$this->vars['".str_replace("$", "", $buf1[1])."']);";
eval($ev);
$buf2 = "";
for ($i = 0; $i < $count; $i++)
{
$tmp = str_replace("[".$leter."]", "[".$i."]", $buf[1]);
// $buf2 .= str_replace("[".$leter."]", "[".$i."]", $buf[1]);
$buf2 .= str_replace("{*index*}", $i, $tmp);
}
*/
$ev = "\$l_var = \$this->vars['".str_replace("$", "", $buf1[1])."'];";
eval($ev);
$buf2 = "";
if (is_array($l_var) && sizeof($l_var) != 0)
{
foreach ($l_var as $i => $a)
{
$tmp = str_replace("[".$leter."]", "[".$i."]", $buf[1]);
$buf2 .= str_replace("{*index*}", $i, $tmp);
}
}
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
if ($this->loop_point != 0)
{
$this->l_add($buf2);
} else {
$this->content .= $buf2;
}
unset($this->loop_stack[$this->loop_point]);
}
function l_add($val)
{
$this->loop_stack[$this->loop_point - 1] .= str_replace("{!", "",$val);
}
//==============OLD FUNCTIONS============================================================
function opt_tag($name, $size, $extra = "", $content, $selected, $disabled = 0)
{
$res = '<select name="'.$name.'" size="'.($size == "" || $size == 0? 1:$size).'"';
if ($extra != "") $res .= $extra;
if ($disabled != 0 ) $res .= ' disabled';
$res .= ">\n";
$res .= $this->option($content, $selected);
$res .= "</select>\n";
return $res;
}
}
?>
Също много проста машинка за шаблони. Надявам се да свърши работа на някого, както и на мен.