2011-05-24 44 views
0

您好我想弄清楚如何做一個PHP模板系統與類,我不喜歡header.php footer.php或聰明或任何blabla,只是想template.html,class.php和page1.php,page2.php等......用我自己的php代碼,我發現很多網站的人都在教這個如何做,但我仍然有很多問題。用於不同的頁面與不同的CSS和MySQL的PHP​​模板

1)我想EXTRA CSS添加到

2部分頁面)有些頁面有PHP代碼,如MySQL查詢和類似的東西這將是一個變量,其中曾經我想,

3)內容在模板中不僅僅是單詞,而是大量的div和東西,CONTENT變量在一些頁面中也有查詢,比如填充(例如)下拉菜單。

希望有人可以指導我在這,我居然有我的explame tempalte,(標籤都只是隨機的)

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 

<head> 
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" /> 
<meta http-equiv="X-UA-Compatible" content="IE=8" /> 
<title>CompanyName | ##TITLE##</title> 

<link href="layout.css" rel="stylesheet" type="text/css" /> 
<link href="styles.css" rel="stylesheet" type="text/css" /> 
##EXTRA_CSS## 
<link rel="shortcut icon" href="/favicon.ico" /> 

<!--[if IE 6]> 
    <script src="DD_belatedPNG_0.0.8a-min.js"></script> 
    <script> 
      DD_belatedPNG.fix('img, div'); 
    </script> 
<![endif]--> 

##EXTA_JS## 

</head> 

<body> 

    <div id="main"> 
     <div id="container_black"> 
      <div id="container_white"> 

       <div id="container_header"> 

        <div id="logo_top"></div>   
        <div id="lineas_verticales_top"> 
         <div class="volver_portada"><a href="index.shtml">Volver a portada</a></div> 
         <div class="english_spanish"><u>Español</u> | <a href="../en/index.shtml" class="english_spanish">English</a></div> 
        </div> 
        <div id="nav_bar_black"><div id="nav_bar_red"><div id="nav_bar_yel"> 

         <ul class="menuholder"> 
          <li class="menu_principal"><a href="#" title="Principal">Principal</a></li> 
          <li class="menu_empresa"><a href="#" title="Empresa">Empresa</a></li> 
          <li class="menu_productos"><a href="#" title="Productos">Productos</a></li> 
          <li class="menu_clientes"><a href="#" title="Clientes">Clientes</a></li> 
          <li class="menu_recetas"><a href="#" title="Recetas">Recetas</a></li> 
          <li class="menu_contacto"><a href="#" title="Contacto">Contacto</a></li> 
         </ul> 

        </div></div></div> 
        <div id="topbg_degr"></div> 

       </div> 

       <div id="container_left"> 
        <div id="conmargen_left_top"></div> 
        <div id="container_conmargen_left_middle"> 

         ##CONTENT## 

        </div> 
        <div id="conmargen_left_bottom"></div> 

        <!--[IF INDEX] 
        <div id="fono"></div> 
        <div id="dir"></div> 
        --> 

       </div> 

       <!--[IF INDEX] 
       <div id="nav"></div> 
       --> 

       <div id="container_right"> 
        <div id="conmargen_right_top"></div> 
        <div id="container_conmargen_right_middle"> 

         <!--[IF PAGE OR PAGE OR PAGE] 
         <a href="#" id="recetas_poster" title="recetas"></a> 
         [ELSE IF] 
         <a href="#" id="clientes_poster" title="recetas"></a> 
         [ELSE IF] 
         <a href="#" id="productos_poster" title="recetas"></a> 

         --> 

        </div> 
        <div id="conmargen_right_bottom"></div> 
       </div> 

       <!--[IF INDEX] 
       <div id="frame_facebook"> 
       <a href="#" class="facebook" title="CompanyName"><span>CompanyName</span> en Facebook</a> 
       <div class="breakL"></div> 
       <fb:like href="#" layout="button_count" show_faces="true" width="100" font="tahoma"></fb:like> 
       </div> 
       --> 

       <br/> 

      </div> <!-- cierre del container white --> 
     </div> <!-- cierre del container black --> 

     <div id="footer"> 
       <div class="footer_comment"> 
        CompanyName Todos los derechos reservados 2011 
       </div> 
     </div> 

    </div> <!-- cierre del main --> 
<br/> 

</body> 

</html> 

例如contact.php,內容將是一個形式,並在頁面頂部,我有這個巨大的PHP代碼,我驗證和所有。

我真的很感激,如果有人會讓我在正確的道路上做到這一點。先謝謝你。

+1

'有些頁面有像mysql查詢和類似東西的php代碼 - 它通常被稱爲'代碼異味'。閱讀關於MVC和框架。或兩者兼得:http://www.yiiframework.com/doc/guide/1.1/en/basics.mvc – 2011-05-24 09:53:49

回答

0

可能有一百種不同的方法可以解決這個問題,每種方法都有各自的優點和缺點 - 就像您已經說過的那樣使用數據庫和/或ajax調用和/或頁眉和頁腳。你必須弄清楚哪種方式最適合你的特定項目或編碼風格,或者兩者兼而有之。然而,如果你真的只想'template.html',但有一些頁面與PHP,JavaScript,MySQL或其他任何內容 - 然後我會建議。

使用佔位符(例如{PAGE_TITLE},{MAIN_CONTENT}或任何您需要的)創建template.html。

創建page1.php/page2.php等並執行任何服務器端工作,生成變量以匹配佔位符。這可能是很方便的將它們存儲爲一個數組,即:

PAGE_VARS array( 
       ['TITLE'] => My Page 
       ['CONTENT'] => This is my page content 
       ) 

在您的「頁面」腳本的末尾,template.html的整個內容加載到一個字符串

$template = file_get_contents('template.html') 

然後,建立你的模板,使用替代變量或是一個基本的循環:

foreach ($PAGE_VARS as $KEY=>$VALUE) {  
    $template = str_replace("{".$KEY."}",$VALUE,$template) 
} 

(隨意弄巧,可能與上述一些正則表達式更高效,這只是一個簡單的例子。)

輸出您的模板。

class Template { 
    var $contents; 

    function load($file) { 
     if ($fp = fopen($file, "r")) { 
      $this->contents = fread($fp, filesize($file)); 
      fclose($fp); 
     } 
    } 

    function replace($str,$var) { 
     $this->contents = str_replace("<".$str.">",$var,$this->contents); 
    } 

    function show() { 
     $search = array(
       '/\t/', //Remove Tabs 
       '/<!--[^\[-]+?-->/', //Remove Comments 
       '/\n\n/' //Remove empty lines 
       ); 
      $replace = array(
       '', 
       '', 
       '' 
       ); 
     $this->contents = preg_replace($search, $replace, $this->contents); 
     echo $this->contents; 
    } 
} 

在模板:

echo $template; 
+0

感謝您的幫助,我仍然有一個疑問,你說「這是我的網頁內容」,是它可以把一個巨大的內容?與div和額外的PHP? – Gmo 2011-05-24 10:06:10

+1

傳遞給$ PAGE_VARS的數據應該是最終的內容。例如,如果要顯示MySQL查詢的結果,請首先生成查詢,然後將結果設置爲$ PAGE_VARS ['CONTENT']。 – moteutsch 2011-05-24 10:14:56

+1

而且,是的,您可以在佔位符中使用HTML,但不能使用PHP。你不應該需要任何東西,就像我上面解釋的那樣。 – moteutsch 2011-05-24 10:16:05

0

可以這樣,這將是你的 '模板引擎' 創建快速類(class.php)。HTML文件,添加你想要把你的內容的特殊標籤,就像這樣:

<html><head></head> 
<body> 
<div id="id1"><page_title></div> 
</body> 
</html> 

...

然後創建一個功能到標籤內寫入(每區一個):

function writetitle($s) { 
    $GLOBALS['writes']++; 
    $GLOBALS['page_title'] .= $s; 
    return; 
} 

最後,在page.php文件,調用類的,寫你的內容,並生成頁面:

require_once('class.php'); 
//Load Class 
$template = new Template; 
$template->load("template.html"); 

//Some query : 
$query = mysql_query('SELECT...'); 
$res = mysql_num_rows($query); 

//write your content : 
writetitle('my title, '.$res.''); 

//Generate the page : 
$template->show(); 

土井只要你願意,你可以創建儘可能多的區域。 writetitle就像echo,所以你可以做出查詢和你想要的一切。

我希望它有幫助。

+0

即時通訊在php中真的很新,但是當你說每個區域的意思是一個函數時,函數的功能如下: 函數writetitle($ s){GLOBALS ['writes'] ++; $ GLOBALS ['page_title']。= $ s; $ GLOBALS ['extra_CSS']。= $ s; ... return; } ??那樣對不對? – Gmo 2011-05-24 22:42:39

0

讓PHP解析你的模板文件的Insetad我建議使用PHP編寫模板而不是你自己的僞代碼。

這是完全有效的寫

... 
<body> 
<?php if($fooBar == "hahahaha"):?> 
    <a href="www.foobar.com">The cool foobar link</a> 
<?php endif;?> 
</body> 
... 

我會再創建一個layout文件,所有常用的html等,然後用小模板片段,其中的主要內容去插入佈局。 通過在你的模板中有實際的PHP代碼,PHP機器人可以處理它們,並且你不必編寫自己的邏輯來處理循環,如果語句等。

看看Zend Framework,你會得到如何編寫模板引擎的好主意。