After the last release of the Simplet Class (http://bbs.phpchina.com/thread-85257-1-1.html), I found a lot of flaws, such as not supporting multidimensional arrays, annotations and no caching function, this time I added these features!!!
If you are interested, I will take a tutorial next time to talk about my thoughts and methods of writing this class!!! Support words can keep abreast support me!
PHP Code:
simplet.class.php
Class Simplet {
Private $t _vars;
Private $templates _dir;
Private $templates _c_dir;
Private $cache;
Private $cache _dir;
Public Function __construct () {
$this->templates_dir =./templates/;
$this->templates_c_dir =./templates_c/;
$this->cache = 0;
$this->cache_dir =./cache/;
}
Public Function Setdir ($dir, $type = template) {
if (Is_dir ($dir)) {
if ($type = = template)
$this->templates_dir = RTrim ($dir,/).
ElseIf ($type = = Template_c)
$this->templates_c_dir = RTrim ($dir,/).
ElseIf ($type = = cache)
$this->cache_dir = RTrim ($dir,/).
Else
return false;
return true;
} else {
return false;
}
}
Public Function Cache ($time) {
if (Is_numeric ($time)) {
$this->cache = $time;
return true;
} else {
return false;
}
}
Public function assign ($var, $value = NULL) {
if (Is_array ($var)) {
foreach ($var as $key = = $val) {
$this->t_vars[$key] = $val;
}
} else {
$this->t_vars[$var] = $value;
}
}
Private Function Comp ($filename) {
try {
if (! $fp = fopen ($filename, R)) {
throw new Exception (Can not open. $filename);
}
$filesize = FileSize ($filename);
if ($filesize <= 0) {
throw new Exception (the file size must > 0);
}
$content = Fread ($fp, $filesize);
Fclose ($FP);
Unset ($FP);
$content = Preg_replace ("/<%= ([$a-za-z0-9_]{1,})%>/", " ", $content);
$content = Preg_replace ("/<% ([$a-za-z0-9_]{1,}). loop%>/", " \$$1_val) {?> ", $content);
$content = Preg_replace ("/<% ([$a-za-z0-9_]{1,}). Loop (([$a-za-z0-9_]{1,})]%>/", " ", $content);
$content = Preg_replace ("/<% ([$a-za-z0-9_]{1,}). Loop (([$a-za-z0-9_]{1,}), ([$a-za-z0-9_]{1,})]%>/", " \$$3) {?> ", $content);
$content = Preg_replace ("/<% ([$a-za-z0-9_]{1,}). key%>/", " ", $content);
$content = Preg_replace ("/<% ([$a-za-z0-9_]{1,}). value%>/", " ", $content);
$content = Preg_replace ("/<% ([$a-za-z0-9_]{1,}) \?%>/", " ", $content);
$content = Preg_replace ("/<%end%>/", " ", $content);
$content = Preg_replace ("/<%# #common ##%> ([^<%# #end ##%>]{0,}) <%# #end ##%>/", " ", $content);
if (Preg_match_all ("/<%{([^ (}%>)]{1,})}%>/", $content, $files)) {
$this->comp ($this->templates_dir $files [1][0]);
}
$content = Preg_replace ("/<%{([^ (}%>)]{1,})}%>/", " templates_c_dir}simplet_comp_$1.php;?> ", $content);
Echo $content;
$fp = fopen ($this->templates_c_dir. Simplet_comp_. BaseName ($filename): PHP, W);
if (!fwrite ($FP, $content)) {
throw new Exception (Can not write content in the. $filename);
&
http://www.bkjia.com/PHPjc/508319.html www.bkjia.com true http://www.bkjia.com/PHPjc/508319.html techarticle after the last release of the Simplet Class (http://bbs.phpchina.com/thread-85257-1-1.html), I found a lot of flaws, such as not supporting multidimensional arrays, annotations and no caching capabilities, this time I put ...