您的位置:首页 > 其它

smarty模板引擎_5-自定义变量调节器

2016-06-16 22:35 267 查看
自定义变量调节器

 两种方式:

第一种通过registerPlugin()方法将定义的函数注册到Smarty对象中,只能在当前模版中使用

[php] view
plain copy

function getExt($filename){  

    return strtolower(pathinfo($filename,PATHINFO_EXTENSION));  

 }  

$smarty->registerPlugin('modifier','ext','getExt');  

modifier:注册为变量调节器

ext:变量调节器的名称

getExt:调用的函数取得文件后缀名

使用该变量调节器

[php] view
plain copy

<{$filename|ext}>  

前面的$filename即为传入getExt()的第一个参数

[php] view
plain copy

function myFunc($content,$size,$color,$family,$weight){  

    $html = '<span style="font-size:'.$size.';color:'.$color.';font-family:'.$family.';font-weight:'.$weight.'">'.$content.'</span>';  

    return $html;  

 }  

$smarty->registerPlugin('modifier','font','myFunc');  

使用该变量调节器

[php] view
plain copy

<{$string4|font:'20px':'green':'Microsoft Yahei':'normal'}>  

也可以直接将PHP系统函数注册成变量调节器插件

[php] view
plain copy

$smarty->registerPlugin('modifier', 'strShuffle', 'str_shuffle');  

str_shuffle()为PHP系统自带的函数

第二种方式可以像系统默认的变量调节器一样,在Smarty库文件所在的插件目录plugins中定义一个文件扩展插件,无需注册即可使用。

插件命名规则:modifier.变量调节器名称.php

插件内方法命名规则:smarty_modifier_变量调节器名称(形参...){}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: