smarty模板引擎的变量修饰器

这个名称听起来邮电怪,简单的说就是smarty对字符串的操作,例如大小写转换、日期格式化、正则替换等。smarty上手不是很困难下面我整理了一些smarty的字符串操作的例子

<?php
require_once 'libs/Smarty.class.php';
header('Content-type: text/html; charset=UTF-8');
$smart=new Smarty();
$smart->left_delimiter="<{";
$smart->right_delimiter="}>";
//字符串连接
$smart->assign("str","hello");
//取得字符串长度
$smart->assign("str2","我是中国人");
//把字符串里的中文变为小写(如果要变为大写则使用upper)
$smart->assign("str3","MY NAME IS AMY");
//正则替换
$smart->assign("str4","我喜欢昆明5");
//字符串替换
$smart->assign("str5","我喜欢杭州");
$smart->display("strtest.tpl");
?>

下面是模板文件

<{$str|cat:' world' }></br>
字符串的长度是:<{$str2|count_characters}></br>
<{*日期格式化*}>
<{$smarty.now|date_format:"%Y-%m-%d %H:%M:%S"}></br>
<{$str3|lower:' world' }></br>
<{$str4|regex_replace:"/[\\d]/":""}> </br>
<{$str5|replace:"杭州":"上海"}> </br>
除这些外其实还有好些个方法,大家查阅文档看看

你可能感兴趣的:(smarty,变量修饰器)