A simple template class--compiled (Simplet class enhanced) _php tutorial

Source: Internet
Author: User
Tags php foreach rtrim

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 ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.