
17.08.2007, 20:57
|
|
Участник форума
Регистрация: 06.06.2006
Сообщений: 213
Провел на форуме: 1628290
Репутация:
474
|
|
на диске у себя нашел, не помню уже откуда -) Сам часто пользуюсь... Мб кому надо
PHP код:
<?
/***************************************************************
Template class for splitting code and design
****************************************************************
Usage:
//creating an object
$Template=new Template("path/to/template.tpl");
//or $Template=new Template("template string",1);
//assigning variables
$Template->a("CONTENT", "This is the Template class test!");
//printing
$Template->p();
also it's possible to get parsed text without printing it:
$content=Template->parse();
****************************************************************/
class Template{
//constructor
function Template($path, $from_str=0){
$this->error="";
if(!$from_str){
if(!file_exists($path)){
$this->error="File not found: <b>{$_SERVER[BASE_URL]}/$path</b>";
//$this->str=$this->error;
return 0;
}//end if
$str=join("", file($path));
}else{//if(!$from_str)
$str=$path;
}//end if
$this->str=$str;
return 1;
}//end of constructor function
//assign
function a($var, $value){
$this->vars[$var]=$value;
}//end of assign function
//parse
function parse(){
$this->parsed_str=$this->str;
if(isset($this->vars))
foreach($this->vars as $k=>$v){
$this->parsed_str=str_replace('%'.$k.'%', $v, $this->parsed_str);
}
return $this->parsed_str;
}//end of parse function
//print
function p(){
echo $this->parse();
}//p
function dieIfError(){
if($this->error)die($this->error);
}//dieIfError
}//end of class description
?>
|
|
|