Smarty中用户自定义插件

用户自定义插件:

    {ts:变量|函数[:参数]}:修饰函数插件(modify)

    {ts:插件名 属性=值 属性=值...}  函数类型插件(function)

    {ts:插件名 属性=值 属性=值...}  block类型插件

    {ts:/插件名}  #插件闭合的结尾一定不可以漏#

  Libs目录详解:

    Plugins: 用户自定义插件目录

    Sysplugins:系统插件目录

  修饰函数插件:

    libs/plugins/创建固定格式文件:

      Modifier.插件名.php

  编写规定规范的函数:

    最后结果数据使用return返回。

    function smarty_modifier_插件名($string,参数1,参数2...){}

    $string:指的是被修饰的变量

    参数1:参数1 作为函数调用的第一个参数,也可以只有一个参数

  插件使用:

    {ts:变量名|插件名:参数1,参数2....}

    =>smarty_modifier_插件名(变量名,参数1,参数2....)

  【modifier.strcon.php】  #文件必须放在/libs/plugins/下#

<?php
 function smarty_modifier_strcon($string,$string2){
     $s = $string.$string2;
     return $s;
 }
?>

 

block类型的插件:

    libs/plugins/创建文件

    Block.插件名.php

  block插件函数定义:

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

      if (is_null($content))  return;

        Return 返回内容

      }

    $params:以属性当键名,以属性值作为数组元素组成的一维关联数组。

    $content: 被修饰的内容

    $template:smarty对象

  调用插件:

    {ts:插件名  属性=值 属性=值...}

    {ts:/插件名}

  【block.hotlink.php】 #文件必须放在/libs/plugins/下#

 

<?php
// 热点关键词  php=>http://www.php.com  hotlink插件
function smarty_block_hotlink($params,$content,$template){
    foreach($params as $key=>$vo){
        $link = "<a href='{$vo}'>".strtoupper($key)."</a>";
        $content = str_replace($key,$link,$content);
    }
    return $content;
}
?>

 

  【block.php】

<?php
require_once("./smarty.inc.php");
$tpl -> display("block.html");
?>

  【block.html】

{ts:hotlink asp="http://www.baidu.com" php="http://www.php.com" Shuo_128="http://www.cnblogs.com/shuo-128/"}
大家好,我是Shuo_128,点这里跳转到asp,点这里跳转到php
{ts:/hotlink}
<hr/>
{ts:/hotlink}

#在block.php中获取前台block.html中的值#

 

 

function类型插件:

    用于网站js效果,数据展示,内容展示等

    {ts:插件名 属性=值 属性=值...}  函数类型插件(function)

  libs/plugins/文件定义

    Function.插件名.php

  函数定义格式:

    Function smarty_function_插件名($params,$template){

      $params: 属性名当键名,属性值当数组元素值一维关联

      $template:smarty对象

      Return 数据:如果没有数据遍历就直接进行数据输出

      Assign:  如果有数据遍历那就需要使用assign向模板传递数据。

    }

    调用: {ts:插件名 属性=值 属性=值...}

    {ts:sqllist table="" max="" orderby="" sql=""}

  【sqllist.php】

<?php
require_once("./smarty.inc.php");
$tpl -> display("sqllist.html");
?>

  【sqllist.php】

{ts:sqllist table="ts_news" max="1"}
<hr/>
{ts:sqllist sql="select * from ts_news" max=2}

#在sqllist.php中获取前台sqllist.html中的值#

 

posted @ 2017-05-17 20:04  Shuo_128  阅读(266)  评论(0编辑  收藏  举报