A php template, mainly to reflect the idea of _php skills

Source: Internet
Author: User
Ideas:
To achieve a balance between speed and ease of use (mainly referring to the convenience of art design). So the way to generate PHP files from HTML files (compile?)
Also want to balance between separating the display logic and separating the HTML code

For example, a forum home page (index.php):
Code:

<?php
Require ('./template.php ');
The HTML-generated PHP file is prefixed with a different style.
$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 learning '),
Array (' forum_id ' => ' 2 ', ' forum_cat_id ' => ' 0 ', ' forum_name ' => ' MySQL learning ')
);
$forums = Array (
Array (' forum_id ' => ' 3 ', ' forum_cat_id ' => ' 1 ', ' forum_name ' => ' php Advanced Tutorials '),
Array (' forum_id ' => ' 4 ', ' forum_cat_id ' => ' 1 ', ' forum_name ' => ' PHP Beginner's course '),
Array (' forum_id ' => ' 5 ', ' forum_cat_id ' => ' 2 ', ' forum_name ' => ' MySQL related information ')
);

if ($cats)
{
if ($tpl->chk_cache ($TPL _index))//Check to determine if the PHP template file needs to be produced again.
{
$tpl->load_tpl ($tpl _index);//load 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}?> ");
Production of PHP template files.
$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 inside is replaced with the PHP loop statement, which is used 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>
?? >

The default_index.php is included in the index.php so that it can be displayed normally.

In this way, the HTML template file can be modified with DW to beautify, the artist should be more convenient.

template.php
Code:

<?php
/*********************************************************************************
* Template class (Template)
* Last modified: 2004.4.07 this forum uses
*
*
*
**********************************************************************************/
Class Template {

$this-> $template to store the template data.
var $template = ';

The template path.
var $tpl _path = ';

Template prefix (style name).
var $tpl _prefix = ';

The cache path (the compiled path).
var $cache _path = ';

The CSS file path.
var $css _path = ';

Header file path.
var $header _path = ';

Footer file path
var $footer _path = ';

/**
* Initialize template path.
*/
function Template ($root = ' default ')
{
Template prefix (style name).
$this->tpl_prefix = $root;
The template file path.
$this->tpl_path = './templates/'. $root. '/';
The generated PHP file store path.
$this->cache_path = './template_data/'. $this->tpl_prefix. '_';
return true;
}

/**
* Chk_cache, check the "compiled" template needs to be updated to determine the basis: the last modified time, "compile" file exists.
*/
function Chk_cache ($tpl _index)
{
$tpl _file = $this->tpl_path. $tpl _index. '. html ';
$cache _file = $this->cache_path. $tpl _index. '. php ';
Determine if 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 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);

Interface language substitution.
$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.