(转~改)smarty里使用php函数
smarty:
<?php //引入Smarty类文件 include_once("Smarty/Smarty.class.php"); //实例化Smarty类 $smarty = new Smarty(); $smarty->caching=false; //定义模版目录 $smarty->template_dir = "./templates"; //定义编译目录 $smarty->compile_dir = "./templates_c"; //定义缓存目录 $smarty->cache_dir = "./smarty_cache"; //定义模版解析的标签 $smarty->left_delimiter = "{"; $smarty->right_delimiter = "}"; ?>
index.php:
<?php //引入smarty配置文件 require './config/Smarty_conf.php'; //模版变量赋值 $smarty->assign('stringword','helloworld'); $smarty->assign('title','this is a test page'); $smarty->assign('content','鐢ㄦ潵娴嬭瘯'); $smarty->assign('str','he says'); $smarty->assign('datetime','1353654721'); //打开对应模版 $smarty->display('index.html'); ?>
index.html:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title></title> </head> <body> <p>{$stringword|strlen}</p> <p>{$title|strpos:'is'}</p> <p>{'gb2312'|iconv:'utf-8':$content}</p> <p>{$str|str_pad:20:".":STR_PAD_LEFT}</p> <p>{$datetime|date_format:"%Y-%m-%d %H:%M:%S"}</p> </body> </html>
输出结果为:
10
2
用来测试
.............he says
2012-11-23 15:12:01
我用四个变量,分别处理1,2,3,4个不能的参数,模板中调用变量时,当只有一个参数是,就直接{$str1|函数名},当有函数有两个参数时,{第一个参数|函数名:第二个参数},当有三个参数时,{第一个参数|函数名:第二个参数:第三个参数},,当有(4,5........)参数时,以此类推.