| 
 | 
 
 
Smarty 樣版 
 | 一、為何要使用樣版 |  PHP程式碼與HTML交雜在一起,產生 
1.程式複雜化(有邏輯運算又有視覺展現)  
2.程式設計師無法和美工設計者協同合作,在大的專案 更顯窘態  
使用樣版可將程式邏輯與網頁展現分離,讓程式人員與美工人員各司其值。目前PHP的樣版有PHPLib、FastTemplat e、Smarty等 |  | 二、下載Smarty樣版 |   
(一)下載: 官方網站 http://smarty.php.net          
(二)安裝:    1.將Smarty-2.6.6.tar.gz解開,會建立一個新目錄Smarty-2.6.6  
2.在專案底下建立以下目錄  
templates //存放網頁樣版  
templates_c //存放編譯後的PHP檔(權限設成777)  
configs //存放設定檔  
cache //存放快取檔案(權限設成777)  
class //*將Smarty-2.6.6中libs內所有檔案複製至此  
modules //*各模組程式至於此  
includes //*共同引入檔  |  | 三、使用步驟與簡單範例 |   
 
 
(一)使用步驟    1.引入 Smarty.class.php 檔案 2.產生 Smarty 物件 3.設定 Smarty 物件的參數 4.設定樣版中的變數 5.顯示樣版             (二)簡單範例 test.php- include ("class/Smarty.class.php");            //引入Smarty物件
 
 -    define('__SITE_ROOT','c:/appserv/www/store/'); //定義網站根目錄
 
 -    $smarty = new Smarty();//產生樣版物件                 
 
 - //樣版預設值
 
 -    $smarty->template_dir = __SITE_ROOT . "/templates/";
 
 -    $smarty->compile_dir = __SITE_ROOT . "/templates_c/";
 
 -    $smarty->config_dir = __SITE_ROOT . "/configs/";
 
 -    $smarty->cache_dir = __SITE_ROOT . "/cache/";
 
 -    $smarty->left_delimiter = '<{';
 
 -    $smarty->right_delimiter = '}>'; //指定變數
 
 
  複製代碼- $tpl->assign('title','第一個Smarty程式'); 
 
 - $tpl->assign('msg','台東縣多媒體教材平台');  //顯示樣版
 
 
  複製代碼- $smarty->display('test.htm');
 
  複製代碼 test.htm- <html> <head> 
 
 - <title><{title}></title> 
 
 - </head> 
 
 - <body> <{msg}> </body> 
 
 - </html>
 
  複製代碼 |  | 四、Smarty 樣版語法  |   
(一)、基本語法(Basic Syntax) 
(二)、變數(Variables) 
(三)、修飾變數(Variable Modifiers ) 
(四)、內建函數(Built-in Functions ) 
(五)、樣版應用函數(Custom Functions) 
詳細說明.....  官方文件 |  | 五、基本語法(Basic Syntax) |   
 
 
(1)、註解       <{* 這是一個註解 *}>        
(2)、函數- <{config_load file="colors.conf"}> 
 
 - <{include file="header.tpl"}> 
 
 - <{if $highlight_name}> Welcome, <font color=“<{#fontColor#}>"><{$name}>!</font> 
 
 - <{else}> Welcome, <{$name}>! 
 
 - <{/if}> 
 
 - <{include file="footer.tpl"} > 
 
  複製代碼 
(3)、函數的參數 
- <{include file="header.tpl"}> 
 
 - <{include file=$includeFile}> 
 
 - <{include file=#includeFile#}> 
 
 - <{html_select_date display_days=yes}> 
 
 - <select name="company"> 
 
 - <{html_options values=$vals selected=$selected output=$output} > 
 
 - </select> 
 
 
  複製代碼 
Smarty函數的參數類似 HTML 的寫法,參數如有預設值,可以不設定,另外參數值若為布林值時, 
可設成 true, on,yes, 或 false, off, no   
(4)、將雙引號內 "" 代入變數  語法範例 
- <{func var="test $foo test"}> 
 
 - <-- sees $foo <{func var="test $foo_bar test"} > 
 
 - <-- sees $foo_bar <{func var="test $foo[0] test"} > 
 
 - <-- sees $foo[0] <{func var="test $foo[bar] test"} > 
 
 - <-- sees $foo[bar] <{func var="test $foo.bar test"} > 
 
 - <-- sees $foo (not $foo.bar) <{func var="test `$foo.bar` test"} > 
 
 - <-- sees $foo.bar> 
 
  複製代碼 實際範例-  
 
 - <{include file="subdir/$tpl_name.tpl"} > 
 
 - <-- will replace $tpl_name with value  >     
 
 - <{cycle values="one,two,`$smarty.config.myval`"}> 
 
 - <-- must have backticks>  
 
  複製代碼 (5)、運算- <{$foo+1}> 
 
 - <{$foo*$bar}> 
 
 - <{* some more complicated examples *}> 
 
 - <{$foo->bar-$bar[1]*$baz->foo->bar()-3*7}> 
 
 - <{if ($foo+$bar.test%$baz*134232+10+$b+10)}> 
 
 - <{$foo|truncate:"`$fooTruncCount/$barTruncFactor-1`"} > 
 
 - <{assign var="foo" value="`$foo+$bar`"} > 
 
  複製代碼 (6)、跳脫符號- <?php $smarty = new Smarty; 
 
 - $smarty->left_delimiter = '<{'; $smarty->right_delimiter = '}>'; 
 
 - $smarty->assign('foo', 'bar'); $smarty->display('example.tpl'); ?> 
 
 
  複製代碼 |  | 六、變數(Variables) |   
 
 
(1)、由PHP指派變數給樣版(變數、陣列、物件)          
在PHP指派變數- <?php $smarty->assign('firstname','Grace'); //單一變數 
 
 - $smarty->assign('Contacts',array('fax' =>'555-222-9876', 'email'=>'zaphod@slartibartfast.com', 'phone'=> array('home'=>'555-444-3333', 'cell' =>'555-111-1234'))); //陣列 
 
 - $smarty->assign('myobj',$conn); //物件 ?>
 
  複製代碼 樣版中取得方式- <{$firstname}> <{* 單一變數 *}> <{$Contacts.fax}>
 
 - <{$Contacts.phone.home}> <{* 取得陣列以. *}> <{$Contacts[0]}>
 
 - <{$Contacts[2][0]}> <{* 取得陣列以[] *}> 
 
 - <{$myobj->setcolor()}> <{* 取得物件以-> *}> 
 
  複製代碼 (2)、從樣版中載入config 檔   
foo.conf- pageTitle = "This is mine" bodyBgColor = "#eeeeee" 
 
 - tableBorderSize = "3" tableBgColor = "#bbbbbb" rowBgColor = "#cccccc" 
 
  複製代碼 index.tpl: - <{config_load file="foo.conf"}> <html> 
 
 - <title> <{#pageTitle#} ></title> 
 
 - <body bgcolor="<{#bodyBgColor#}>"> 
 
 - <table border="<{#tableBorderSize#}>" bgcolor="<{#tableBgColor#}>"> 
 
 - <tr bgcolor="<{#rowBgColor#}>"> 
 
 - <td>First</td> 
 
 - <td>Last</td> 
 
 - <td>Address</td>
 
 - </tr>
 
 - </table>
 
 - </body>
 
 - </html>
 
  複製代碼 (3)、保留變數- <{* display value of page from URL (GET) *}> 
 
 - <{$smarty.get.page}> 
 
 - <{* display the variable "page" from a form (POST) *}> 
 
 - <{$smarty.post.page}> 
 
 - <{* display the value of the cookie "username" *}> 
 
 - <{$smarty.cookies.username}> 
 
 - <{* display the server variable "SERVER_NAME" *}> 
 
 - <{$smarty.server.SERVER_NAME}> 
 
 - <{* display the php session variable "id" *}> 
 
 - <{$smarty.session.id} > 
 
 - <{* display the variable "username" from merged get/post/cookies/server/env *}> 
 
 - <{$smarty.request.username}> 
 
 - <{$smarty.now}> <{$smarty.const}> 
 
 - <{$smarty.capture}> <{$smarty.config}> 
 
 - <{$smarty.section}> <{$smarty.foreach}> 
 
 - <{$smarty.template}> <{$smarty.version}> 
 
 - <{$smarty.ldelim}> <{$smarty.rdelim}> 
 
  複製代碼 (4)、修飾變數  
1.換行(遇\n換成<br>)2.計算字元- <{$articleTitle|count_characters}>
 
  複製代碼 3.時間格式- <{$yesterday|date_format:"%H:%M:%S"}>
 
  複製代碼 4.換行(幾個字元就換行)- <{$articleTitle|wordwrap:30:"<br />\n"}>
 
  複製代碼 |  | 七、內建函數(Built-in Functions ) |   
 
 
(1)if,elseif,else 使用範例:- <{if $name eq "Fred"}> Welcome Sir. 
 
 - <{elseif $name eq "Wilma"} > Welcome Ma'am. 
 
 - <{else} > Welcome, whatever you are. <{/if}>
 
  複製代碼 (2)section,sectionelse 使用範例:- <{section name=customer loop=$custid}> id: 
 
 - <{ $custid[customer] }><br> 
 
 - <{/section}>
 
  複製代碼 其中loop後面接的是陣列,name後面接的是指標- <{section name=customer loop=$custid}>
 
 -       id: <{ $custid[customer]}><br> 
 
 -       name:< {$name[customer]}><br> 
 
 -       address: <{$address[customer]}><br> 
 
 -       <{section name=contact loop=$contact_type[customer]}> 
 
 -            <{$contact_type[customer][contact]}><br>
 
 -       <{/section}> 
 
 -        <p> 
 
 -     <{/section}> 
 
 -          
 
 -    <{section name=customer loop=$contacts}> 
 
 -        name: <{$contacts[customer].name}><br> 
 
 -        home: <{$contacts[customer].home}><br> 
 
 -        cell: <{$contacts[customer].cell}><br> 
 
 -        e-mail: <{$contacts[customer].email}><p> 
 
 -     <{/section} >   <{section name=customer loop=$custid}> 
 
 -          id: <{$custid[customer]}><br> 
 
 -    <{sectionelse}> 
 
 -          there are no values in $custid.
 
 - <{/section} >       
 
 
  複製代碼 |  | 八、樣版應用函數(Custom Functions  |   
 
 
一、 html_checkboxes- $smarty->assign('cust_checkboxes', array( 1000 => 'Joe Schmoe', 1001 => 'Jack Smith', 1002 => 'Jane Johnson', 1003 => 'Charlie Brown')); 
 
 - $smarty->assign('customer_id', 1001); 
 
 
  複製代碼 樣版- <{html_checkboxes name="id" options=$cust_checkboxes selected=$customer_id separator=“<br />"} > 
 
  複製代碼 輸出- <label><input type="checkbox" name="id[]" value="1000" />Joe Schmoe</label><br /> <label>
 
 - <input type="checkbox" name="id[]" value="1001" checked="checked" />Jack Smith</label><br /> <label>
 
 - <input type="checkbox" name="id[]" value="1002" />Jane Johnson</label><br /> 
 
 - <label> <input type="checkbox" name="id[]" value="1003" />Charlie Brown</label><br>
 
  複製代碼 二、 html_image- <{html_image file="pumpkin.jpg"}> 
 
 - <img src="pumpkin.jpg" alt="" border="0" width="44" height="68" /> 
 
  複製代碼 三、 html_radios- smarty->assign('cust_radios', array( 1000 => 'Joe Schmoe', 1001 => 'Jack Smith', 1002 => 'Jane Johnson', 1003 => 'Charlie Brown'));
 
 - $smarty->assign('customer_id', 1001); 
 
  複製代碼 樣版- <{html_radios name="id" options=$cust_radios selected=$customer_id separator="<br />"} 
 
 - <input type="radio" name="id" value="1000">Joe Schmoe<br /> 
 
 - <input type="radio" name="id" value="1001" checked="checked">Jack Smith<br /> 
 
 - <input type="radio" name="id" value="1002">Jane Johnson<br /> 
 
 - <input type="radio" name="id" value="1003">Charlie Brown<br />
 
  複製代碼 四、 html_options- $smarty->assign('cust_options', array( 1001 => 'Joe Schmoe', 1002 => ‘Jack Smith', 1003 => 'Jane Johnson', 1004 => 'Charlie Brown')); 
 
 - $smarty->assign('customer_id', 1001); 
 
  複製代碼 樣版- <select name=customer_id> 
 
 -          <{html_options options=$cust_options selected=$customer_id} >
 
 -       </select>        <select name=customer_id>
 
 -          <option value="1000">Joe Schmoe</option> 
 
 -          <option value="1001" selected="selected">Jack Smith</option> 
 
 -          <option value="1002">Jane Johnson</option> 
 
 -          <option value="1003">Charlie Brown</option> 
 
 -       </select> 
 
 
  複製代碼 五、 html_select_date 、html_select_time- <{html_select_date prefix="StartDate" time=$time start_year="-5" end_year="+1" display_days=false} > 
 
 - <{html_select_time use_24_hours=true} >
 
  複製代碼 六、 html_table- <?php require('Smarty.class.php'); 
 
 - $smarty = new Smarty; $smarty->assign('data',array(1,2,3,4,5,6,7,8,9)); 
 
 - $smarty->assign('tr',array('bgcolor="#eeeeee"','bgcolor="#dddddd"')); 
 
 - $smarty->display('index.tpl'); ?> <{html_table loop=$data}> 
 
 - <{html_table loop=$data cols=4 table_attr='border="0"'}> 
 
 - <{html_table loop=$data cols=4 tr_attr=$tr}>
 
  複製代碼 |  
  |   
 
 
 
 |