With a data operation class, a project can only perform simple operations on data, but a good template engine is required to display beautiful pages with the artist. 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
[Html]
<? Php
Class template {
// Array of variable Storage
Private $ vars = array ();
// Template directory
Public $ template_dir = './template /';
// Cache directory
Public $ cache_dir = './cache /';
// Compile the 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 ', // <? =
);
/**
* Constructor
*/
Public function _ construct (){
If (defined ('tmp _ path ')){
$ This-> template_c_dir = TMP_PATH. 'template _ c /';
$ This-> cache_dir = TMP_PATH. 'cache /';
}
}
/**
* Variable 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 function 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 in 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 contents of the parser.
* @ Param $ content string read content
* @ Return $ token_content string content after replacement
*/
Public function token ($ content ){
$ Token_content = $ content;
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 Storage
* @ Param $ content string read content
*
Public function store_buff ($ content ){
$ This-> cache_file = md5 ($ this-> template_file). $ this-> template_file. '.html ';
$ Tempfile = $ this-> cache_dir. $ this-> cache_file;
$ Fp = fopen ($ tempfile, 'w ');
Fputs ($ fp, $ content );
Fclose ($ fp );
Unset ($ fp );
}
*/
/*
* Compilation and storage
* @ Param $ content string read content
*
*/
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 the 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;
}
} Www.2cto.com
}
?>
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.
Author: tomyjohn