ホームページ  >  記事  >  バックエンド開発  >  主に idea_PHP チュートリアルを反映する PHP テンプレート

主に idea_PHP チュートリアルを反映する PHP テンプレート

WBOY
WBOYオリジナル
2016-07-21 15:58:42837ブラウズ

思路:
欲在速度和易用(主要指的是美工设计的方便性)之间取得一个平衡点.于是采用了由html文件生成php文件的办法(编译?)
也想在分离显示逻辑和分离html代码之间平衡一下

例如一个论坛首页(index.php):

代码:

require('./template.php');
//由html生成的php文件的前缀,区别使用多种风格.
$tpl_prefix = 'default';
//模板文件名
$tpl_index = 'index';

$tpl = new Template($tpl_prefix);

$cats = array(
array('forum_id'=>'1','forum_cat_id'=>'0','forum_name'=>'PHP学习'),
   array('forum_id'=>'2','forum_cat_id'=>'0','forum_name'=>'MYSQL学习')
);
$forums = array(
   array('forum_id'=>'3','forum_cat_id'=>'1','forum_name'=>'PHP高级教程'),
   array('forum_id'=>'4','forum_cat_id'=>'1','forum_name'=>'PHP初级教程'),
   array('forum_id'=>'5','forum_cat_id'=>'2','forum_name'=>'MYSQL相关资料')
);

if ($cats)
{
   if ($tpl->chk_cache($tpl_index))//检查判断是否需要重新生产PHP模板文件.
    {
       $tpl->load_tpl($tpl_index);//加载html模板文件.
      //替换PHP语句
      $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}","");
      //生产PHP模板文件.
      $tpl->write_cache($tpl_index);
   }
}
//包含PHP模板文件.
include($tpl->parse_tpl($tpl_index));
?>

对应的html模板文件(index.html):
代码:

{block_cat}

 
   
 
{block_forum}
 
   
 
{/block_forum}
{=$cat['forum_name']}
{=$forum['forum_name']}



{/block_cat}

经过处理,里面的{block_forum}{block_cat}标签被替换成PHP循环语句,用于显示数组种所有元素.

生成的PHP模板文件(default_index.php):
代码:



 
   
 
if($forum['forum_cat_id'] == $cat['forum_id']) {?>
 
   
 
}?>





default_index.php は、index.php に含まれているので、普通に表示できます

このように、HTML テンプレートファイルを dw で修正して美化することができ、アーティストにとってはより便利になるはずです

template。 .php
コード:

/*************************************************** * ******************************
*
* の ‐ ‐
*
*
***** ******************************************* ******* ******************************/
class Template {

//$this->$template、テンプレート データを格納します。
var $template = '';
var $tpl_path = '';

// テンプレートのプレフィックス (スタイル名)
var $tpl_prefix = '';
var $cache_path = ''; css ファイルのパス。
var $css_path = '';

// ヘッダー ファイルのパス
var $footer_path = ''; &*/
function Template($root = 'default')
{
// テンプレートのプレフィックス (スタイル名)
$this->tpl_prefix = $root;
$this- & gt; TPL_PATH/'' // .php '; fopen($tpl_file, 'r');
$this->template = fread($fp, filesize($tpl_file));
を通じてアウト スルー オフ オフ スルー '' スルー スルー スルー スルー スルースルー ‐‐ ‐‐‐‐‐ . #&*/
function write_cache($tpl_index)
{

$cache_file = $this->cache_path . '.php' // 変数の表示。
$this->template = preg_replace( "/({=)(.+?)(})/is", "", $this->template

$this->template = preg_replace("/{lang +(.+?)}/ies", "$lang['main']['\1']", $this->template);
$fp = fopen($cache_file, 'w');
flock($fp, 3);
fclose($fp);

/**
* ブロックを交換します。*/
function assign_block($search,$replace)
{
$this->template = str_replace($search,$replace,$this->template)
}
}
?>

http://www.bkjia.com/PHPjc/317542.htmlwww.bkjia.comtru​​ehttp://www.bkjia.com/PHPjc/317542.html技術記事アイデア:速度と使いやすさのバランスを取りたい(主にアートデザインの利便性を参照)。そこで、htmlファイルからphpファイルを生成(コンパイル?)する方法を採用し、表示ロジックも分離したいと考えました。 ..
声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。