溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Smarty模板常見的簡單應(yīng)用有哪些

發(fā)布時(shí)間:2021-09-16 14:23:01 來源:億速云 閱讀:127 作者:柒染 欄目:開發(fā)技術(shù)

這篇文章給大家介紹Smarty模板常見的簡單應(yīng)用有哪些,內(nèi)容非常詳細(xì),感興趣的小伙伴們可以參考借鑒,希望對大家能有所幫助。

首先要將smarty這個(gè)類包含進(jìn)來:

include_once '../libs/Smarty.class.php';

然后創(chuàng)一個(gè)Smarty對象:

$smarty = new Smarty;

可以自定義Smarty 的開始結(jié)束符,默認(rèn)為{ }

$smarty->left_delimiter ='<<'; //左符號為 <<
$smarty->right_delimiter='>>'; //右符號 為 >>

最重要方法的好像是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}//使輸出序列化,序號從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"} //對$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)容可以對大家有一定的幫助,可以學(xué)到更多知識。如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI