A php template, mainly want to reflect the idea _php tutorial

Source: Internet
Author: User
Tags php template
Ideas:
To achieve a balance between speed and ease of use (mainly refers to the convenience of the art design). The method of generating PHP files from HTML files (compile?) is used.
Also want to balance between separating the display logic and separating the HTML code

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

Require ('./template.php ');
PHP files generated by HTML are prefixed with different styles.
$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 Tutorial '),
Array (' forum_id ' = ' 4 ', ' forum_cat_id ' = ' 1 ', ' forum_name ' = ' php beginner tutorial '),
Array (' forum_id ' = ' 5 ', ' forum_cat_id ' = ' 2 ', ' forum_name ' = ' MySQL related data ')
);

if ($cats)
{
if ($tpl->chk_cache ($TPL _index))//check to see if you need to re-produce PHP template files.
{
$tpl->load_tpl ($tpl _index);//loading HTML template files.
Replace 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}", " ");
Production of PHP template files.
$tpl->write_cache ($tpl _index);
}
}
Contains php template files.
Include ($tpl->parse_tpl ($tpl _index));
?>

corresponding HTML template file (index.html):
Code:

{Block_cat}









{Block_forum} {/block_forum}
{= $cat [' Forum_name ']}
{= $forum [' Forum_name ']}



{/block_cat}

After processing, the {BLOCK_FORUM}{BLOCK_CAT} tag inside is replaced with the PHP loop statement, which displays all elements of the array.

Generated php template file (default_index.php):
Code:

!--? foreach ($cats as $cat) {?-->









if ($forum [' forum_cat_id '] = = $cat [' forum_id ']) {?> }?> !--? foreach ($forums as $forum) { !--?}
!--? = $cat [' forum_name ']?-->
!--? = $forum [ ' Forum_name ']?-->



!--?}?-- 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:

/*********************************************************************************
* Template class
* Last modified: 2004.4.07 this forum uses
*
*
*
**********************************************************************************/
Class Template {

$template $this, save 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 path to the CSS file.
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;
Template file path.
$this->tpl_path = './templates/'. $root. '/';
The generated PHP file storage path.
$this->cache_path = './template_data/'. $this->tpl_prefix. '_';
return true;
}

/**
* Chk_cache, check whether the template after "compile" needs to be updated, judging by: The last modification time, "compile" the 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 updates are 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 "," ", $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);
}
}
?>

http://www.bkjia.com/PHPjc/317542.html www.bkjia.com true http://www.bkjia.com/PHPjc/317542.html techarticle idea: To achieve a balance between speed and ease of use (mainly refers to the convenience of the art design). So the use of HTML files generated by the Php file method (compile?) also want to be in the separation of the display logos ...

  • 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.