• 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}

  • 相关阅读:
    python高阶函数——返回函数(闭包)
    python高阶函数——sorted排序算法
    python高阶函数—filter
    python高阶函数——map/reduce
    Python——高阶函数概念(Higher-order function)
    Python高级特性——迭代器
    Python高级特性——生成器(generator)
    Python高级特性——列表生成式(list Comprehensions)
    Python高级特性——迭代(Iteration)
    Python高级特性——切片(Slice)
  • 原文地址:https://www.cnblogs.com/canfengyiyue/p/4756989.html
Copyright © 2020-2023  润新知