主にアイデアを反映するための PHP テンプレート

黄舟
リリース: 2016-12-14 11:34:33
オリジナル
1098 人が閲覧しました

速度と使いやすさのバランスを取りたかったので(主にアートデザインの利便性を参考に)、htmlファイルからphpファイルを生成(コンパイル?)する方法を採用しました
表示ロジックと分離もしたかったのです。別の HTML コードのバランスをとる

たとえば、フォーラムのホームページ (index.php):

Code:


require('./template.php'); によって生成されたファイルhtml プレフィックス、複数のスタイルを使用します。
$tpl_prefix = 'default';
$tpl_index = 'index';

$cats = 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);
}
//
include($tpl->parse_tpl($tpl_index)); HTML テンプレート ファイル (index.html):


コード:


{block_cat}

< ;b> ;{=$cat['forum_name']}


{=$forum['forum_name']}

{ /block_cat } {block_forum}{block_cat} タグは、配列のすべての要素を表示するために使用される PHP ループ ステートメントに置き換えられます。生成された PHP テンプレート ファイル (default_index.php):



コード:




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

}?>
コード:


/*************************************************** * ******************************
* テンプレートクラス (Template)
* 最終更新日: 2004.4.07 このフォーラムでは
を使用しています*
*
*
******************************************* **** ************************************/
class Template {

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

// テンプレートのプレフィックス (スタイル名)

// キャッシュ パス (コンパイルされたパス)

// css ファイルのパス。

//ヘッダー ファイルのパス。

//フッター ファイルのパス。

/**
* 初期化テンプレートのパス。*/
function Template($root = 'default')
{
//テンプレートのプレフィックス (スタイル名)
$this->tpl_prefix = $root;
$this-> tpl_path = './templates/' . $root . '/';
$this->cache_path = './template_data/' .$this->tpl_prefix 。 ';
true を返す
}

/**
* chk_cache、「コンパイル済み」テンプレートを更新する必要があるかどうかを確認します。判断は、最終変更時刻と「コンパイル済み」ファイルが存在するかどうかに基づいて行われます。*/
関数 chk_cache($tpl_index)
$tpl_file = $this->tpl_index .html'; this->cache_path . '.php';
if(!file_exists($cache_file))
{
return true;
elseif($tpl_file) ) > filemtime($cache_file))
{
return true;
}
}

/**
* テンプレートファイルを出力します。*/
function parse_tpl($tpl_index,$message='')
{
return $this-> ;キャッシュ_パス . '.php';

/**
* テンプレートファイルをロードします。*/
関数load_tpl($tpl_index)
$tpl_file = $tpl_index . ;
$fp = fopen($tpl_file, 'r');
$this->template = fread($fp, filesize($fp));

/**
* 変数を置き換えてテンプレートを「コンパイル」します。*/
function write_cache($tpl_index)
{

$cache_file = $this->cache_path . '.php';
$this->template = preg_replace( "/({=)(.+?)(})/is", "", $this->template);

//インターフェース言語の置換。 >template = preg_replace("/{lang +(.+?)}/ies", "$lang['main']['\1']", $this->template); fopen($cache_file, 'w');
flock($fp, $this->template);

/**& */
function assign_block($search,$replace)
{
$this->template = str_replace($search,$replace,$this->template)
}
}
?>読んでいて、さらに関連コンテンツを入手したい場合は、php 中国語 Web サイト (www.php.cn) に注目してください。

関連ラベル:
ソース:php.cn
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
最新の問題
人気のおすすめ
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート
私たちについて 免責事項 Sitemap
PHP中国語ウェブサイト:福祉オンライン PHP トレーニング,PHP 学習者の迅速な成長を支援します!