smarty基础之插件

1.遵循原系统主程序,数据,函数库,接口等,改变其并不会影响整体。本质上是函数。

2.插件常用类型: functions 函数插件

                       modifiers 修饰插件(变量调节器插件)

                       block functions    区块函数插件

3.制作和使用插件:

    (1)使用registerPlugin 方法注册写好的插件。

    (2)将插件放入smarty解压目录中lib目录下的plugins目录里

    (3)php内置函数,可以直接以变量调节器的方式在模板中使用。

 

三种常用插件的定义与使用

functions函数插件:

    在lib/plugins 中定义文件function.test.php

        function smarty_function_test($params){     //smarty_function_插件名

              $width = $params['width'];                   //$params 接收 array(参数1=>参数值, 参数2=>参数值);

              $height = $params['height'];

              $area = $width*$height;

              return $area;

        }

  在模板文件中调用:

        {test width=200 height=100}

 

修饰插件(变量调节器插件)

    在lib/plugins 中定义文件modifier.test.php

        function smarty_modifier_test($utime, $format){     //smarty_modifer_插件名

             return date($format, $utime);

        }

    php文件中:$smarty->assign('time', time());

 

  在模板文件中调用:

        {time | test:"Y-m-d H:i:s"}

 

区块插件(block functions插件)

    在lib/plugins 中定义文件block.test.php

        function smarty_block_test($params, $content){     //smarty_block_插件名

             $replace = $params['replace'];

             $maxnum = $params['maxnum];

             if($replace == 'true'){

                   $content = str_replace(',' ,  ',' , $content);

                   $content = str_replace('。' , '.' , $content);

             }

            $content = sub_str($content, '0', $maxnum);

            return $content;

        }

    php文件中:$smarty->assign('str', "Hello,my name is LiLei。How are you?");

 

  在模板文件中调用:

         {test replace='true' maxnum=20}

             {$str}

         {/test}

 

posted @ 2015-08-25 14:33  残风一月  阅读(315)  评论(0编辑  收藏  举报