實(shí)例學(xué)習(xí)PHP之FastTemplate 模板篇
Jun 21, 2016 am 09:15 AM模板
如果你從來沒有接觸過PHP,那么還是先看看這個(gè)吧,當(dāng)然即使是你已經(jīng)對(duì)PHP有所了解,但一本PHP4的的使用手冊也還是需要的,:)。此外一本HTML語法手冊當(dāng)然也是不可缺少的啦。。。。。。。。。。
??
?? 在網(wǎng)站開發(fā)過程中你是不是經(jīng)常面對(duì)改版的苦惱?幾百幾千個(gè)文件因?yàn)榘媸缴系囊稽c(diǎn)小變化就需要全部重新處理,是不是讓你頭痛無比?唉,如果能夠把內(nèi)容和表現(xiàn)形式分開就好了,這可是我們一直翹首等待的。但可惜用于處理這個(gè)問題的XML還未完全的成熟。難道除此之外就沒有辦法了嗎?正所謂東西是死的,人卻是活的,我們今天要學(xué)習(xí)的這個(gè)php庫,就可以幫助我們在一定程度上處理這個(gè)問題。:))
??
?? FastTemplate是什么?從PHP語言上來講它是一個(gè)PHP庫;從它的起源來說它源于一個(gè)同樣名稱的Perl軟件包;從用途上來講是一個(gè)可以讓你在幾秒內(nèi)改變整個(gè)站點(diǎn)外觀的實(shí)用工具。用最通俗的語言來說,它就是一個(gè)模板,一個(gè)類似DreamWaver中的模板?,F(xiàn)在FastTemplate在你心里是一個(gè)問號(hào)?還是一個(gè)感嘆號(hào)?又或是一個(gè)句號(hào)?(編輯:靠,在這騙稿費(fèi)呀,我扁!)算了,不管那么多,你只要知道他是好東西就成,:)
??
?? 首先在我們使用這個(gè)庫以前當(dāng)然要先下載它(編輯:'廢話,又騙稿費(fèi)啊' 。地藏:'好吧,好吧,我不說了。:( '。),大家可以在下面這個(gè)網(wǎng)址http://www.thewebmasters.net/php/下載它。下載之后呢把它解壓縮到你的web服務(wù)器的一個(gè)目錄上,下面是解壓縮后的目錄結(jié)構(gòu)
??
??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 ??FastTemplate-1.1.0/docs/FastTemplate.html ??
??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/htaccess.tpl
??FastTemplate-1.1.0/templates/middle.tpl
??FastTemplate-1.1.0/templates/table.tpl
?? 注意喲,這個(gè)目錄一定要是php程序可以訪問的目錄喲,換句話說就是php.ini里那個(gè)include目錄。然后呢,使用php4編程的朋友注意了,你現(xiàn)在還不能直接使用這個(gè)庫,還需要人工做些修改!等下注意看下面。php3的讀者就不用管那么多,現(xiàn)在就可以試一下它帶的那幾個(gè)例子了,(嘿嘿,不過呢,它那些例子的后綴名全是phtml,如果你沒辦法設(shè)置web的話。你可以嘗試把后綴名改改看,應(yīng)該也可以用,地藏沒有試過不敢打包票。)那!接著呢,php4的兄弟可千萬要注意喲,如果不你不按下面的修改的話,這個(gè)庫可是沒有辦法用的喲??!
??
??--- 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文件,找到上面的部分。'-'減號(hào)代表消除這行,'+'加號(hào)代表加入這行。另外windows系統(tǒng)下的朋友注意喲!你們還需要改點(diǎn)小東西,把下面的那個(gè)$win32變量的值改成true,不改的話,用不了可別罵我,;)。
??
?? var $WIN32 = true; // Set to true if this is a WIN32 server
??
?? 改好后,大家就可以試試他的那幾個(gè)例子了。 如何?成功了吧。嘿嘿嘿嘿~~~,地藏不會(huì)騙大家的啦。
??
?? OK,現(xiàn)在這個(gè)庫已經(jīng)可以用啦,我們準(zhǔn)備進(jìn)行下面的學(xué)習(xí)吧,:)
??
?? 如何使用這個(gè)庫?簡單喲,首先要把這個(gè)庫包含進(jìn)來。也就是說呢,要 include "class.FastTemplate.php3"; 然后?呵呵…… 然后我也不知道啦?。ň庉嫞?嘿!找扁啊,你!")啊,啊…!Sorry,我想起來了(在地藏被眾編輯海扁一頓之后)。然后是學(xué)習(xí)這個(gè)庫的使用原理。在這里呢我們先做個(gè)假設(shè)。
??
?? 我們假設(shè)一個(gè)頁面是由很多的小的部分組成(比如:每個(gè)網(wǎng)站都有分欄目導(dǎo)航等等),而且每個(gè)小的部分都有一個(gè)唯一的標(biāo)識(shí)符(比如:我們把分欄每個(gè)小項(xiàng)定義為一個(gè)名字)
??
?? 我們首先以這個(gè)庫自帶的example_1為例,讓大家可以盡快的學(xué)會(huì)這個(gè)庫的基本用法,;),不過大家要注意喲,這幾個(gè)文件在服務(wù)器上的位置要按解壓時(shí)的相對(duì)路徑來存,千萬不要忘記,就象不要忘記你女朋友的生日一樣。呵呵~~~~
??
?? ??
?? // 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 ??{
??$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;
??
?? ? >
??
??
??
?? {TITLE}
??
??
??{MAIN}
??
??
??
??
??
??
??{ROWS}
??
??
??
??
??
??{NUMBER}
??{BIG_NUMBER}
??
??
??
?? 使用這個(gè)庫首先要如前面所說的 include "class.FastTemplate.php3"; 然后是定義模板所在目錄$tpl = new FastTemplate("./templates");注意喲!這里因?yàn)槲沂窃趙indows系統(tǒng)運(yùn)行,所以用的是"./templates",在Linux或UNIX中可能不一樣,大家根據(jù)具體情況來設(shè)定。然后呢,我們對(duì)應(yīng)不同的模板,定義下面這個(gè)矩陣數(shù)組。$tpl- >define( array( main = > "main.tpl", table = > "table.tpl", row = > "row.tpl" ) ); define是這個(gè)類中的一個(gè)函數(shù)。下面是它的語法:define( array( key,value pairs) ) ,define()函數(shù)映射一個(gè)名字到模板文件上,這個(gè)新的名字將是你用來代表模板的唯一名字,因?yàn)槌酥庠俨粫?huì)有模板文件名出現(xiàn)。而且大家注意,這是使用類時(shí)決不能缺少的步驟,不能少的喲,如果少了那就和你不帶生日禮物去你女朋友的生日Party有異曲同工之妙,嘿嘿嘿~~~~
??
?? 現(xiàn)在!關(guān)鍵的,最有個(gè)性的東西來了!assign( (key,value pair) 或 assign ( array(key value pairs) ) 這個(gè)函數(shù)將把你在模板中定義的標(biāo)識(shí)符定義為你網(wǎng)頁上真正想要的東西。比如就象是上面$tpl- >assign( array( TITLE = > "FastTemplate Test") ); 這句,將模板main.tpl里的{TITLE}替換為 FastTemplate Test 看不懂想不明的朋友多看看上面的那些代碼。接著呢!接著是另外一個(gè)很有特色的東東。parse(RETURN, FileHandle(s) ) 將一個(gè)已定義模板插入的定義到另外一個(gè)模板文件中。大家仔細(xì)研究下面的代碼,相信會(huì)比較容易了解。
??
?? for ($n=1; $n ??{
??$Number = $n;
??$BigNum = $n*10;
??$tpl- >assign(
??array(
??NUMBER = > $Number,
??BIG_NUMBER = > $BigNum
??)
??);
??$tpl- >parse(ROWS,".row");
??}
??
?? parse這個(gè)函數(shù)有三種用法
??
?? $tpl- >parse(MAIN, "main"); // 標(biāo)準(zhǔn)
??$tpl- >parse(MAIN, array ( "table", "main") ); // 簡潔
??$tpl- >parse(MAIN, ".row"); // 增加
??
?? 其中以第三種最有意思,它表示的是在原來已經(jīng)有的基礎(chǔ)上再加上一個(gè)新數(shù)據(jù)。呵呵,這么說大家可能無法明白,我們還是看看再多研究一下上面的源代碼吧,畢竟編程這東西有很多是只可意會(huì)不可言傳的?。ㄏ旅娓缴嫌⑽牡恼f明,不是地藏想偷懶不翻譯,實(shí)在是有些東西看原味的比翻譯的更加容易理解呀,
??
?? In the regular version, the template named ``main'' is loaded if it hasn't been already, 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 important 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. )
??
?? 最后在完成上面的過程后,就可以進(jìn)行輸出了,F(xiàn)astTemplate庫使用使用FastPrint(HANDLE) 來進(jìn)行輸出。
??
?? $tpl- >FastPrint();
??
?? 在經(jīng)過上面的這些步驟后,大家對(duì)使用FastTemplate庫的過程是否清楚明白呢?地藏試試把上面的過程總結(jié)一下,看看對(duì)各位朋友是否有幫助:
??
?? 首先:將庫用include加進(jìn)來
??
?? 其次:定義一個(gè)類變量,在上例中是$tpl
??
?? 再次:定義各種最小元素,比如上面的$tpl = new FastTemplate("./templates");
??
?? 然后:利用parse函數(shù)將各種最小元素進(jìn)行組合起來
??
?? 最后:用FastPrint()進(jìn)行輸出
??
?? 上面就是使用FastTemplate的最簡單的過程了。地藏寫的不好,也不知道大家能從中學(xué)到多少,《:-)

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Windows 11 brings fresh and elegant design to the forefront; the modern interface allows you to personalize and change the finest details, such as window borders. In this guide, we'll discuss step-by-step instructions to help you create an environment that reflects your style in the Windows operating system. How to change window border settings? Press + to open the Settings app. WindowsI go to Personalization and click Color Settings. Color Change Window Borders Settings Window 11" Width="643" Height="500" > Find the Show accent color on title bar and window borders option, and toggle the switch next to it. To display accent colors on the Start menu and taskbar To display the theme color on the Start menu and taskbar, turn on Show theme on the Start menu and taskbar

The message "Your organization has asked you to change your PIN" will appear on the login screen. This happens when the PIN expiration limit is reached on a computer using organization-based account settings, where they have control over personal devices. However, if you set up Windows using a personal account, the error message should ideally not appear. Although this is not always the case. Most users who encounter errors report using their personal accounts. Why does my organization ask me to change my PIN on Windows 11? It's possible that your account is associated with an organization, and your primary approach should be to verify this. Contacting your domain administrator can help! Additionally, misconfigured local policy settings or incorrect registry keys can cause errors. Right now

By default, the title bar color on Windows 11 depends on the dark/light theme you choose. However, you can change it to any color you want. In this guide, we'll discuss step-by-step instructions for three ways to change it and personalize your desktop experience to make it visually appealing. Is it possible to change the title bar color of active and inactive windows? Yes, you can change the title bar color of active windows using the Settings app, or you can change the title bar color of inactive windows using Registry Editor. To learn these steps, go to the next section. How to change title bar color in Windows 11? 1. Using the Settings app press + to open the settings window. WindowsI go to "Personalization" and then

Taskbar thumbnails can be fun, but they can also be distracting or annoying. Considering how often you hover over this area, you may have inadvertently closed important windows a few times. Another disadvantage is that it uses more system resources, so if you've been looking for a way to be more resource efficient, we'll show you how to disable it. However, if your hardware specs can handle it and you like the preview, you can enable it. How to enable taskbar thumbnail preview in Windows 11? 1. Using the Settings app tap the key and click Settings. Windows click System and select About. Click Advanced system settings. Navigate to the Advanced tab and select Settings under Performance. Select "Visual Effects"

Do you see "A problem occurred" along with the "OOBELANGUAGE" statement on the Windows Installer page? The installation of Windows sometimes stops due to such errors. OOBE means out-of-the-box experience. As the error message indicates, this is an issue related to OOBE language selection. There is nothing to worry about, you can solve this problem with nifty registry editing from the OOBE screen itself. Quick Fix – 1. Click the “Retry” button at the bottom of the OOBE app. This will continue the process without further hiccups. 2. Use the power button to force shut down the system. After the system restarts, OOBE should continue. 3. Disconnect the system from the Internet. Complete all aspects of OOBE in offline mode

Screen brightness is an integral part of using modern computing devices, especially when you look at the screen for long periods of time. It helps you reduce eye strain, improve legibility, and view content easily and efficiently. However, depending on your settings, it can sometimes be difficult to manage brightness, especially on Windows 11 with the new UI changes. If you're having trouble adjusting brightness, here are all the ways to manage brightness on Windows 11. How to Change Brightness on Windows 11 [10 Ways Explained] Single monitor users can use the following methods to adjust brightness on Windows 11. This includes desktop systems using a single monitor as well as laptops. let's start. Method 1: Use the Action Center The Action Center is accessible

We all have different preferences when it comes to display scaling on Windows 11. Some people like big icons, some like small icons. However, we all agree that having the right scaling is important. Poor font scaling or over-scaling of images can be a real productivity killer when working, so you need to know how to customize it to get the most out of your system's capabilities. Advantages of Custom Zoom: This is a useful feature for people who have difficulty reading text on the screen. It helps you see more on the screen at one time. You can create custom extension profiles that apply only to certain monitors and applications. Can help improve the performance of low-end hardware. It gives you more control over what's on your screen. How to use Windows 11

The activation process on Windows sometimes takes a sudden turn to display an error message containing this error code 0xc004f069. Although the activation process is online, some older systems running Windows Server may experience this issue. Go through these initial checks, and if they don't help you activate your system, jump to the main solution to resolve the issue. Workaround – close the error message and activation window. Then restart the computer. Retry the Windows activation process from scratch again. Fix 1 – Activate from Terminal Activate Windows Server Edition system from cmd terminal. Stage – 1 Check Windows Server Version You have to check which type of W you are using
