TShopping

 找回密碼
 註冊
搜索
查看: 2522|回復: 1
打印 上一主題 下一主題

[教學] Smarty 樣版

[複製鏈接]
跳轉到指定樓層
1#
發表於 2010-1-21 17:57:27 | 只看該作者 回帖獎勵 |倒序瀏覽 |閱讀模式
 
Push to Facebook
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
  1. include ("class/Smarty.class.php");            //引入Smarty物件
  2.    define('__SITE_ROOT','c:/appserv/www/store/'); //定義網站根目錄
  3.    $smarty = new Smarty();//產生樣版物件                 
  4. //樣版預設值
  5.    $smarty->template_dir = __SITE_ROOT . "/templates/";
  6.    $smarty->compile_dir = __SITE_ROOT . "/templates_c/";
  7.    $smarty->config_dir = __SITE_ROOT . "/configs/";
  8.    $smarty->cache_dir = __SITE_ROOT . "/cache/";
  9.    $smarty->left_delimiter = '<{';
  10.    $smarty->right_delimiter = '}>'; //指定變數
複製代碼
  1. $tpl->assign('title','第一個Smarty程式');
  2. $tpl->assign('msg','台東縣多媒體教材平台');  //顯示樣版
複製代碼
  1. $smarty->display('test.htm');
複製代碼
test.htm
  1. <html> <head>
  2. <title><{title}></title>
  3. </head>
  4. <body> <{msg}> </body>
  5. </html>
複製代碼
四、Smarty 樣版語法

(一)、基本語法(Basic Syntax)
(二)、變數(Variables)
(三)、修飾變數(Variable Modifiers )
(四)、內建函數(Built-in Functions )
(五)、樣版應用函數(Custom Functions)
詳細說明..... 官方文件
五、基本語法(Basic Syntax)



(1)、註解       <{* 這是一個註解 *}>      
(2)、函數
  1. <{config_load file="colors.conf"}>
  2. <{include file="header.tpl"}>
  3. <{if $highlight_name}> Welcome, <font color=“<{#fontColor#}>"><{$name}>!</font>
  4. <{else}> Welcome, <{$name}>!
  5. <{/if}>
  6. <{include file="footer.tpl"} >
複製代碼

(3)、函數的參數
  1. <{include file="header.tpl"}>
  2. <{include file=$includeFile}>
  3. <{include file=#includeFile#}>
  4. <{html_select_date display_days=yes}>
  5. <select name="company">
  6. <{html_options values=$vals selected=$selected output=$output} >
  7. </select>
複製代碼

Smarty函數的參數類似 HTML 的寫法,參數如有預設值,可以不設定,另外參數值若為布林值時,
可設成 true, on,yes, 或 false, off, no  
(4)、將雙引號內 "" 代入變數  語法範例
  1. <{func var="test $foo test"}>
  2. <-- sees $foo <{func var="test $foo_bar test"} >
  3. <-- sees $foo_bar <{func var="test $foo[0] test"} >
  4. <-- sees $foo[0] <{func var="test $foo[bar] test"} >
  5. <-- sees $foo[bar] <{func var="test $foo.bar test"} >
  6. <-- sees $foo (not $foo.bar) <{func var="test `$foo.bar` test"} >
  7. <-- sees $foo.bar>
複製代碼
實際範例

  1. <{include file="subdir/$tpl_name.tpl"} >
  2. <-- will replace $tpl_name with value  >     
  3. <{cycle values="one,two,`$smarty.config.myval`"}>
  4. <-- must have backticks>  
複製代碼
(5)、運算
  1. <{$foo+1}>
  2. <{$foo*$bar}>
  3. <{* some more complicated examples *}>
  4. <{$foo->bar-$bar[1]*$baz->foo->bar()-3*7}>
  5. <{if ($foo+$bar.test%$baz*134232+10+$b+10)}>
  6. <{$foo|truncate:"`$fooTruncCount/$barTruncFactor-1`"} >
  7. <{assign var="foo" value="`$foo+$bar`"} >
複製代碼
(6)、跳脫符號
  1. <?php $smarty = new Smarty;
  2. $smarty->left_delimiter = '<{'; $smarty->right_delimiter = '}>';
  3. $smarty->assign('foo', 'bar'); $smarty->display('example.tpl'); ?>
複製代碼
六、變數(Variables)



(1)、由PHP指派變數給樣版(變數、陣列、物件)         
在PHP指派變數
  1. <?php $smarty->assign('firstname','Grace'); //單一變數
  2. $smarty->assign('Contacts',array('fax' =>'555-222-9876', 'email'=>'zaphod@slartibartfast.com', 'phone'=> array('home'=>'555-444-3333', 'cell' =>'555-111-1234'))); //陣列
  3. $smarty->assign('myobj',$conn); //物件 ?>
複製代碼
樣版中取得方式
  1. <{$firstname}> <{* 單一變數 *}> <{$Contacts.fax}>
  2. <{$Contacts.phone.home}> <{* 取得陣列以. *}> <{$Contacts[0]}>
  3. <{$Contacts[2][0]}> <{* 取得陣列以[] *}>
  4. <{$myobj->setcolor()}> <{* 取得物件以-> *}>
複製代碼
(2)、從樣版中載入config 檔  
foo.conf
  1. pageTitle = "This is mine" bodyBgColor = "#eeeeee"
  2. tableBorderSize = "3" tableBgColor = "#bbbbbb" rowBgColor = "#cccccc"
複製代碼
index.tpl:
  1. <{config_load file="foo.conf"}> <html>
  2. <title> <{#pageTitle#} ></title>
  3. <body bgcolor="<{#bodyBgColor#}>">
  4. <table border="<{#tableBorderSize#}>" bgcolor="<{#tableBgColor#}>">
  5. <tr bgcolor="<{#rowBgColor#}>">
  6. <td>First</td>
  7. <td>Last</td>
  8. <td>Address</td>
  9. </tr>
  10. </table>
  11. </body>
  12. </html>
複製代碼
(3)、保留變數
  1. <{* display value of page from URL (GET) *}>
  2. <{$smarty.get.page}>
  3. <{* display the variable "page" from a form (POST) *}>
  4. <{$smarty.post.page}>
  5. <{* display the value of the cookie "username" *}>
  6. <{$smarty.cookies.username}>
  7. <{* display the server variable "SERVER_NAME" *}>
  8. <{$smarty.server.SERVER_NAME}>
  9. <{* display the php session variable "id" *}>
  10. <{$smarty.session.id} >
  11. <{* display the variable "username" from merged get/post/cookies/server/env *}>
  12. <{$smarty.request.username}>
  13. <{$smarty.now}> <{$smarty.const}>
  14. <{$smarty.capture}> <{$smarty.config}>
  15. <{$smarty.section}> <{$smarty.foreach}>
  16. <{$smarty.template}> <{$smarty.version}>
  17. <{$smarty.ldelim}> <{$smarty.rdelim}>
複製代碼
(4)、修飾變數
1.換行(遇\n換成<br>)
  1. <{$articleTitle|nl2br}>
複製代碼
2.計算字元
  1. <{$articleTitle|count_characters}>
複製代碼
3.時間格式
  1. <{$yesterday|date_format:"%H:%M:%S"}>
複製代碼
4.換行(幾個字元就換行)
  1. <{$articleTitle|wordwrap:30:"<br />\n"}>
複製代碼
七、內建函數(Built-in Functions )



(1)if,elseif,else 使用範例:
  1. <{if $name eq "Fred"}> Welcome Sir.
  2. <{elseif $name eq "Wilma"} > Welcome Ma'am.
  3. <{else} > Welcome, whatever you are. <{/if}>
複製代碼
(2)section,sectionelse 使用範例:
  1. <{section name=customer loop=$custid}> id:
  2. <{ $custid[customer] }><br>
  3. <{/section}>
複製代碼
其中loop後面接的是陣列,name後面接的是指標
  1. <{section name=customer loop=$custid}>
  2.       id: <{ $custid[customer]}><br>
  3.       name:< {$name[customer]}><br>
  4.       address: <{$address[customer]}><br>
  5.       <{section name=contact loop=$contact_type[customer]}>
  6.            <{$contact_type[customer][contact]}><br>
  7.       <{/section}>
  8.        <p>
  9.     <{/section}>
  10.          
  11.    <{section name=customer loop=$contacts}>
  12.        name: <{$contacts[customer].name}><br>
  13.        home: <{$contacts[customer].home}><br>
  14.        cell: <{$contacts[customer].cell}><br>
  15.        e-mail: <{$contacts[customer].email}><p>
  16.     <{/section} >   <{section name=customer loop=$custid}>
  17.          id: <{$custid[customer]}><br>
  18.    <{sectionelse}>
  19.          there are no values in $custid.
  20. <{/section} >      
複製代碼
八、樣版應用函數(Custom Functions



一、 html_checkboxes
  1. $smarty->assign('cust_checkboxes', array( 1000 => 'Joe Schmoe', 1001 => 'Jack Smith', 1002 => 'Jane Johnson', 1003 => 'Charlie Brown'));
  2. $smarty->assign('customer_id', 1001);
複製代碼
樣版
  1. <{html_checkboxes name="id" options=$cust_checkboxes selected=$customer_id separator=“<br />"} >
複製代碼
輸出
  1. <label><input type="checkbox" name="id[]" value="1000" />Joe Schmoe</label><br /> <label>
  2. <input type="checkbox" name="id[]" value="1001" checked="checked" />Jack Smith</label><br /> <label>
  3. <input type="checkbox" name="id[]" value="1002" />Jane Johnson</label><br />
  4. <label> <input type="checkbox" name="id[]" value="1003" />Charlie Brown</label><br>
複製代碼
二、 html_image
  1. <{html_image file="pumpkin.jpg"}>
  2. <img src="pumpkin.jpg" alt="" border="0" width="44" height="68" />
複製代碼
三、 html_radios
  1. smarty->assign('cust_radios', array( 1000 => 'Joe Schmoe', 1001 => 'Jack Smith', 1002 => 'Jane Johnson', 1003 => 'Charlie Brown'));
  2. $smarty->assign('customer_id', 1001);
複製代碼
樣版
  1. <{html_radios name="id" options=$cust_radios selected=$customer_id separator="<br />"}
  2. <input type="radio" name="id" value="1000">Joe Schmoe<br />
  3. <input type="radio" name="id" value="1001" checked="checked">Jack Smith<br />
  4. <input type="radio" name="id" value="1002">Jane Johnson<br />
  5. <input type="radio" name="id" value="1003">Charlie Brown<br />
複製代碼
四、 html_options
  1. $smarty->assign('cust_options', array( 1001 => 'Joe Schmoe', 1002 => ‘Jack Smith', 1003 => 'Jane Johnson', 1004 => 'Charlie Brown'));
  2. $smarty->assign('customer_id', 1001);
複製代碼
樣版
  1. <select name=customer_id>
  2.          <{html_options options=$cust_options selected=$customer_id} >
  3.       </select>        <select name=customer_id>
  4.          <option value="1000">Joe Schmoe</option>
  5.          <option value="1001" selected="selected">Jack Smith</option>
  6.          <option value="1002">Jane Johnson</option>
  7.          <option value="1003">Charlie Brown</option>
  8.       </select>
複製代碼
五、 html_select_date 、html_select_time
  1. <{html_select_date prefix="StartDate" time=$time start_year="-5" end_year="+1" display_days=false} >
  2. <{html_select_time use_24_hours=true} >
複製代碼
六、 html_table
  1. <?php require('Smarty.class.php');
  2. $smarty = new Smarty; $smarty->assign('data',array(1,2,3,4,5,6,7,8,9));
  3. $smarty->assign('tr',array('bgcolor="#eeeeee"','bgcolor="#dddddd"'));
  4. $smarty->display('index.tpl'); ?> <{html_table loop=$data}>
  5. <{html_table loop=$data cols=4 table_attr='border="0"'}>
  6. <{html_table loop=$data cols=4 tr_attr=$tr}>
複製代碼

 

臉書網友討論
*滑块验证:
您需要登錄後才可以回帖 登錄 | 註冊 |

本版積分規則



Archiver|手機版|小黑屋|免責聲明|TShopping

GMT+8, 2024-5-8 19:12 , Processed in 0.092427 second(s), 18 queries .

本論壇言論純屬發表者個人意見,與 TShopping綜合論壇 立場無關 如有意見侵犯了您的權益 請寫信聯絡我們。

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回復 返回頂部 返回列表