2012-04-12 139 views
1

我使用php/mysql創建一個動態網站。我使用純php代碼+ html打印數據的模板。現在,更好,優化,更快的方式結構混合php + html? (無模板引擎)將html模板與php代碼混合:php html模板

每個的網頁我有:實施例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'); ?> 

每個的網頁我有:例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

更好的**上下文**? – 2012-04-12 12:10:00

+0

優化結構,加載更快。 – BBKing 2012-04-12 12:12:25

+2

定義優化結構。另外,更快的加載?它們基本相同。如果有的話,你不會在兩者之間削減一毫秒。如果你想要性能,不要使用'include',這會強制操作系統在文件上執行'stat'。但是,爲了代碼清晰和可維護性,您決不應該交易(最小)性能。 – 2012-04-12 12:17:26

回答

0

爲什麼厭惡模板引擎?實際上,PHP是一個模板引擎,但是如果你檢查了幾個(比如Twig),它可能會幫助你設計更靈活,快速,響應快的模板引擎,或者你可能會開始喜歡它,像我一樣;-)

+1

yeap! Twig是最好的模板引擎RainTPL後,Smarty支持緩存。但我目前正在使用它。 – BBKing 2012-04-12 12:24:20

0

我建議你保持你的HTML和PHP分開。如果你不想使用模板系統,爲什麼你不這樣做?

做一個HTML模板:

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

在不同的php文件:

$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類中以擺脫全局範圍。