php打印模板插件,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

function smarty_modifier_reverse($string)

{

if(is_string($string)){

return strrev($string);

}else{

return false;

}

}

?>

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

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

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

reverse == {$test|reverse}

上面的这个例子是把一个字符串进行反转,结果是: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'], "

  • ".$_html_result."
");

} else {

return "

  • ".implode("\n",$_html_result)."
";

}

}

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

$_output = '';

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

$_output .= '

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", "οnclick=sep()");

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

{html_lis options=$cust_lis js=$onclick}

d),输入结果为

  • 中国
  • 上海
  • 合肥
  • 六安

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值