天天看点

smarty的插件功能是smarty模板的精华

一,smarty插件介绍

smarty的插件放在/smarty/libs/plugins下面,它为程序的开发 提供了很大的方便,例如:{$yesterday|date_format:"%h:%m:%s"}smarty自带的日期格式化插件,对变 量$yesterday进行格式化。在我们的php文件中,并不需要对date_format进行处理,我们只要拿来用就好了。

二,smarty插件命名规则

1,插件文件名命名规则

type . name .php

type有以下几种

function

modifier

block

compiler

prefilter

postfilter

outputfilter

resource

insert

例如:modifier .date_format .php这个就是smarty自带的日期插件的文件名

2,插件文件里面的函数命名规则

smarty_type _name ()

例如:smarty_modifier _date_format

上面的紫色字对应的是插件类型,桔黄色字对应的是插件名称

三,添加自定义插件功能

个人觉得modifier 和function 这二种类型的插件最有用,也是最常用的。所以下面我以这二个类型来举例子

1,添加modifier插件

a ),/smarty/libs/plugins下面建个文件modifier.reverse.php

smarty的插件功能是smarty模板的精华

<?php  

function smarty_modifier_reverse($string)  

{  

 if(is_string($string)){  

  return strrev($string);  

 }else{  

  return false;  

 }  

}  

?>  

 b),在调用模块的文件文件里加上

smarty的插件功能是smarty模板的精华

$this->tpl->assign("test", "123456789");  

 c),在模块文件文件中加入

smarty的插件功能是smarty模板的精华

<div>reverse == {$test|reverse}</div>     

上面的这个例子是把一个字符串进行反转,结果是:987654321

2,添加function插件

a ),/smarty/libs/plugins下面建个文件function.html_lis.php

smarty的插件功能是smarty模板的精华

function smarty_function_html_lis($params, &$smarty)  

    require_once $smarty->_get_plugin_filepath('shared','escape_special_chars');  

    $class = 'li_style';  

    $options = null;  

    $separator = '';  

    $js = '';  

    $labels =true;  

    $output = null;  

    $extra = '';  

    foreach($params as $_key => $_val) {  

        switch($_key) {  

            case 'class':  

            case 'separator':  

                $$_key = $_val;  

                break;  

            case 'labels':  

                $$_key = (bool)$_val;  

            case 'js':  

            case 'options':  

                $$_key = (array)$_val;  

            case 'output':  

                $$_key = array_values((array)$_val);  

            case 'lis':  

                $smarty->trigger_error('html_lis: the use of the "lis" attribute is deprecated, use "options" instead', e_user_warning);  

                $options = (array)$_val;  

            default:  

                if(!is_array($_val)) {  

                    $extra .= ' '.$_key.'="'.smarty_function_escape_special_chars($_val).'"';  

                } else {  

                    $smarty->trigger_error("html_lis: extra attribute '$_key' cannot be an array", e_user_notice);  

                }  

        }  

    }  

    if (!isset($options) && !isset($values))  

        return ''; /* raise error here? */  

    $_html_result = array();  

    if (isset($options)) {  

        foreach ($options as $_key=>$_val)  

            $_html_result[] = smarty_function_html_lis_output($class, $_key, $_val, $extra, $separator, $labels, $js);  

    } else {  

        foreach ($values as $_i=>$_key) {  

            $_val = isset($output[$_i]) ? $output[$_i] : '';  

    if(!empty($params['assign'])) {  

        $smarty->assign($params['assign'], "<ul>".$_html_result."</ul>");  

        return "<ul>".implode("\n",$_html_result)."</ul>";  

function smarty_function_html_lis_output($class, $value, $output, $extra, $separator, $labels, $js) {  

    $_output = '';  

    if ($labels) $_output .= '';  

    $_output .= '<li tip="'  

        . smarty_function_escape_special_chars($value) . '"';  

 if($js) $_output .= $js  ;  

    $_output .= $extra . ' />' . $output;  

    $_output .=  $separator;  

    return $_output;  

  b),在调用模块的文件文件里加上

smarty的插件功能是smarty模板的精华

$this->tpl->assign('cust_lis', array(  

   "china" => '中国',  

   "shanghai" => '上海',  

   "heifei" => '合肥',  

   "luan" => '六安'));  

$this->tpl->assign("onclick", "onclick=sep()");   

smarty的插件功能是smarty模板的精华

<div>  

{html_lis  options=$cust_lis js=$onclick}  

</div>  

 d),输入结果为

smarty的插件功能是smarty模板的精华

<ul><li class="li_style" tip="china" onclick=sep() />中国  

<li class="li_style" tip="shanghai" onclick=sep() />上海  

<li class="li_style" tip="heifei" onclick=sep() />合肥  

<li class="li_style" tip="luan" onclick=sep() />六安</ul>  

 上面的例子是生成ul标签的一个smarty插件,把checkbox拿过来改一改,而成的。