smarty模板引擎中变量及变量修饰器用法实例
本文实例讲述了smarty变量及变量修饰器的应用。分享给大家供大家参考。具体如下
模板文件temp.htm
{$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
require_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://.sina./'>新浪</a> And\n aassu@163.";
$str2 = "aassu@163.";
$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");
?>
希望本文所述对大家的php程序设计有所帮助。
编程语言
- 宿迁百度关键词排名指南:实现精准营销的关键
- 四川SEO优化怎么做网络推广
- 立昂技术备案老域名收购:如何为您的业务赋能
- 安徽百度关键词seo贵不贵,一般需要多少钱
- 吉林百度快照排名怎么做电话营销
- 多伦新手做SEO怎么做
- 甘肃优化关键词排名推广怎么做论坛营销
- 沙雅SEO网站推广:提升您的在线可见性
- 四川SEO优化如何提升销售额和销售量
- 聂荣网站排名优化:提升网站可见性的全方位指
- 涞水SEO:提升地方企业在线可见性的策略
- 辽宁百度seo排名怎样做网站排名
- 临湘哪有关键词排名优化:提升网站可见度的关
- 黑龙江百度网站优化有没有优惠
- 凉城优化关键词排名推广:提升您的网络可见性
- 萝北整站优化:提升您网站流量和排名的全面指