這篇文章給大家介紹Smarty模板常見的簡單應(yīng)用有哪些,內(nèi)容非常詳細(xì),感興趣的小伙伴們可以參考借鑒,希望對(duì)大家能有所幫助。
首先要將smarty這個(gè)類包含進(jìn)來:
include_once '../libs/Smarty.class.php';
然后創(chuàng)一個(gè)Smarty對(duì)象:
$smarty = new Smarty;
可以自定義Smarty 的開始結(jié)束符,默認(rèn)為{ }
$smarty->left_delimiter ='<<'; //左符號(hào)為 << $smarty->right_delimiter='>>'; //右符號(hào) 為 >>
最重要方法的好像是assign,如:
$smarty->assign('test',$te); //將$test的值付給test,在模板頁tpl中顯示用{$test}
又如$arr=array(1,2,3);賦值仍是這樣:
$smarty->assign('arr',$arr);
但在模板頁顯示時(shí)要借助foreach 或 section,foreach 用法如下:
{foreach item=item from=$arr key=ke name=foe} $item {/foreach} //此處的$item相當(dāng)于$arr[$ke],foreach序列化{$smarty.foreach.foe.iteration}
而section 用法如下:
{section name='test' loop=$arr} {$smarty.section.name.iteration}//使輸出序列化,序號(hào)從1開始,index從0開始 {$arr[test]} {/section}
最后最重要的一步操作千萬不要忘記那就是:
$smarty->display('test.tpl');
下面說一些常用東西怎么樣的在模板上顯示
1.連接操作:
我叫{$str1|cat:"李白"};//輸出結(jié)果就是:我叫 $str1 李白
2.當(dāng)前日期:
{$str2|rdate_format:"Y%-m%-d%"} //輸出結(jié)果格式化$str2日期,形如0000-00-00
3.縮進(jìn):
{$str3|indent:8:"*"} //$str3前縮進(jìn)8個(gè)* 默認(rèn)縮進(jìn)的是空格
4.大小寫:
{$str4|lower} //$str4的小寫形式 {$str4|upper} //$str4的大寫形式
過濾:
{$url|escape:"url"} //對(duì)$url相關(guān)特殊字符進(jìn)行替換 <tr bgcolor='{cycle values="#EBEBEB,#ACABAB"}'>//tr背景交替 顏色分別為#EBEBEB,#ACABAB
匹配替換:
{$str|regex_replace:"~[0-9]~":"asd"} //如果$str匹配[0-9]輸出asd
替換
{$str|replace:"net":"com"} //將$str中的net全部替換成com
包含頭模板文件:
{include file="top.tpl"}
調(diào)用time.inc.php里面的函數(shù):
{insert name="getCurrentTime" assign="current_time" script="time.inc.php"} 當(dāng)前時(shí)間為{$current_time}; {/insert}
其中time.inc.php內(nèi)容如下:
<?php function smarty_insert_getCurrentTime { return gmdate('l,j F Y g:i a T');//獲得當(dāng)前日期及時(shí)間 } ?>
聯(lián)系:
mailto{ mailto address="contact@smartyllc.com" subject="Smarty LLC Contact" encode="javascript"}
載入test.conf:
{conf_load file="test.conf" section="test"} {#tt#}
test.conf內(nèi)容如下:
[test] tt = 12122
關(guān)于Smarty模板常見的簡單應(yīng)用有哪些就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到。
網(wǎng)站名稱:Smarty模板常見的簡單應(yīng)用有哪些-創(chuàng)新互聯(lián)
本文來源:http://www.rwnh.cn/article28/ccjijp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供做網(wǎng)站、網(wǎng)站導(dǎo)航、外貿(mào)建站、電子商務(wù)、微信公眾號(hào)、外貿(mào)網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容