實例學習PHP之FastTemplate模板篇
發表于:2007-07-14來源:作者:點擊數:
標簽:
如果你從來沒有接觸過 PHP ,那么還是先看看這個吧,當然即使是你已經對PHP有所了解,但一本PHP4的的使用手冊也還是需要的,:)。此外一本HTML語法手冊當然也是不可缺少的啦。。。。。。。。。。 在網站 開發 過程中你是不是經常面對改版的苦惱?幾百幾千個
如果你從來沒有接觸過
PHP,那么還是先看看這個吧,當然即使是你已經對PHP有所了解,但一本PHP4的的使用手冊也還是需要的,:)。此外一本HTML語法手冊當然也是不可缺少的啦。。。。。。。。。。
在網站
開發過程中你是不是經常面對改版的苦惱?幾百幾千個文件因為版式上的一點小變化就需要全部重新處理,是不是讓你頭痛無比?唉,如果能夠把內容和表現形式分開就好了,這可是我們一直翹首等待的。但可惜用于處理這個問題的XML還未完全的成熟。難道除此之外就沒有辦法了嗎?正所謂東西是死的,人卻是活的,我們今天要學習的這個php庫,就可以幫助我們在一定程度上處理這個問題。:))
FastTemplate是什么?從PHP語言上來講它是一個PHP庫;從它的起源來說它源于一個同樣名稱的Perl軟件包;從用途上來講是一個可以讓你在幾秒內改變整個站點外觀的實用工具。用最通俗的語言來說,它就是一個模板,一個類似DreamWaver中的模板?,F在FastTemplate在你心里是一個問號?還是一個感嘆號?又或是一個句號?(編輯:靠,在這騙稿費呀,我扁!)算了,不管那么多,你只要知道他是好東西就成,:)
首先在我們使用這個庫以前當然要先下載它,大家可以在下面這個網址http://www.thewebmasters.net/php/下載它(本站下載地址為: http://www.phpe.net/downloads/1.shtml)。下載之后呢把它解壓縮到你的web
服務器的一個目錄上,下面是解壓縮后的目錄結構
FastTemplate-1.1.0/
FastTemplate-1.1.0/README < - 這個文件就不用說了吧?
FastTemplate-1.1.0/class.FastTemplate.php3 < - 這個文件最重要,它就庫文件,核心耶!
FastTemplate-1.1.0/example_1.phtml < - 一個例子
FastTemplate-1.1.0/example_2.phtml < - 第二個例子
FastTemplate-1.1.0/example_3.phtml < - 第三個例子
FastTemplate-1.1.0/dynamic_example.phtml < - 第四個例子
FastTemplate-1.1.0/docs/ < - 文檔目錄
FastTemplate-1.1.0/docs/FastTemplate.3 < - Unix man page
FastTemplate-1.1.0/docs/FastTemplate.html < - HTML documentation
FastTemplate-1.1.0/templates/ < - 模板例子的目錄
FastTemplate-1.1.0/templates/begin.tpl
FastTemplate-1.1.0/templates/header.tpl
FastTemplate-1.1.0/templates/main.tpl
FastTemplate-1.1.0/templates/row.tpl
FastTemplate-1.1.0/templates/test.tpl
FastTemplate-1.1.0/templates/footer.tpl
FastTemplate-1.1.0/templates/hta
clearcase/" target="_blank" >ccess.tpl
FastTemplate-1.1.0/templates/middle.tpl
FastTemplate-1.1.0/templates/table.tpl
注意喲,這個目錄一定要是php程序可以訪問的目錄喲,換句話說就是php.ini里那個include目錄。然后呢,使用php4編程的朋友注意了,你現在還不能直接使用這個庫,還需要人工做些修改!等下注意看下面。php3的讀者就不用管那么多,現在就可以試一下它帶的那幾個例子了,(嘿嘿,不過呢,它那些例子的后綴名全是phtml,如果你沒辦法設置web的話。你可以嘗試把后綴名改改看,應該也可以用,地藏沒有試過不敢打包票。)那!接著呢,php4的兄弟可千萬要注意喲,如果不你不按下面的修改的話,這個庫可是沒有辦法用的喲??!
--- class.FastTemplate.php3 Sun Jun 27 13:44:47 1999
+++ php4.FastTemplate.php3 Tue Jul 20 10:49:25 1999
@@ -196,8 +196,10 @@
settype($val,"string");
}
- $template = ereg_replace("{$key}","$val","$template");
- //$template = str_replace("{$key}","$val","$template");
+ // php4 doesn't like '{$' combinations.
+ $key = '{'."$key".'}';
+ $template = ereg_replace("$key","$val","$template");
+ //$template = str_replace("$key","$val","$template");
}
}
@@ -410,7 +412,7 @@
}
if($end)
{
- $newParent .= "{$MacroName}
";
+ $newParent .= '{'."$MacroName}
";
}
// Next line please
if($end) { $end = false; }
大家用文本編輯器打開class.FastTemplate.php3文件,找到上面的部分。'-'減號代表消除這行,'+'加號代表加入這行。另外
windows系統下的朋友注意喲!你們還需要改點小東西,把下面的那個$win32變量的值改成true,不改的話,用不了可別罵我,;)。
var $WIN32 = true; // Set to true if this is a WIN32 server
改好后,大家就可以試試他的那幾個例子了。 如何?成功了吧。嘿嘿嘿嘿~~~,地藏不會騙大家的啦。
OK,現在這個庫已經可以用啦,我們準備進行下面的學習吧,:)
如何使用這個庫?簡單喲,首先要把這個庫包含進來。也就是說呢,要 include "class.FastTemplate.php3"; 然后?呵呵…… 然后我也不知道啦?。ň庉嫞?quot;嘿!找扁啊,你!")啊,啊…!Sorry,我想起來了(在地藏被眾編輯海扁一頓之后)。然后是學習這個庫的使用原理。在這里呢我們先做個假設。
我們假設一個頁面是由很多的小的部分組成(比如:每個網站都有分欄目導航等等),而且每個小的部分都有一個唯一的標識符(比如:我們把分欄每個小項定義為一個名字)
我們首先以這個庫自帶的example_1為例,讓大家可以盡快的學會這個庫的基本用法,;),不過大家要注意喲,這幾個文件在服務器上的位置要按解壓時的相對路徑來存,千萬不要忘記,就象不要忘記你女朋友的生日一樣。呵呵~~~~
< ?
// Example FastTemplate Demo #1 - The example from the man page
Header("Content-type: text/plain");
include("class.FastTemplate.php3");
$tpl = new FastTemplate("./templates");
$tpl- >define(
array(
main = > "main.tpl",
table = > "table.tpl",
row = > "row.tpl"
)
);
$tpl- >assign( array( TITLE = > "FastTemplate Test") );
for ($n=1; $n < = 3; $n++)
{
$Number = $n;
$BigNum = $n*10;
$tpl- >assign(
array(
NUMBER = > $Number,
BIG_NUMBER = > $BigNum
)
);
$tpl- >parse(ROWS,".row");
}
$tpl- >parse(MAIN, array("table","main"));
$tpl- >FastPrint();
exit;
? >
< !-- NAME: main.tpl -- >
< html >
< head >< title > {TITLE} < /title >
< /head >
< body >
{MAIN}
< /body >
< /html >
< !-- END: main.tpl -- >
< !-- NAME: table.tpl -- >
< table border='1' >
{ROWS}
< /table >
< !-- END: table.tpl -- >
< !-- NAME: row.tpl -- >
< tr >
< td >{NUMBER}< /td >
< td >{BIG_NUMBER}< /td >
< /tr >
< !-- END: row.tpl -- >
使用這個庫首先要如前面所說的 include "class.FastTemplate.php3"; 然后是定義模板所在目錄$tpl = new FastTemplate("./templates");注意喲!這里因為我是在windows系統運行,所以用的是"./templates",在
Linux或
UNIX中可能不一樣,大家根據具體情況來設定。然后呢,我們對應不同的模板,定義下面這個矩陣數組。$tpl- >define( array( main = > "main.tpl", table = > "table.tpl", row = > "row.tpl" ) ); define是這個類中的一個函數。下面是它的語法:define( array( key,value pairs) ) ,define()函數映射一個名字到模板文件上,這個新的名字將是你用來代表模板的唯一名字,因為除此之外再不會有模板文件名出現。而且大家注意,這是使用類時決不能缺少的步驟,不能少的喲,如果少了那就和你不帶生日禮物去你女朋友的生日Party有異曲同工之妙,嘿嘿嘿~~~~
現在!關鍵的,最有個性的東西來了!assign( (key,value pair) 或 assign ( array(key value pairs) ) 這個函數將把你在模板中定義的標識符定義為你網頁上真正想要的東西。比如就象是上面$tpl- >assign( array( TITLE = > "FastTemplate Test") ); 這句,將模板main.tpl里的{TITLE}替換為 FastTemplate Test 看不懂想不明的朋友多看看上面的那些代碼。接著呢!接著是另外一個很有特色的東東。parse(RETURN, FileHandle(s) ) 將一個已定義模板插入的定義到另外一個模板文件中。大家仔細研究下面的代碼,相信會比較容易了解。
for ($n=1; $n < = 3; $n++)
{
$Number = $n;
$BigNum = $n*10;
$tpl- >assign(
array(
NUMBER = > $Number,
BIG_NUMBER = > $BigNum
)
);
$tpl- >parse(ROWS,".row");
}
parse這個函數有三種用法
$tpl- >parse(MAIN, "main"); // 標準
$tpl- >parse(MAIN, array ( "table", "main") ); // 簡潔
$tpl- >parse(MAIN, ".row"); // 增加
其中以第三種最有意思,它表示的是在原來已經有的基礎上再加上一個新數據。呵呵,這么說大家可能無法明白,我們還是看看再多研究一下上面的源代碼吧,畢竟編程這東西有很多是只可意會不可言傳的?。ㄏ旅娓缴嫌⑽牡恼f明,不是地藏想偷懶不翻譯,實在是有些東西看原味的比翻譯的更加容易理解呀,
In the regular version, the template named ``main'' is loaded if it hasn't been a
lready, all the variables are interpolated, and the result is then stored in FastTemplate as the value MAIN. If the variable '{MAIN}' shows up in a later template, it will be interpolated to be the value of the parsed ``main'' template. This allows you to easily nest templates, which brings us to the compound style.
The compound style is designed to make it easier to nest templates.
The following are equivalent:
$tpl- >parse(MAIN, "table");
$tpl- >parse(MAIN, ".main");
// is the same as:
$tpl- >parse(MAIN, array("table", "main"));
// this form saves function calls and makes your code cleaner
It is import
ant to note that when you are using the compound form, each template after the first, must contain the variable that you are parsing the results into. In the above example, 'main' must contain the variable '{MAIN}', as that is where the parsed results of 'table' is stored. If 'main' does not contain the variable '{MAIN}' then the parsed results of 'table' will be lost. The append style allows you to append the parsed results to the target variable. Placing a leading dot . before a defined file handle tells FastTemplate to append the parsed results of this template to the returned results. This is most useful when building tables that have an dynamic number of rows - such as data from a database query. )
最后在完成上面的過程后,就可以進行輸出了,FastTemplate庫使用使用FastPrint(HANDLE) 來進行輸出。
$tpl- >FastPrint();
在經過上面的這些步驟后,大家對使用FastTemplate庫的過程是否清楚明白呢?地藏試試把上面的過程總結一下,看看對各位朋友是否有幫助:
首先:將庫用include加進來
其次:定義一個類變量,在上例中是$tpl
再次:定義各種最小元素,比如上面的$tpl = new FastTemplate("./templates");
然后:利用parse函數將各種最小元素進行組合起來
最后:用FastPrint()進行輸出
原文轉自:http://www.anti-gravitydesign.com