温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

smarty模板引擎中变量及变量修饰器的实例应用

发布时间:2021-09-06 17:17:29 来源:亿速云 阅读:93 作者:chen 栏目:开发技术

这篇文章主要讲解了“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模板引擎中变量及变量修饰器的实例应用这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是亿速云,小编将为大家推送更多相关知识点的文章,欢迎关注!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI