A php template mainly wants to reflect the idea. Idea: to strike a balance between speed and ease of use (mainly referring to the convenience of the artist's design), we adopted the method of generating php files from html files (compile ?) Also want to display the separator in the separation Ideas:
To achieve a balance between speed and ease of use (mainly referring to the convenience of the art design), we adopted the method of generating php files from html files (Compilation ?)
I also want to balance the separation of display logic and html code.
For example, a forum homepage (index. php ):
Code: |
Require ('./template. php '); // The prefix of PHP files generated by html. different styles are used. $ Tpl_prefix = 'default '; // Template file name $ Tpl_index = 'index ';
$ Tpl = new Template ($ tpl_prefix );
$ Cats = array ( Array ('forum _ id' => '1', 'Forum _ cat_id '=> '0', 'Forum _ name' => 'php learn '), Array ('forum _ id' => '2', 'Forum _ cat_id '=> '0', 'Forum _ name' => 'MySQL learn ') ); $ Forums = array ( Array ('forum _ id' => '3', 'Forum _ cat_id '=> '1', 'Forum _ name' => 'php advanced tutorial '), Array ('forum _ id' => '4', 'Forum _ cat_id '=> '1', 'Forum _ name' => 'php '), Array ('forum _ id' => '5', 'Forum _ cat_id '=> '2', 'Forum _ name' => 'MySQL related information ') );
If ($ cats) { If ($ tpl-> chk_cache ($ tpl_index) // Check whether you need to re-produce the PHP template file. { $ Tpl-> load_tpl ($ tpl_index); // load the html template file. // Replace the PHP statement $ Tpl-> assign_block ("{block_cat }"," "); $ Tpl-> assign_block ("{/block_cat }"," "); $ Tpl-> assign_block ("{block_forum }"," \ Nif (\ $ forum ['forum _ cat_id '] =\$ cat ['forum _ id']) {?> "); $ Tpl-> assign_block ("{/block_forum }"," "); // Generate the PHP template file. $ Tpl-> write_cache ($ tpl_index ); } } // Contains the PHP template file. Include ($ tpl-> parse_tpl ($ tpl_index )); ?>
|
Corresponding HTML template file (index.html ):
Code: |
{Block_cat}
{= $ Cat ['forum _ name']} |
{Block_forum}
{= $ Forum ['forum _ name']} |
{/Block_forum}
{/Block_cat}
|
After processing, the {block_forum} {block_cat} tag is replaced with a PHP loop statement to display all elements of the array.
Generated PHP template file (default_index.php ):
Code: |
|
If ($ forum ['forum _ cat_id '] = $ cat ['forum _ id']) {?>
|
}?>
|
Default_index.php is included in index. php so that it can be displayed normally.
In this way, the HTML template file can be modified and beautified using dw, which should be easier for the artist.
Template. php
Code: |
/*************************************** **************************************** ** * Template) * Last modification time: 2004.4.07 used in this forum * * * **************************************** **************************************** **/ Class Template {
// $ This-> $ template: Save template data. Var $ template = '';
// Template path. Var $ tpl_path = '';
// Template prefix (style name ). Var $ tpl_prefix = '';
// Cache path (compiled path ). Var $ cache_path = '';
// Css file path. Var $ css_path = '';
// Header file path. Var $ header_path = '';
// Footer file path Var $ footer_path = '';
/** * Initialize the template path. */ Function Template ($ root = 'default ') { // Template prefix (style name ). $ This-> tpl_prefix = $ root; // Template file path. $ This-> tpl_path = './templates/'. $ root .'/'; // Generated PHP file storage path. $ This-> cache_path = './template_data/'. $ this-> tpl_prefix .'_'; Return true; }
/** * Chk_cache: Check whether the template after "compilation" needs to be updated. determine whether the "compilation" file exists at the last modification time. */ Function chk_cache ($ tpl_index) { $ Tpl_file = $ this-> tpl_path. $ tpl_index. '.html '; $ Cache_file = $ this-> cache_path. $ tpl_index. '. php '; // Determine whether an update is required. If (! File_exists ($ cache_file )) { Return true; } Elseif (filemtime ($ tpl_file)> filemtime ($ cache_file )) { Return true; } }
/** * Output template file. */ Function parse_tpl ($ tpl_index, $ message = '') { Return $ this-> cache_path. $ tpl_index. '. php '; }
/** * Load the template file. */ Function load_tpl ($ tpl_index) { $ Tpl_file = $ this-> tpl_path. $ tpl_index. '.html '; $ Fp = fopen ($ tpl_file, 'r '); $ This-> template = fread ($ fp, filesize ($ tpl_file )); Fclose ($ fp ); }
/** * Replace the variable and compile the template. */ Function write_cache ($ tpl_index) {
$ Cache_file = $ this-> cache_path. $ tpl_index. '. php ';
// Variable display. $ This-> template = preg_replace ("/(\ {=) (. + ?) (\})/Is "," ", $ This-> template );
// UI language replacement. $ This-> template = preg_replace ("/\ {lang + (. + ?) \}/Ies "," \ $ lang ['main'] ['\ 1'] ", $ this-> template );
$ Fp = fopen ($ cache_file, 'w '); Flock ($ fp, 3 ); Fwrite ($ fp, $ this-> template ); Fclose ($ fp ); }
/** * Replace block. */ Function assign_block ($ search, $ replace) { $ This-> template = str_replace ($ search, $ replace, $ this-> template ); } } ?> |
NLP wants to strike a balance between speed and ease of use (mainly referring to the convenience of the art design). so it adopts the method of generating php files from html files (Compilation ?) Also want to display the separator...