smary檔案配置步驟
//步驟1
require '/libs/Smarty.class.php';
//步驟2
$smarty = new Smarty;
//步驟3
/*
$smarty->template_dir = "";
$smarty->compile_dir = "";
$smarty->cache_dir = "";
$smarty->caching = 1;
$smarty->cache_lifetime = 60*60;//秒,這裡的值是一個小時
$smarty->left_delimiter ='{#';
$smarty->right_delimiter = '#}';
$smarty->compile_check = true;
$smarty->debugging = true;
*/
//步驟4
$smarty->assign("demo1", "fengsulei");
$smarty->assign("demo2", "cool is working");
$smarty->assign(array("demo3"=>"string1","demo4"=>"smarty is worked"));
//assign 賦值,有兩種形式
//$smarty->assign("demo1", "fengsulei");
//$smarty->assign("demo1", $str);
//$smarty->assign(array("city"=>"string1","state"=>"string2"));
$smarty->assign("p1","kloyu");
$farray=array("a","b","c","d","e","ss"=>99);
$smarty->assign("farray",$farray);
//步驟5
$smarty->display('index.tpl');
//顯示有三種方式
//使用檔案$smarty->display('index.tpl')
//使用路徑$smarty->display('admin/index.tpl')
//使用絕對路徑$smarty->display('www/templates/index.tpl'),$smarty->display('file:D:/wamp/www/templates/index.tpl')
模板程式碼範例
<h1>{#$demo1#}</h1>
<b>{#$demo2#}</b>
<A HREF="#">{#$demo3#}</A>
<A HREF="#">{#$demo4#}</A>
{#$p1|upper|truncate:4#}
get
{#$smarty.get.att#}
post
{#$smarty.post.att#}
cookies
{#$smarty.cookies.att#}
session
{#$smarty.session.att#}
request
{#$smarty.request.att#}
server_name
{#$smarty.server.SERVER_NAME#}
env.path
{#$smarty.env.PATH#}
<p class="STYLE1">foreach exp1
</p>
<p>{#foreach from=$farray item=row#}
{#$row#}<br />
{#foreachelse#}
$farray is empty
{#/foreach#}</p>
<p class="STYLE1"> foreach exp2</p>
<p>{#foreach key=kk item=vv from=$farray #}
{#$kk#}-{#$vv#}<br />
{#foreachelse#}
$farray is empty
{#/foreach#}
</p>
<p>{#section name=line loop=$farray #}
{#$farray[line]#}<br />
{#if $smarty.section.line.iteration==5#}
{#$farray.ss#}<br />
{#/if#}
{#sectionelse#}
$farray is empty
{#/section#} </p>
設定檔執行個體
#global variable
page_titile="xxxxxx"
body_color="#333"
table_bgcolor="#666"
[index]
index_page_titile=""
index_body_color=""
index_table_bgcolor=""
[list]
list_page_titile=""
list_body_color=""
list_table_bgcolor=""
[view]
view_page_titile=""
view_body_color=""
view_table_bgcolor=""
載入設定檔
{config_load file="head.conf" section|scope|global=""}
config_load 參數
|
|
類型 |
預設值 |
section |
載入設定檔特定的一節 |
string |
無 |
scope |
載入資料的範圍,取值是local,parent,global。local指定變數範圍是當前模板,parent指定變數範圍是當前模板和當前模板的父模板 |
string |
local |
global |
載入變數是否全域可見 |
bool |
false |
程式碼範例
{#config_load file="head.conf"#}
引用設定檔程式碼範例
1,{#$smarty.config.page_titile#}
2,{##page_titile##}
模板包含
{include file="模板檔案名稱" assign="指定傳遞給的變數名(變數名不加$符號)"}
常用變數
capitalize |
所有字元首字元大寫 |
count_characters |
計算字元個數 |
cat |
將cat裡的參數值串連到給定變數後 |
count_paragraphs |
計算段落數量 |
count_sentences |
計算句子數量 |
count_words |
計算單詞數量 |
date_format |
時間格式化 |
default |
給空變數設定一個預設值 |
escape |
用於html轉碼 |
indent |
每行縮排字元個數 |
lower |
變數小寫 |
nl2br |
分行符號轉換成<br /> |
regex_replace |
使用正則替換 |
replace |
替換和搜尋 |
spacify |
在字元之間插入字元,預設插入空格 |
string_format |
|
strip |
替換所有重複空格,換行,tab為指定字元 |
strip_tags |
去除html標籤 |
truncate |
截取字串 |
upper |
變數大寫 |
wordwrap |
指定段落寬度,預設80個字元 |
foreach迴圈參數和設定
foreach
form |
使用數組 |
array |
|
item |
當前處理元素的值 |
string |
|
key |
當前處理元素的鍵名 |
string |
|
name |
迴圈的名稱 |
string |
|
|
|
|
|
foreach 可調用變數
iteration |
迴圈當前執行次數 |
first |
是否是迴圈第一次執行,是則返回true |
last |
是否是迴圈最後一次執行,是則返回true |
show |
|
total |
迴圈執行總數 |
section迴圈參數和變數
section
name |
指定迴圈變數名 |
|
|
loop |
使用數組名 |
|
|
start |
從第幾次開始 |
|
|
setp |
每次增加值 |
|
|
max |
最多迴圈次數 |
|
|
show |
是否顯示該迴圈值為bool型 |
|
|
section 可調用的變數
index |
顯示當前索引,如果指定了start則從該設定值開始每次加1,如果設定了setp則按設定值增加 |
index_prev |
顯示上次迴圈的索引值,首次值為-1 |
index_next |
顯示下一次迴圈索引值 |
iteration |
顯示迴圈次數 |
first |
判斷是否是迴圈執行的首次,是則返回true |
last |
判斷是否是迴圈執行的最後一次,是則返回true |
rownum |
同iteration,互為別名 |
loop |
顯示該迴圈上次的索引 |
show |
|
total |
顯示迴圈執行總次數 |