标签:
<?php
require_once ‘libs/Smarty.class.php‘;
$smarty=new Smarty;
$smarty->left_delimiter="<{";
$smarty->right_delimiter="}>";
$smarty->assign("a","hello world");
$smarty->assign("b","smarty");
$smarty->assign("c","hello");
$smarty->assign("d","hello \n world");
$smarty->assign("e","hello. world.");
$smarty->assign("f","hello world hello world.");
$smarty->assign(‘yesterday‘,strtotime(‘0 day‘)); //可以定义
$smarty->assign("title","ni hao");
$smarty->assign("dz","<a href=‘http://www.baidu.com‘>百度</a>");
$smarty->assign("con","ni\nhao");
$smarty->assign("h","hello\nworld");
$smarty->display("index.html");
?>
模版文件index.html:
<head>
<title>Smarty模版的变量调节器实例</title>
</head>
<h2>-----capitalize(首字母大写)---------</h2>
<{$a|capitalize}><br>
<h2>-----count_characters(字符计数)----</h2>
<{$b}>=<{$b|count_characters}>个字符
<h2>------cat(连接字符串)--------</h2>
<{$c|cat:world}>
<h2>-----count_paragraphs(计算段数)----(换行符\n)----</h2>
<{$d}><br>
<{$d|count_paragraphs}>
<h2>-------count_sentences(计算句数)------------</h2>
<{$e}><br>
<{$e|count_sentences}>
<h2>----count_words(计算词数)--------</h2>
<{$f}><br>
<{$f|count_words}>
<h2>------date_format(格式化日期)-----(区分大小写)--</h2>
<{$smarty.now|date_format}><br>
<{$smarty.now|date_format:"%y-%m-%d %H:%I:%S"}><br>
<{$yesterday|date_format}><br>
<{$yesterday|date_format:"%y-%m-%d %H:%I:%S"}><br>
<h2>-----default(默认值)-------------</h2>
<{$title|default:"no title"}><br>
<{$biaoti|default:"no biaoti"}>
<h2>------escape(编码)------</h2>
<{$dz}><br>
<{$dz|escape}>
<h2>------indent(首行缩进)------</h2>
<{$in}><br>
<{$in|indent}><br>
<{$in|indent:1:"\t"}>
<h2>------nl2br换行符替换成<br />--------</h2>
<{$con}><br>
<{$con|nl2br}>
<h2>-----------regex_replace 正则替换------ </h2>
<{$h}><br>
<{$h|regex_replace:"/[\r\t\n]/":" <br/> "}>
<h2>------replace(替换)------</h2>
<{$h|replace:"hello":"你好"}>
</html>
往往开发中有时候是满足不了需求的,那就可以直接写这些变量调节器,其实所谓的变量调节器就是一个函数,在这里我们自己建一个,首先在plugins文件夹里面间建立一个php文件,命名一定按着他的规范,。
modifier.capita.php
function Smarty_modifier_capita($string){ //函数命名也是有规范的
return strtoupper(substr($string,0,1)).strtolower(substr($string,1));
}
模版文件调用:
<{$a|capita}>
标签:
原文地址:http://www.cnblogs.com/mfc-itblog/p/5754975.html