Smarty的插件本质上就是function函数。
functions 函数插件
modifiers 修饰插件
block functions 区块函数插件
test.php文件:
<?php
require ('../smarty/Smarty.class.php');
$smarty = new Smarty();
//Smarty “自编口诀-五配置两方法”
//五配置介绍
$smarty -> left_delimiter = "{"; //左定界符
$smarty -> right_delimiter = "}"; //右定界符
$smarty -> template_dir = "tpl"; //html模板的地址
$smarty -> compile_dir = "template_c"; //模板编译生成的文件
$smarty -> cache_dir = "cache"; //缓存
$str = 'abcdefg';
$smarty->assign('str', $str);
function test($params){
$p1 = $params['p1'];
$p2 = $params['p2'];
$name = $params['name'];
return '传入的参数1值为:'.$p1.',传入的参数2值为:'.$p2 .'新加的'.$name;
}
//registerPlugin函数 registerPlugin() — 注册插件方法
$smarty->registerPlugin('function', 'test', 'test');
$smarty->display('test.tpl');
test.tpl文件:
{test p1='abc' p2='123' name='tom'}
将写好的插件放入Smarty解压目录中的lib目录下的plugins目录里:functions
lib目录下的plugins目录下function.test.php文件:
<?php
function smarty_function_test($params){
$width = $params['width'];
$height = $params['height'];
$area = $width * $height;
return $area;
}
test.php文件:
$smarty->display('area.tpl');
area.tpl文件:
{test width=150 height=200}
将写好的插件放入Smarty解压目录中的lib目录下的plugins目录里:modifier
<?php
function smarty_modifier_test($utime, $format){
return date($format, $utime);
}
test.php文件:
<?php
require ('../smarty/Smarty.class.php');
$smarty = new Smarty();
//Smarty “自编口诀-五配置两方法”
//五配置介绍
$smarty -> left_delimiter = "{"; //左定界符
$smarty -> right_delimiter = "}"; //右定界符
$smarty -> template_dir = "tpl"; //html模板的地址
$smarty -> compile_dir = "template_c"; //模板编译生成的文件
$smarty -> cache_dir = "cache"; //缓存
$smarty ->assign('time',time());
$smarty->display('datetime.tpl');
datetime文件:
{$time|test:'Y-m-d H:i:s'}
将写好的插件放入Smarty解压目录中的lib目录下的plugins目录里:block
<?php
function smarty_block_teststr($parmas, $content){
$replace = $parmas['replace'];
$maxnum = $parmas['maxnum'];
if($replace == 'true'){
$content = str_replace(',',',', $content);
$content = str_replace('。','.', $content);
}
$content = substr($content, 0 ,$maxnum);
return $content;
}
test.php文件:
require ('../smarty/Smarty.class.php');
$smarty = new Smarty();
//Smarty “自编口诀-五配置两方法”
//五配置介绍
$smarty -> left_delimiter = "{"; //左定界符
$smarty -> right_delimiter = "}"; //右定界符
$smarty -> template_dir = "tpl"; //html模板的地址
$smarty -> compile_dir = "template_c"; //模板编译生成的文件
$smarty -> cache_dir = "cache"; //缓存
$str = 'Hello,, my name is HanMeimei。 How are you?';
$smarty->assign('str',$str);
$smarty->display('content.tpl');
content文件:
{teststr replace='true' maxnum=29}
{$str}
{/teststr}
本文链接:https://cloud.tencent.com/developer/article/1558018
本文采用CC BY-NC-SA 3.0 Unported协议进行许可,转载请保留此文章链接