8. Smarty 基本语法

来源:互联网 发布:久其软件 决算 编辑:程序博客网 时间:2024/06/04 23:48

这里写图片描述
这里写图片描述
这里写图片描述


2. Smarty 调节器(变量操作符)
这里写图片描述
这里写图片描述
这里写图片描述


3.自定义变量调节器(以插件形式)
这里写图片描述
这里写图片描述
这里写图片描述


TestController4.php

<?phprequire_once './libs/Smarty.class.php';$smarty = new Smarty();$smarty->left_delimiter = "{<";$smarty->right_delimiter = ">}";$smarty->assign("sal",'100');$smarty->assign("tax_rate",'0.08');//定义常量define('TAX_RATE','0.08');$smarty->assign("str1",'hello world');$smarty->assign("str2",'hello world\\n shanghai');$smarty->assign("str4",'hello world shanghai 上海');$smarty->assign("bb",'<a href="##">go to 搜狐</a>');$smarty->assign("article",file_get_contents('aa.txt'));$smarty->assign("cc",'1234abcdef9999ae ew e ew 2222wrwer3333');$smarty->assign("dd",'hello    world aaa   iii   ');$smarty->assign("ee",'hello上海人 world aaa   iii   ');$smarty->assign("hh",'hello上海人 world aaa   iii   hello上海人 world aaa   iii   hello上海人 world aaa   iii   hello上海人 world aaa   iii   hello上海人 world aaa   iii   hello上海人 world aaa   iii   hello上海人 world aaa   iii   hello上海人 world aaa   iii   hello上海人 world aaa   iii   hello上海人 world aaa   iii   hello上海人 world aaa   iii   hello上海人 world aaa   iii   hello上海人 world aaa   iii   hello上海人 world aaa   iii   ');$smarty->display('test4.tpl');

test4.tpl

<h1> smarty 语法细节</h1><h2>个人所得税</h2>{<$sal*0.08+2-5>}<br/>{<$sal*$tax_rate>}<br/><h2>如何取出常量</h2>{<$smarty.const.TAX_RATE>}<br/>{<$smarty.const.TAX_RATE+100>}<h2>首字母大写</h2>{<$str1|capitalize:false>}<h2>自定义调节器</h2>{<"abcd"|mycap:'hello'>}<h2>统计字符</h2>{<$str1|count_characters>}<h2>统计段落数</h2>{<$str2|count_paragraphs>}<h2>统计单词数</h2>{<$str4|count_words>}<h2>显示当前时间</h2>{<$smarty.now|date_format:"%Y-%m-%d %H-%I-%S %a">}<h2>default使用</h2>{*如果这个变量有,就输出,没有就是 default的值*}{<$aa|default:"没有值">}<h2>escape</h2>{*默认形式*}{<$bb>}<br/>{*源码输出*}{<$bb|escape>}<h2>输出文件内容</h2>{*将文件的换行\r\n,替换成网页的< br/>*}{<$article|nl2br>}<h2>正则表达式的使用</h2>{<$cc|regex_replace:"/(\d\d\d\d)/":"这里有4个数">}<h2>spacify</h2>{<"abc"|spacify>}<br/>{<"abc"|spacify:"^_^">}<h2>去掉空格  strip</h2>{<$dd|strip:"||">}<h2>截取 truncate</h2>默认是 ... ,而且这3个点也要占位置<br/>{<$ee|truncate:9:"...":true>}<br/><a href="##" title='{<$ee>}'>{<$ee|truncate:9:"...":true>}</a><h2>行宽的约束</h2>{<$hh|wordwrap:50:"<br/>">}

这里写图片描述

<h2>测试 ismail</h2>{<"wj@sohu.com"|ismail>}
<?phpfunction smarty_modifier_ismail($str){    $pattern = '/^[a-zA-Z][a-zA-Z0-9_-]*@[a-zA-Z0-9]+(\.[a-zA-Z0-9]+)+$/i';    if(preg_match($pattern,$str)==1){        return '是一个合法的邮件';    }else{        return '不是一个合法的邮件';    }}

$smarty->assign("kk",'aaaaaaaaaabbbbbbbbbbbcccccccccccccccc');
<h2>组合使用</h2>{<$kk|upper|truncate:8>}
0 0
原创粉丝点击