您好,登錄后才能下訂單哦!
這篇文章主要講解了“smarty模板引擎中變量及變量修飾器的實例應用”,文中的講解內容簡單清晰,易于學習與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學習“smarty模板引擎中變量及變量修飾器的實例應用”吧!
本文實例講述了smarty變量及變量修飾器的應用。分享給大家供大家參考。具體如下:
模板文件:temp.htm:
復制代碼 代碼如下:
{config_load file="foo.conf"}{$name.na1|cat:$name['na2']}{$name['na1']|cat:'與'|cat:$name.na2}{foreach from=$name item=na}{$na}{/foreach}{$dog->leee()}{$dog->name}<script>{literal}function foobar{ alert('foobar!');}{/literal}</script><title>{#pageTitle#}</title><body bgcolor="{#bodyBgColor#}"><table border="{#tableBorderSize#}" bgcolor="{#tableBgColor#}" > <tr bgcolor="{#rowBgColor#}"> <td>dosomething</td> <td>帥鍋</td> </tr></table>{$smarty.server.SERVER_NAME}<hr />{$str|count_words}<hr />-------常量--------<br>{$smarty.now}<br />{$smarty.const.MY_CONST}<br />{$smarty.template}<br />{$smarty.current_dir}<br />{$smarty.version}<br />{$smarty.ldelim|cat:$smarty.rdelim}<hr />{$smarty.now|date_format:$config}{$yesterday|date_format:'Y-m-d'}<hr />{$string|default:'default變量修飾:smarty學習'}<hr />{$str1|escape:'html'}<br />{$str2|escape:'mail'}<hr /><p>{$str1|indent|upper}</p>{$str1|nl2br}<hr />{$str1|regex_replace:"/@\d{3}/":"ABC"}<br />{$str1|replace:"163":"sina"}<br />{$str1|spacify}<br /><hr />{$number|string_format:"%.2f"}<br />{$number|string_format:"%d"}<br /><hr />{$str3|strip:"|"}<br />去除包含在<>之間的字符:{$str3|strip_tags}<br />截取長度:{$str3|truncate:10:"...":true}<br />按長度換行:{$str3|wordwrap:30:"<br />"}<hr />{append var='name' value="Bob" index="first"}{append var='name' value="John" index="last"}{$name.last}<br />{foreach from=$family item=home}{foreach from=$home item=person}{$person}{/foreach}{/foreach}{$family[1].girl}<hr />{assign var="name" value="張三豐"}{$name}</body> php文件:index.php 復制代碼 代碼如下: <?phprequire_once('libs/Smarty.class.php');$smarty = new Smarty();$smarty->setTemplateDir($_SERVER['DOCUMENT_ROOT']."/php/templates/");$smarty->setCompileDir($_SERVER['DOCUMENT_ROOT']."/php/templates_c/");$smarty->setCacheDir($_SERVER['DOCUMENT_ROOT']."/php/cache/");$smarty->caching = false;$arr = array("na1"=>"帥鍋","na2"=>"美女");$smarty->assign("name",$arr); class Dog{ public $name; public $age; function leee(){ return $this->name."在干嗎"; }}$dog = new Dog();$dog->name="小狗";$smarty->assign("dog",$dog); //$str = "hello world,i am here. i love smarty!";$str = "帥鍋";$str1 = "<a href='http://www.sina.com/'>新浪</a> And\n aassu@163.com";$str2 = "aassu@163.com";$smarty->assign("str",$str);$smarty->assign("str1",$str1);$smarty->assign("str2",$str2);$smarty->assign("number",30.293934);$smarty->assign("str3","akie abfal <a;fa>,dooerw,show databases,desc table"); $config = "Y-m-d H:i:s";$smarty->assign("config",$config);$smarty->assign("yesterday",strtotime('-1 day'));//常量define("MY_CONST","百度"); //append成員方法的使用$family = array("husband"=>"帥鍋","wife"=>"美女");$famiadd = array("boy"=>"張三豐","girl"=>"王昭君");$smarty->append("family",$family);$smarty->append("family",$famiadd);echo "<pre>";print_r($family);$smarty->display("temp.htm");?>
感謝各位的閱讀,以上就是“smarty模板引擎中變量及變量修飾器的實例應用”的內容了,經過本文的學習后,相信大家對smarty模板引擎中變量及變量修飾器的實例應用這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是億速云,小編將為大家推送更多相關知識點的文章,歡迎關注!
免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經查實,將立刻刪除涉嫌侵權內容。