前提:
1. 部署smarty模板目錄;
2. 編寫Smarty類的子類,定制好template_dir、compile_dir、config_dir、cache_dir、left_delimiter、right_delimiter、compile_check、caching等配置信息。
3. 在BaseAction類中定義該類對象,然后便可使用。
4. 暫定delimiter使用{和}

一.  變量

1. php變量
   A. 普通變量:{$var}
   B. 關(guān)聯(lián)數(shù)組:{$array.var1.var2}
   C. 數(shù)字數(shù)組:{$array[0][1]}
   D. 對象:{$object->var}
   E. 在引號內(nèi)的使用方法,以關(guān)聯(lián)數(shù)組為例,其他類似:{function var="{$array.var1.var2}"}
2. conf變量
   A. {#var#}
   B. 在引號內(nèi)的使用方法:{function var="{#var#}"}
3. smarty保留變量:略。

二. 變量調(diào)節(jié)器

1. capitalize / lower / upper
   用途:首字母大寫 / 轉(zhuǎn)為小寫 / 轉(zhuǎn)為大寫
   用法:{$var|capitalize} / {$var|lower} / {$var|upper}
2. count_characters / count_paragraphs / count_sentences / count_words
   用途:計算字節(jié)數(shù),默認不計算空格符 / 計算段落數(shù)量 / 計算句子數(shù)量 / 計算詞數(shù)
   用法:{$var|count_characters}  {$var|count_characters:true} / {$article|count_paragraphs} / {$article|count_sentences} / {$sentence|count_words}
3. cat
   用途:字符串連接
   用法:{$var|cat:" is a boy."}
4. nl2br
   用途:換行符替換為<br/>
   用法:{$var|nl2br}
5. regex_replace / replace
   用途:正則替換 / 普通替換
   用法:{$var|regex_replace:"/\[old\]/":"[new]"} / {$var|replace:"old":"new"}
6. spacify
   用途:在每個字符間插入指定字符
   用法:{$var|spacify:"^_^"}
7. date_format
   用途:格式化日期,類似"-1 days ago"、"201111010000"、時間戳等都可以,與strftime()功能類似
   用法:{$time|date_format:"%H:%M:%S"}
8. default
   用途:當變量為空時,設(shè)置默認值
   用法:{$var|default:"no value"}
9. escape
   用途:轉(zhuǎn)碼,包括:html,htmlall,url,quotes,hex,hexentity,javascript
   用法:{$articleTitle|escape:"html"}
10.indent
   用途:文字縮進,可以制定縮進字符數(shù)和使用什么字符代替
   用法:{$var|indent}  {$var|indent:4}  {$var|indent:4:"\t"}
11.string_format
   用途:類似用sprintf
   用法:{$var|string_format:"%.2f"}
12.strip / strip_tags
   用途:去除多余空白符,可以指定去除的字符 / 去除<>以及包含在里面的所有字符
   用法:{$var|strip}  {$var|strip:"&nbsp;"} / {$var|strip_tags}
13.truncate
   用途:字符串截取,默認截取80字符,可以指定追加的字符串
   用法:{$var|truncate:40}  {$var|truncate:40:"...":true}
14.組合修改器
   用途:顧名思義,可以將多個變量調(diào)節(jié)器組合使用,中間用|來代替
   例子:{$articleTitle|lower|spacify|truncate:30:"..."}

三. 內(nèi)建函數(shù)

1. capture
   用途:捕獲模板內(nèi)容到某變量var,并不進行輸出
   用法:
   在{capture}{/capture}中間的數(shù)據(jù)被捕獲,可以使用$smarty.capture.var來使用,不指定name的話,默認為default
   {capture name=banner}
   This is a test.
   {/capture}
2. foreach
   用途:循環(huán)處理
   用法:
   from: 數(shù)組,需要用$
   item: 單元元素名稱,不需要用$
   key: key名稱,不需要用$
   name: 該循環(huán)的名稱,可以用于訪問該循環(huán),例如:{$smarty.foreach.foreachname.varname}
   {foreach item=contact from=$contacts}
   {foreach key=key item=item from=$contact}
   {$key}: {$item}<br>
   {/foreach}
   {/foreach}
   注意:在foreach中有一些特殊的變量,需要使用{$smarty.foreach.foreachname.***}來訪問:
   iteration: 表示當前循環(huán)的執(zhí)行次數(shù),初始為1
   first: 循環(huán)第一次執(zhí)行時被置為true
   last: 同上
   total: 用于顯示循環(huán)執(zhí)行的次數(shù),在循環(huán)中或者循環(huán)后皆可使用
   show: 是foreach的一個標簽,用于決定是否顯示該foreach的內(nèi)容
3. include / insert
   用途:包含其他模板 / 與include不同, insert 所包含的內(nèi)容不會被緩存,每次調(diào)用該模板都會重新執(zhí)行該函數(shù).
   用法:{include file="footer.tpl" title="Main menu" logo="4. if elseif else
   用途:分之判斷
   用法:
   {if $name eq "Fred"}
   Welcome Sir.
   {elseif $name eq "Wilma"}
   Welcome Ma'am.
   {else}
   Welcome, whatever you are.
   {/if}
5. ldelim / rdelim
   用途:分別表示左括號、右括號,因為這兩個符號被用作smarty模板的標識符
   用法:
   {ldelim}  {rdelim}
6. literal
   用途:在內(nèi)部的數(shù)據(jù)當做文本處理,不使用smarty模板解析,主要用于javascript腳本等
   用法:
   {literal}
   ......
   {/literal}
7. section
   用途:循環(huán)處理
   用法:
   name: 該循環(huán)的名稱
   loop:決定循環(huán)次數(shù)的數(shù)組,注意這里的使用方法比較特別,可以使用section來對多個數(shù)組進行處理,但必須先用可以決定循環(huán)次數(shù)的數(shù)組給loop賦值
   {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]}: {$contact_info[customer][contact]}<br>
   {/section}
   <p>
   {/section}
   如果要遍歷多維關(guān)聯(lián)數(shù)組,需要這樣來使用:
   {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}
   注意:與foreach類似,在section中同樣有一些特殊變量可供使用,使用方法是:{$smarty.section.sectionname.***}
   index: 顯示當前循環(huán)的索引,默認從0或者start開始
   index_prev: 顯示上一個循環(huán)索引值,默認從-1開始
   index_next: 同上,直至最后都比上一次大1
   ineration: 同foreach,與index不同,后者是索引
   first: 同foreach
   last: 同foreach
   show: 同foreach
   total: 同foreach
8. strip
   用途:將位于{strip}{/strip}內(nèi)部html標簽外的所有空格和回車清除干凈,并以html標簽開頭和結(jié)尾
   用法:
   {strip}
   <table border=0>
     <tr>
    <td>
      <A HREF="{$url}">
   <font color="red">This is a test</font>
   </A>
    </td>
  </tr>
   </table>
   {/strip}
   上述輸出為:
   <table border=0><tr><td><A HREF="
color="red">This is a test</font></A></td></tr></table>

四. 程序員需要注意的地方

1. 常量
   SMARTY_DIR:SMARTY_DIR常量用于定位smarty類文件的完整系統(tǒng)路徑,必須以斜杠結(jié)束,也可以不定義,smarty模板會自動創(chuàng)建合適的值。
2. 類屬性
   $template_dir: 模板目錄,也就是各html文件放置目錄,默認為"./templates"
   $compile_dir: 編譯后的模板目錄,經(jīng)過smarty解析后的html文件放置目錄,默認為"./templates_c"
   $config_dir: 模板配置文件目錄,默認為"./configs"
   $plugins_dir: 插件目錄,默認為SMARTY_DIR 。 "plugins"
   $cache_dir: 存放模板緩存的目錄,默認為"./cache"
   $debugging: 可以啟動調(diào)試控制臺,默認為false,很有用啊!!!
   $debug_tpl: 定義用于調(diào)試控制臺的模板文件名字,默認為SMARTY_DIR . "libs/debug.tpl"
   $global_assign: 用于定義全局變量,例如:
       php里:$this->tpl->global_assign = array('my_global_1' => .....);
    在template里:{$smarty.my_global_1.***}
   $compile_check: 自動編譯模板,默認設(shè)置為true,投入產(chǎn)品后為性能起見,可以設(shè)置為false。
   $force_compile: 強迫每次調(diào)用時重新編譯模板,默認為false,不受$compile_check的限制,一旦設(shè)置為true后會強迫重新編譯。
   $caching: 是否緩存模板輸出,默認為false,有利于增強性能。
   $caching_lifetime: 緩存生存時間,只在$caching為true時有效,-1表示永遠有效,0表示永遠需要重新生成。單位是秒。
   注意:$compile_check、$force_compile、$caching三者的關(guān)系如下:
   A. 如果設(shè)置了$compile_check,如果任何模板文件或配置文件更新,都會重新編譯,緩存也會重新生成;
   B. 如果設(shè)置了$force_compile,則$compile_check不起作用,而緩存也總會重新生成;
   C. 如果沒有設(shè)置$caching,則沒有緩存,性能受一定影響。

五. smarty的method

1. assign: 對模板使用到的php變量進行賦值
2. assign_by_ref: 也是賦值,不過是引用賦值
3. clear_all_assign: 清除所有賦值
4. clear_all_cache: 清除所有緩存,參數(shù)可以指定閾值時間
5. clear_assign: 清除某個賦值,可以指定單個變量名或者數(shù)組
6. clear_cache: 清除某個template的緩存,需要指定template的名稱
7. clear_config: 清除所有配置變量,如果指定則清除特定配置變量
8. config_load: 加載某配置文件并將數(shù)據(jù)輸出到模板
9. display: 顯示某個模板
10.fetch: 捕獲某個模板的輸出,與display不同的是,模板輸出內(nèi)容并不會直接顯示出來(有什么用呢???)
11.get_config_vars: 獲取所有配置變量的值,也可以指定某個具體的變量名
12.get_template_vars: 獲取所有模板變量的值,也可以指定某個具體的變量名
13.is_cached: 判斷某模板的緩存是否存在,只有在$caching設(shè)置為true時才有效
14.template_exists: 判斷某模板是否存在