引用文件路徑,路徑必需正確。 eg:include_once("smarty/Smarty.class.php");
2 $smarty=new Smarty();新建一個對象smarty, 實例化一個對象。
3 $smarty->template_dir=“”;指定$smarty對象使用的tpl模板的路徑,它是一個目錄,默認目錄爲當前的templates的目錄,實際編程中,可能要指定目錄。
4 $smarty->compile_dir=””;指定$smarty對象的編譯時的目錄,就是smarty編譯模板的目錄,linux服務器,請你確認有可寫可讀權限。通常chmod -R 777 filename 修改權限,默認情況下它編譯目錄是當前的目錄下的templates_c。
5 $smarty->left_delimiter 與 $smarty->right_delimiter;查找模板變量左右的分割符,默認情況下爲{ } 爲了與script中括號相區別,通常寫爲<{ }>.
6 $tp1->cache_dir=”./”;模板文件緩存的位置,Smarty最大的優點在於可以緩存,這裏設置緩存的目錄,默認情況下當前目錄下的cache目錄,同上,linux確保它的可讀可寫性。
7 $smarty->cache-lifetime=60*60*24;這裏以秒爲單位計算緩存有效的時間,第一次緩存時間到期時Smarty的caching變量設置爲true時緩存將被重建。-1表示建立緩存從不過期,爲0時表示每次程度執行時緩存被重新建立,上述一天。
8 $smarty->catching=true;緩 存方式三個狀態。0:Smarty'默認值,表示不對模板進行緩存。1:Smarty使用cache_lifetime來決定是否結束。2:表示 Smarty將使用cache被建立時使用cache_lifetime這個值,習慣上用true和false來表示是否進行緩存。
Smarty常用語法
1.foreach : 循環簡單數組,它是一個選擇性的section循環.
form指定循環的數組變量,item循環變量的名稱,循環次數由from指定數組變量的個數決定。
數組爲空,執行<{foreachelse}>語句.格式如下:
<{foreach from=$newsArray item=newID}>
The Id:<{$newsID.newsID}>
The Content:<{$newsID.newsTitle}>
<{foreachelse}>
No content to put
<{/foreach}>
2.section: 用於設計模板內的循環塊。可完成foreach語句所完成所有的功能。Section的格式:
<{section loop= $varName[,start=$start,step=$setp,max=$max,$show=true]}>
name: section的名稱,不用加$;
$loop: 要循環的變量,程度中要使用assign對這個變量進行操作。
$start: 開始循環的下標。默認爲0;
$step: 每次循環下標的增數;
$show : boolean型。決定是否對於這塊進行顯示。默認爲true;
<{section}>的屬性;
index:循環下標。默認爲0;
index_prev:當前下標的上一個值,默認爲-1;
index_next:當前下標的下一個值,默認爲1;
first:是否爲第一下循環;
last:是否爲最後一個循環;
iteration:循環個數;
rownum:當前行號,iteration的別名;
loop:最後一個循環號。Section的循環次數;
show:是否顯示;
<{section loop=$News}>
新聞編號:<{$News[loop].newID}><br>
新聞內容:<{$News[loop].newTitle}><br>
<{sectionelse}>
I am sorry
<{/section}>
對於一維數組:
{section name=row loop=$list}
{$list.name}
{/section}
對於二維數組:
{section name=row loop=$list}
{$list[row].name}
{/section}
另外還可以直接單一輸出:
< { $commendList.0.infoType } >
< { $commendList.1.infoType } >
對於三維數組:
<{section name=cate loop=$myarray}>
<{section name=scate loop=$myarray[cate]}>
id ------> <{$myarray[cate][scate].id}>
title --------> <{$myarray[cate][scate].title}>
url ----------> <{$myarray[cate][scate].url}>
<{/section}>
<{/section}>
3.if用法:
{if $list[row].name eq “1"}
星期1
{elseif $list[row].name==“2"}
星期2
{else}
默認
{/if}
4.{ include file=a.template}
<{include file=uhead.html}>
5.Literal
標籤區域內的數據將被當作文本處理,此時模板將忽略其
內部的所有字符信息. 該特性用於顯示有可能包含大括號等字符信
息的 javascript 腳本.
<literal>
<script>
alert('js');
</script>
<literal>
6.assign
用於在模板被執行時爲模板變量賦值.
{assign var="name" value="Bob"}
$smarty->assign("newsArray", $array);
//編譯並顯示位於./templates下的index.tpl模板
$smarty->display("example6.tpl");
Smarty常用函數
count_characters 計算變量裏的字符數 {$articleTitle|count_characters}
cat 將cat裏的值連接到給定的變量後面. {$articleTitle|cat:" yesterday."}
count_paragraphs計算變量裏的段落數量。 {$articleTitle|count_paragraphs}
count_sentences 計算變量裏句子的數量。{$articleTitle|count_sentences}
count_words 計算變量裏的詞數 。{$articleTitle|count_words}
date_format格式化從函數strftime()獲得的時間和日期。 {$yesterday|date_format:"%H:%M:%S"}
default 爲空變量設置一個默認值{$articleTitle|default:"no title"}
escape 用於html等轉碼
index.tpl:
{$articleTitle}
{$articleTitle|escape}
{$articleTitle|escape:"html"} {* escapes & " ' < > *}
{$articleTitle|escape:"htmlall"} {* escapes ALL html entities *}
{$articleTitle|escape:"url"}
{$articleTitle|escape:"quotes"}
<a href="http://z040145.blog.163.com/blog/mailto:{$EmailAddress|escape:"hex"}">{$EmailAddress|escape:"hexentity"}</a>
OUTPUT:
'Stiff Opposition Expected to Casketless Funeral Plan'
'Stiff Opposition Expected to Casketless Funeral Plan'
'Stiff Opposition Expected to Casketless Funeral Plan'
'Stiff Opposition Expected to Casketless Funeral Plan'
%27Stiff+Opposition+Expected+to+Casketless+Funeral+Plan%27
\'Stiff Opposition Expected to Casketless Funeral Plan\'
<a href="http://z040145.blog.163.com/blog/mailto:%62%6f%62%40%6d%65%2e%6e%65%74">bob@me.net</a>
indent 在每行縮進字符串,默認是4個字符{$articleTitle|indent:1:"\t"}
lower 小寫{$articleTitle|lower}
nl2br
regex_replace {$articleTitle|regex_replace:"/[\r\t\n]/":" "}
replace {$articleTitle|replace:"被替換":"Vineyard"}
spacify 每個字符間插入字符{$articleTitle|spacify:"^^"}
string_format格式化字符串 {$number|string_format:"%.2f"}
strip 用一個空格或一個給定字符替換所有重複空格,換行和製表符. {$articleTitle|strip:" "}
strip_tags 去除<和>標籤,包括在<和>之間的任何內容.{$articleTitle|strip_tags}
truncate
upper 大寫
wordwrap以指定段落的寬度,默認80.第二個參數,可以指定在約束點使用什麼字符(默認是換行符\n).{$articleTitle|wordwrap:30:"\n":true}
html_options
<select name="media_id" id="media_id">
<{html_options options=$media_id selected=$ap.media_id}>
</select>
html_radios
<{html_radios name="is_valid" options=$is_valid checked=$ap.is_valid separator=" "}>