天天看點

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