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

张映 发表于 2010-07-20

分类目录: smarty

标签:, , ,

一,smarty插件介绍

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

二,smarty插件命名规则

1,插件文件名命名规则

type . name .php

type有以下几种

  1. function
  2. modifier
  3. block
  4. compiler
  5. prefilter
  6. postfilter
  7. outputfilter
  8. resource
  9. insert

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

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

smarty_type_name()

例如:smarty_modifier_date_format

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

三,添加自定义插件功能

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

1,添加modifier插件

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

<?php
function smarty_modifier_reverse($string)
{
 if(is_string($string)){
  return strrev($string);
 }else{
  return false;
 }
}
?>

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

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

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

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

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

2,添加function插件

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

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;
                break;

            case 'js':
                $$_key = $_val;
                break;

            case 'options':
                $$_key = (array)$_val;
                break;

            case 'output':
                $$_key = array_values((array)$_val);
                break;

            case 'lis':
                $smarty->trigger_error('html_lis: the use of the "lis" attribute is deprecated, use "options" instead', E_USER_WARNING);
                $options = (array)$_val;
                break;

            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);
                }
                break;
        }
    }

    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] : '';
            $_html_result[] = smarty_function_html_lis_output($class, $_key, $_val, $extra, $separator, $labels, $js);
        }

    }

    if(!empty($params['assign'])) {
        $smarty->assign($params['assign'], "<ul>".$_html_result."</ul>");
    } else {

        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;
    if ($labels) $_output .= '';
    $_output .=  $separator;

    return $_output;
}

?>

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

  $this->tpl->assign('cust_lis', array(
     "china" => '中国',
     "shanghai" => '上海',
     "heifei" => '合肥',
     "luan" => '六安'));
  $this->tpl->assign("onclick", "onclick=sep()"); 

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

<div>
{html_lis  options=$cust_lis js=$onclick}
</div>

d),输入结果为

<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拿过来改一改,而成的。



转载请注明
作者:海底苍鹰
地址:http://blog.51yip.com/smarty/926.html/comment-page-1

3 条评论

  1. xmory 留言

    谢谢,文章很不错~

  2. Lily 留言

    请问有没有测试过是在Php里面直接对数据转换速度快还是在模板里面用插件的速度快?

  3. oooo 留言

    哈哈哈哈哈哈哈哈