Smarty模板常見(jiàn)的簡(jiǎn)單應(yīng)用分析
本文實(shí)例分析了Smarty模板常見(jiàn)的簡(jiǎn)單應(yīng)用。分享給大家供大家參考,具體如下:
首先要將smarty這個(gè)類(lèi)包含進(jìn)來(lái):
include_once '../libs/Smarty.class.php';
然后創(chuàng)一個(gè)Smarty對(duì)象:
$smarty = new Smarty;
可以自定義Smarty 的開(kāi)始結(jié)束符,默認(rèn)為{ }
$smarty->left_delimiter ='<<'; //左符號(hào)為 << $smarty->right_delimiter='>>'; //右符號(hào) 為 >>
最重要方法的好像是assign,如:
$smarty->assign('test',$te); //將$test的值付給test,在模板頁(yè)tpl中顯示用{$test}
又如$arr=array(1,2,3);賦值仍是這樣:
$smarty->assign('arr',$arr);
但在模板頁(yè)顯示時(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開(kāi)始,index從0開(kāi)始 {$arr[test]} {/section}
最后最重要的一步操作千萬(wàn)不要忘記那就是:
$smarty->display('test.tpl');
下面說(shuō)一些常用東西怎么樣的在模板上顯示
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.大小寫(xiě):
{$str4|lower} //$str4的小寫(xiě)形式 {$str4|upper} //$str4的大寫(xiě)形式
過(guò)濾:
{$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相關(guān)內(nèi)容感興趣的讀者可查看本站專(zhuān)題:《smarty模板入門(mén)基礎(chǔ)教程》、《PHP模板技術(shù)總結(jié)》、《PHP基于pdo操作數(shù)據(jù)庫(kù)技巧總結(jié)》、《PHP運(yùn)算與運(yùn)算符用法總結(jié)》、《PHP網(wǎng)絡(luò)編程技巧總結(jié)》、《PHP基本語(yǔ)法入門(mén)教程》、《php面向?qū)ο蟪绦蛟O(shè)計(jì)入門(mén)教程》、《php字符串(string)用法總結(jié)》、《php+mysql數(shù)據(jù)庫(kù)操作入門(mén)教程》及《php常見(jiàn)數(shù)據(jù)庫(kù)操作技巧匯總》
希望本文所述對(duì)大家基于smarty模板的PHP程序設(shè)計(jì)有所幫助。
相關(guān)文章
PHP類(lèi)中的魔術(shù)方法(Magic Method)簡(jiǎn)明總結(jié)
這篇文章主要介紹了PHP類(lèi)中的魔術(shù)方法(Magic Method)簡(jiǎn)明總結(jié),這些方法包括__construct()、__destruct()、__call()、__callStatic()、__get()、__set()、__toString()等,需要的朋友可以參考下2014-07-07destoon會(huì)員注冊(cè)提示“數(shù)據(jù)校驗(yàn)失?。?)”解決方法
這篇文章主要介紹了destoon會(huì)員注冊(cè)提示“數(shù)據(jù)校驗(yàn)失?。?)”解決方法,需要的朋友可以參考下2014-06-06WordPress后臺(tái)中實(shí)現(xiàn)圖片上傳功能的實(shí)例講解
這篇文章主要介紹了WordPress后臺(tái)中實(shí)現(xiàn)圖片上傳功能的實(shí)例講解,包括多個(gè)圖片上傳表單功能的實(shí)現(xiàn),需要的朋友可以參考下2016-01-01

基于Swoole實(shí)現(xiàn)PHP與websocket聊天室

eWebEditor v3.8 商業(yè)完整版 (PHP)

PHP中使用substr()截取字符串出現(xiàn)中文亂碼問(wèn)題該怎么辦

Laravel5.4框架使用socialite實(shí)現(xiàn)github登錄的方法

php解析xml 的四種簡(jiǎn)單方法(附實(shí)例)