A PHP template mainly wants to reflect the ideas

Source: Internet
Author: User
Tags php template

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:
<? Php
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}", "<? Foreach (\ $ cats as \ $ cat) {?> ");
$ Tpl-> assign_block ("{/block_cat}", "<?}?> ");
$ Tpl-> assign_block ("{block_forum}", "<? Foreach (\ $ forums as \ $ forum ){

\ Nif (\ $ forum ['Forum _ cat_id '] =\$ cat ['Forum _ id']) {?> ");
$ Tpl-> assign_block ("{/block_forum}", "<?} \ N}?> ");
// 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}
<Table width = "100%" border = "0" cellspacing = "1" cellpadding = "1" bgcolor = "#000000" align = "center">
<Tr align = "{= TR_ALING}" bgcolor = "# FFFFFF">
<Td colspan = "6"> <span class = "title"> <B >{= $ cat ['Forum _ name']} </B> </span> </td>
</Tr>
{Block_forum}
<Tr bgcolor = "# FFFFFF">
<Td valign = "top" >{=$ forum ['Forum _ name']} </td>
</Tr>
{/Block_forum}
</Table>
<Br>
{/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:
<? Foreach ($ cats as $ cat) {?>
<Table width = "100%" border = "0" cellspacing = "1" cellpadding = "1" bgcolor = "#000000" align = "center">
<Tr align = "<? = TR_ALING?> "Bgcolor =" # FFFFFF ">
<Td colspan = "6"> <span class = "title"> <B> <? = $ Cat ['Forum _ name']?> </B> </span> </td>
</Tr>
<? Foreach ($ forums as $ forum ){
If ($ forum ['Forum _ cat_id '] = $ cat ['Forum _ id']) {?>
<Tr bgcolor = "# FFFFFF">
<Td valign = "top"> <? = $ Forum ['Forum _ name']?> </Td>
</Tr>
<?}
}?>
</Table>
<Br>
<?}?>


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:
<? Php
/*************************************** **************************************** **
* 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 "," <? =\\ 2?> ", $ 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 );
}
}
?>

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.