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拿過來改一改,而成的。
總結
以上是生活随笔為你收集整理的php打印模板插件,smarty的插件功能是smarty模板的精华的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 德 梅齐里亚克的砝码问题matlab,德
- 下一篇: 72空调能带多少平方