<? Php
Class MyTpl {
Private $ template_dir;
Private $ compile_dir;
Private $ tpl_vars = array ();
Public function _ construct ($ template_dir = "./templates", $ compile_dir = "./templates_c "){
$ This-> template_dir = rtrim ($ template_dir ,"/").'/';
$ This-> compile_dir = rtrim ($ compile_dir ,"/").'/';
}
Public function assign ($ tpl_var, $ value = null ){
If ($ tpl_var! = "")
$ This-> tpl_vars [$ tpl_var] = $ value;
}
Public function display ($ fileName ){
$ TplFile = $ this-> template_dir. $ fileName;
If (! File_exists ($ tplFile )){
Return false;
}
$ ComFileName = $ this-> compile_dir. "com _". $ fileName. ". php ";
If (! File_exists ($ comFileName) | filemtime ($ comFileName) <filemtime ($ tplFile )){
$ RepContent = $ this-> tpl_replace (file_get_contents ($ tplFile ));
File_put_contents ($ comFileName, $ repContent );
}
Include $ comFileName;
}
Private function tpl_replace ($ content ){
$ Pattern = array (
'/\ <\ {\ S * \ $ ([a-zA-Z _ \ x7f-\ xff] [a-zA-Z0-9 _ \ x7f-\ xff] *) \ s * \} \>/I'
);
$ Replacement = array (
'<? Php echo $ this-> tpl_vars ["$ {1}"];?> '
);
$ RepContent = preg_replace ($ pattern, $ replacement, $ content );
Return $ repContent;
}
}
?>
From: column of enough_br