An easy template class (PHP)

Source: Internet
Author: User
A simple template class (PHP) is connected with a data operation class, so that the project can only perform simple data operations, but to display beautiful pages together with the artist, a good template engine is required. Compared with a relatively large template engine like SMARTY, I think the following is actually much smaller. This template class was previously seen on the Internet and is well written, so I referenced it. I still don't know who the author is. here I will talk about the principle of this class first. First, this class has only one simple template class (PHP)

Next, there is a data operation class, so that the project can only operate data simply, but to achieve a beautiful page that can be displayed with the artist, a better template engine is required. Compared with a relatively large template engine like SMARTY, I think the following is actually much smaller.

This template class was previously seen on the Internet and is well written, so I referenced it. I still don't know who the author is. here I will talk about the principle of this class first.

First, this class only has a simple regular parser. But it can basically be used. If we can expand on this basis, I believe this little thing is very promising. Please join us with the comrades who share the same interests to strengthen his goal. I am leaving the bricks.

Template. class. php

<? Phpclass template {// array of variable Storage private $ vars = array (); // template directory public $ template_dir = '. /template/'; // cache directory public $ cache_dir = '. /cache/'; // compilation directory public $ template_c_dir = '. /template_c/'; // template file public $ template_file = ''; // left connector public $ left_delimiter =' <{'; // right connector public $ right_delimiter = '}>'; // compile the file private $ template_c_file = ''; // cache file private $ cache_file = ''; // cache time public $ cache_time = 0; // built-in parser private $ preg _ Temp = array ('~ <\ {(\ $[A-z0-9 _] +) \}> ~ I '=>' <? Php echo $1;?> ', // <{$ Name}> '~ <\ {(\ $[A-z0-9 _] +) \. ([a-z0-9 _] +) \}> ~ I '=>' <? Php echo $1 [\ '$2 \'];?> ', // <{$ Arr. key}> '~ <\ {(\ $[A-z0-9 _] +) \. ([a-z0-9 _] +) \. ([a-z0-9 _] +) \}> ~ I '=>' <? Php echo $1 [\ '$2 \'] [\ '$3 \'];?> ', // <{$ Arr. key. key2}> '~ <\? Php \ s + (include_once | require_once | include | require) \ s * \ (\ s * (. + ?) \ S * \) \ s *;? \ S * \?> ~ I '=>' <? Php include \ $ this-> _ include ($2);?> ', // <? Php include ('Inc/top. php');?> '~ <\ {: (. + ?) \}> ~ '=>' <? Php echo $1;?> ', // <{: Strip_tags ($ a)}> '~ <\{\~ (. + ?) \}> ~ '=>' <? Php $1;?> ', // <{~ Var_dump ($ a)}> '~ <\? = \ S *~ '=>' <? Php echo ', // <? =);/*** Construct */public function _ construct () {if (defined ('tmp _ path') {$ this-> template_c_dir = TMP_PATH. 'Template _ c/'; $ this-> cache_dir = TMP_PATH. 'cache/';}/*** variable value assignment * @ param $ key mixed key name * @ param $ value mixed value */public function assign ($ key, $ value = '') {if (is_array ($ key) {$ this-> vars = array_merge ($ key, $ this-> vars );} else {$ this-> vars [$ key] = $ value;}/** display page * @ param $ file string template file name */public fun Ction display ($ file) {echo $ this-> fetch ($ file );} /*** return cache content * @ param $ file string template file name * @ return $ content string cached content */public function fetch ($ file) {$ this-> template_file = $ file; $ desc_template_file = $ this-> template_dir. $ file; $ desc_content = $ this-> readfile ($ desc_template_file); $ template_c_file_time = filemtime ($ desc_template_file); // if the cache time is exceeded, compile if ($ this-> cache_time <time ()-$ template_c_file_time ){ $ This-> complie ($ this-> token ($ desc_content);} // Obtain the content of the cache area ob_start (); @ extract ($ this-> vars, EXTR_OVERWRITE); include ($ this-> template_c_dir. $ this-> template_c_file); $ content = ob_get_contents (); ob_end_clean (); // $ this-> store_buff ($ content); return $ content ;} /** replace the separator and the parser content * @ param $ content string read content * @ return $ token_content string replaced content */public function token ($ content) {$ token_content = $ cont Ent; if ($ left_delimiter! = '<{') {$ Token_content = str_replace ($ left_delimiter, '<{', $ token_content); $ token_content = str_replace ($ right_delimiter, '}> ', $ token_content) ;}$ token_content = preg_replace (array_keys ($ this-> preg_temp), $ this-> preg_temp, $ token_content); return $ token_content ;} /** generate and store the content read by @ param $ content string * public function store_buff ($ content) {$ this-> cache_file = md5 ($ this-> template_file ). $ this-> t Emplate_file. '.html '; $ tempfile = $ this-> cache_dir. $ this-> cache_file; $ fp = fopen ($ tempfile, 'w'); fputs ($ fp, $ content); fclose ($ fp ); unset ($ fp);} * // ** compile and store the content read by @ param $ content string **/public function complie ($ content) {$ this-> template_c_file = md5 ($ this-> template_file ). $ this-> template_file. '. php'; $ tempfile = $ this-> template_c_dir. $ this-> template_c_file; $ fp = fopen ($ tempfile, 'w'); fputs ($ Fp, $ content); fclose ($ fp); unset ($ fp );} /** read file content * @ param $ file string file name * @ return $ content string file content */public function readfile ($ file) {if (file_exists ($ file )) {$ fp = fopen ($ file, 'r'); $ content = ''; while (! Feof ($ fp) {$ content. = fgets ($ fp, 4096);} fclose ($ fp); unset ($ fp); return $ content;} else {exit ($ file. 'Not exist! ') ;}}/** Template nesting * @ param $ file string file name * @ return absolute address of the string file */public function _ include ($ file) {if (file_exists ($ this-> template_dir. $ file) {return ($ this-> template_dir. $ file) ;}else {echo "the template file does not exist"; exit ;}}?>
With this template, you can forget those large templates. after all, your artist won't write so many SMARTY tags in his slice file, as long as he cut it, you can use it directly without modifying the images, css, and js addresses.

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.