zoukankan      html  css  js  c++  java
  • 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}

  • 相关阅读:
    C# 之 HttpRequest 类
    C# 之 日常问题积累(一)
    DataGrid前台数据绑定技巧
    [转]C,C++开源项目中的100个Bugs
    10行Python代码解决约瑟夫环(模拟)
    基于ASP.NET的comet简单实现 http长连接,IAsyncResult
    架构设计分享之权限系统(看图说话)
    内核request_mem_region 和 ioremap的理解
    【调侃】IOC前世今生 工厂模式 反射 依赖倒置
    ecos内核概览--bakayi译
  • 原文地址:https://www.cnblogs.com/canfengyiyue/p/4756989.html
Copyright © 2011-2022 走看看