smarty模版手冊


一.  變量

1. php變量
   A. 普通變量:{$var}
   B. 關聯數組:{$array.var1.var2}
   C. 數字數組:{$array[0][1]}
   D. 對象:{$object->var}
   E. 在引號內的使用方法,以關聯數組爲例,其他類似:{function var="{$array.var1.var2}"}
   F. 聲明變量 :
      {assign var="id" value="0"}
      {assign var=running_total value=`$running_total+$some_array[row].some_value`}
      {assign var=fitem value=$pout.a.b scope="global"}

2. conf變量
   A. {#var#}
   B. 在引號內的使用方法:{function var="{#var#}"}

3. smarty保留變量:略。

二. 變量調節器

1. capitalize / lower / upper
   用途:首字母大寫 / 轉爲小寫 / 轉爲大寫
   用法:{$var|capitalize} / {$var|lower} / {$var|upper}
2. count_characters / count_paragraphs / count_sentences / count_words
   用途:計算字節數,默認不計算空格符 / 計算段落數量 / 計算句子數量 / 計算詞數
   用法:{$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"}
      {$item.create_time|date_format:'%Y-%m-%d %H:%M:%S'}
8. default
   用途:當變量爲空時,設置默認值
   用法:{$var|default:"no value"}
9. escape
   用途:轉碼,包括:html,htmlall,url,quotes,hex,hexentity,javascript
   用法:{$articleTitle|escape:"html"}
10.indent
   用途:文字縮進,可以制定縮進字符數和使用什麼字符代替
   用法:{$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.組合修改器
   用途:顧名思義,可以將多個變量調節器組合使用,中間用|來代替
   例子:{$articleTitle|lower|spacify|truncate:30:"..."}

三. 內建函數

1. capture
   用途:捕獲模板內容到某變量var,並不進行輸出
   用法:
   在{capture}{/capture}中間的數據被捕獲,可以使用$smarty.capture.var來使用,不指定name的話,默認爲default
   {capture name=banner}
   This is a test.
   {/capture}
2. foreach
   用途:循環處理
   用法:
   from: 數組,需要用$
   item: 單元元素名稱,不需要用$
   key: key名稱,不需要用$
   name: 該循環的名稱,可以用於訪問該循環,例如:{$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: 表示當前循環的執行次數,初始爲1
   first: 循環第一次執行時被置爲true
   last: 同上
   total: 用於顯示循環執行的次數,在循環中或者循環後皆可使用
   show: 是foreach的一個標籤,用於決定是否顯示該foreach的內容
3. include / insert
   用途:包含其他模板 / 與include不同, insert 所包含的內容不會被緩存,每次調用該模板都會重新執行該函數.
   用法:{include file="footer.tpl" title="Main menu" logo="http://my.domain.com/logo.gif"}
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
   用途:在內部的數據當做文本處理,不使用smarty模板解析,主要用於javascript腳本等
   用法:
   {literal}
   ......
   {/literal}
7. section
   用途:循環處理
   用法:
   name: 該循環的名稱
   loop:決定循環次數的數組,注意這裏的使用方法比較特別,可以使用section來對多個數組進行處理,但必須先用可以決定循環次數的數組給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}
   如果要遍歷多維關聯數組,需要這樣來使用:
   {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: 顯示當前循環的索引,默認從0或者start開始
   index_prev: 顯示上一個循環索引值,默認從-1開始
   index_next: 同上,直至最後都比上一次大1
   ineration: 同foreach,與index不同,後者是索引
   first: 同foreach
   last: 同foreach
   show: 同foreach
   total: 同foreach


{assign var=i value=0}
{section name=total loop=$get_fields_count}
   <tr>
     <td>{$i+1}</td>
     <td>
       <input class="get_name" name="get_field_{$i}_name" id="get_field_{$i}_name" value="{$get_field_name[{$i}]}"/>
     </td>
     <td>
        <input class="get_value" name="get_field_{$i}_value" id="get_field_{$i}_value" value="{$get_field_value[{$i}]}"/>
     </td>
     </tr>
     {assign var=i value=$i+1}
{/section}

8. strip
   用途:將位於{strip}{/strip}內部html標籤外的所有空格和回車清除乾淨,並以html標籤開頭和結尾
   用法:
   {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="http://my.domain.com"><font color="red">This is a test</font></A></td></tr></table>
9.for

{section name=total loop=100}
{$smarty.section.total.index+1} //當前的索引
{$smarty.section.total.iteration} //用於顯示循環的次數
{/section}
{assign var=i value=0}
{section name=total loop=100}
{assign var=i value=$i+1} {$i} //使用assign的方式賦值
{/section}


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

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

五. smarty的method

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

六.使用php代碼段

<{php}>
$a = '123';
$this->assign("a", $a);
echo "hello world".$a;
<{/php}>
<{$a}>

七、php 標籤 訪問 smarty 模版變量

<{assign var="a" value="123"}>
<{php}>
var_dump($this->_tpl_vars['a']);
<{/php}>
<{$a}>

{$detail|@json_encode nofilter}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章