2012-04-12 12 views
1

php/mysqlを使って動的なウェブサイトを作成します。純粋なPHPコード+ HTMLを使用して印刷データ用の私のテンプレート。今、より良い、最適化された、より速い方法のためのの構造の混合PHP + HTML?私が持っている各ページため PHPテンプレートを組み合わせたhtmlテンプレート:php html template

(テンプレートエンジンなし):私が持っているex.1(ラッパーの前にPHPコード)ため

<?PHP include (DEFINE_SITE . '/templates/header.php'); 

// isset : post : get : SELECT : INSERT ... Or ANY CODE OF PHP 
?> 

<div id="wrapper"> 
<div id="leftsidebar"><?PHP // Left DATA ?></div> 
<div id="centercontent"> 

<?PHP // while {} Print result of php ANY LOOPS ..... ?> 

</div> 
<div id="rightsidebar"><?PHP // Right DATA ?></div> 
</div> 
<?PHP include (DEFINE_SITE . '/templates/footer.php'); ?> 

ページ:ex.2(後サイドバーとセンターコンテンツの前)

<?PHP include (DEFINE_SITE . '/templates/header.php'); 

<div id="wrapper"> 
<div id="leftsidebar"><?PHP // Left DATA ?></div> 
<?PHP // isset : post : get : SELECT : INSERT ... Or ANY CODE OF PHP ?> 
<div id="centercontent"> 

<?PHP // while {} Print result of php ANY LOOPS ..... ?> 

</div> 
<div id="rightsidebar"><?PHP // Right DATA ?></div> 
</div> 

<?PHP include (DEFINE_SITE . '/templates/footer.php');?> 

どちらが良いですか? e.x 1またはe.x 2?あなたの意見 ?

+0

**コンテキスト**の方がよかったですか? –

+0

構造が最適化され、読み込みが高速になりました。 – BBKing

+2

最適化された構造を定義します。また、より速いローディングですか?基本的に同じです。もしあれば、2つの間のミリ秒を削ってはいけません。パフォーマンスを望むなら、 'include'を使わないでください。そうすれば、OSは強制的にファイルの' stat'を実行します。ただし、コードの明快さと保守性のためにパフォーマンスを最小限に抑えてはいけません。 –

答えて

0

なぜテンプレートエンジンへの嫌悪?実際には、PHPはテンプレートエンジンですが、おそらくあなたが(Twigのような)いくつかの項目をチェックした場合、テンプレートエンジンなしで、より柔軟で迅速かつ反応性の高いものを設計するのに役立つかもしれません。私のように;-)

+1

yeap! TwigはRainTPL後のテンプレートエンジンに最適ですが、Smartyはキャッシュをサポートしていますが、現在私はそれを使用しています。 – BBKing

0

あなたのHTMLとPHPを分けておくことをお勧めします。テンプレートシステムを使用したくない場合は、なぜこのようにしないのですか?別のPHPファイルで

<div id="wrapper"> 
    <div id="leftsidebar">{LEFT}</div> 
    <div id="centercontent">{CONTENT}</div> 
    <div id="rightsidebar">{RIGHT}</div> 
</div> 

は、HTMLテンプレートを作成し

$content = file_get_contents('template.html'); 
$templateVars = array(); 

startVar('CONTENT'); 
echo '<p>This is the content.</p>'; // New template vars are also allowed 
endVar('CONTENT'); 

echo replaceTemplateVars($content); 

/** 
* Replace template variables recursively with the key-value pairs that are in the $templateVars queue 
* @param string $content (default NULL) is code to convert. 
* @return the string that is replaced 
*/ 
function replaceTemplateVars($content = NULL) 
{ 
    global $templateVars; 
    $matches = array(); 
    preg_match_all("/{[A-Z0-9_:]*}/", $content, $matches); // $matches = All vars found in your template 
    foreach($matches[0] as $key) 
    { 
     if(isset($templateVars[substr($key, 1, -1)])) 
     { 
      $content = str_replace($key, $templateVars[substr($key, 1, -1)], $content); // Substitute template var with contents 
     } 
     else 
     { 
      $content = str_replace($key, "", $content); // Remove empty template var from template 
     } 
    } 

    // Check if the replacement has entered any new template variables, if so go recursive and replace these too 
    if(preg_match_all("/{[A-Z0-9_:]*}/", $content, $matches)) 
    { 
     $content = replaceTemplateVars($content); 
    } 

    return $content; 
} 

/** 
* Start output buffer for a template key 
* @param string $key is not used. Only for overview purposes 
*/ 
function startVar($key) 
{ 
    ob_start(); 
} 

/** 
* End output buffer for a template key and store the contents of the output buffer in the template key 
* @param string $key 
*/ 
function endVar($key) 
{ 
    setVar($key, ob_get_contents()); 
    ob_end_clean(); 
} 

/** 
* @param string $key to store value in 
* @param mixed $value to be stored 
*/ 
function setVar($key, $value) 
{ 
    global $templateVars; 
    $templateVars[$key] = $value; 
} 

をそして、これはあなたの画面上で得るものです:

<div id="wrapper"> 
    <div id="leftsidebar"></div> 
    <div id="centercontent"><p>This is the content.</p></div> 
    <div id="rightsidebar"></div> 
</div> 

だから前処理が行われますまずすべてのhtmlコードが出力されます。

もちろん、startPrependVar()startAppendVar()のような関数を追加し、すべてをTemplateクラスにラップしてグローバルスコープを取り除くことができます。

関連する問題