file:block.data_grid.php

<?php

/**

* Smarty plugin

* @package Smarty

* @subpackage plugins

*/

/**

* Smarty {data_grid}{/data_grid} block plugin

*

* Type: block function<br>

* Name: data_grid<br>

* Purpose: format a array to a table

* @param array

* <pre>

* Params: style: string (email)

* indent: integer (0)

* wrap: integer (80)

* wrap_char string ("\n")

* indent_char: string (" ")

* wrap_boundary: boolean (true)

* </pre>

* @author lxb <lxbzmy at 163 dot com>

* @param string contents of the block

*/

function smarty_block_data_grid($params, $content, &$smarty , &$repeat )

{

global $_smarty_data_grid;

global $_smarty_data_grid_column_stack ;

static $counter;

static $column_stack;

$last = false;

if (is_null($content)) {//&#36825;&#26159;&#31532;&#19968;&#27425;&#35843;&#29992;datagrid&#65292;&#38656;&#35201;&#21021;&#22987;&#21270;

$_smarty_data_grid =array();

$_smarty_data_grid_column_stack = array();

$column_stack = array();

$counter = 0;

}

//&#21021;&#22987;&#21270;&#20197;&#21518;&#31532;&#20108;&#27425;&#35843;&#29992;&#20197;&#21069;&#65292;&#25152;&#26377;&#30340;&#21015;&#37117;&#27880;&#20876;&#20102;&#19968;&#20415;&#65292;&#38656;&#35201;&#20851;&#38381;&#21015;&#27880;&#20876;&#12290;&#23558;&#21015;&#20449;&#24687;&#20445;&#23384;&#21040;&#38745;&#24577;&#21464;&#37327;&#12290;

if(is_array($_smarty_data_grid_column_stack) && count($_smarty_data_grid_column_stack) >0)

{

$column_stack = $_smarty_data_grid_column_stack;

$_smarty_data_grid_column_stack = false;

}

//&#25968;&#25454;&#28304;&#25351;&#38024;&#65292;&#21040;&#20102;&#37027;&#19968;&#34892;

if(count($params['datasource']) > $counter)

{

$_smarty_data_grid['data'] = $params['datasource'][$counter++];

$repeat = true;

}else

{

$repeat = false;

$last = true;

}

// $table_attributes = array('width','id','class','cellspacing','style','summary','datasource');

$attributes = '';

if($counter <2)//&#20195;&#34920;&#31532;&#19968;&#27425;&#35775;&#38382;&#36825;&#20010;&#20989;&#25968;&#65292;&#38656;&#35201;&#36755;&#20986;&#34920;&#26684;&#22836;&#65292;&#20197;&#21518;&#35775;&#38382;&#21017;&#19981;&#38656;&#35201;&#20877;&#25191;&#34892;&#36825;&#20010;&#36807;&#31243;&#20102;

{

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

switch($_key)

{

case 'id':

case 'summary':

case 'class':

case 'cellspacing':

case 'style':

case 'width':

case 'border':

case 'height'://&#19981;&#24314;&#35758;&#20351;&#29992;&#35813;&#23646;&#24615;.xhtml1.1 &#37324;&#27809;&#26377;&#35813;&#23646;&#24615;

$attributes .= $_key .'="' .$_val.'" ';

break;

case 'caption':

//$_smarty_data_grid['caption'] = $_val;

case 'datasource':

break;

default:

$smarty->trigger_error("data_grid: unknown attribute '$_key'");

}

}

}

if(!is_null($content) && !$last && $counter >2) //

{

return "\t\t<tr class=\"".($counter%2==0?'odd':'even')."\" >\n".$content."\t\t</tr>\n";

}elseif($last )

{

return ("\t\t<tr class=\"".($counter%2==0?'even':'odd')."\" >\n".$content."\t\t</tr>\n\t</tody>\n</table>");

//exit('first');

}elseif(count($column_stack) ==0 && is_null($content))

{

//&#31532;&#19968;&#27425;&#35775;&#38382;&#65292;&#36755;&#20986;&#34920;&#22836;&#12290;

echo '<table '.$attributes.">\n";

if(isset($params['caption']))echo "\t<caption>".$params['caption']."</caption>\n";

}else

{

//&#31532;&#20108;&#27425;&#35775;&#38382;,&#30693;&#36947;&#20102;&#21015;&#20449;&#24687;,&#25152;&#20197;&#38656;&#35201;&#20808;&#36755;&#20986;&#21015;&#20449;&#24687;&#12290;

$table_col = '';

$table_thead = "\t<thead>\n";

foreach($column_stack as $v)

{

$table_col .="\t<col ".(isset($v['style'])?'style="'.$v['style'].'"':'')." />\n";

$table_thead .= "\t\t<th scope=\"col\">". $v['title']."</th>\n";

}

//var_dump($column_stack);

$table_thead .= "\t</thead>\n\t<tbody>\n";

return ($table_col.$table_thead."\t\t<tr class=\"odd\" >\n".$content."\t\t</tr>\n");

}

}

?>

file2:function.column.php

<?php

/**

* Smarty plugin

* @package Smarty

* @subpackage plugins

*/

/**

* Smarty {html_options} function plugin

*

* Type: function<br>

* Name: column<br>

* Input:<br>

* - title (required if no options supplied) - array

* - item (required if no values supplied) - associative array

* @link http://smarty.php.net/manual/en/language.function.html.options.php {html_image}

* (Smarty online manual)

* @author lxb <lxbzmy at 163 dot com>

* @param array

* @param Smarty

* @return string

*/

function smarty_function_column($params, &$smarty)

{

if($smarty->_tag_stack[count($smarty->_tag_stack)-1][0] == 'data_grid')

{

//&#20174;data_grid&#20989;&#25968;&#37324;&#21462;&#24471;&#21464;&#37327;&#20869;&#23481;&#12290;

global $_smarty_data_grid;

global $_smarty_data_grid_column_stack;

//&#35745;&#25968;&#22120;&#32479;&#35745;&#20989;&#25968;&#35843;&#29992;&#20102;&#20960;&#27425;

static $counter;

//&#20197;&#19979;&#21464;&#37327;&#24517;&#39035;&#35774;&#32622;

if(isset($params['title'],$params['item']))

{

//&#22914;&#26524;&#26159;&#31532;&#19968;&#27425;&#35775;&#38382;$_smarty_data_grid_column_stack&#26159;&#20010;&#25968;&#32452;&#65292;&#31532;&#20108;&#27425;&#20197;&#21518;&#20026;false

if(is_array($_smarty_data_grid_column_stack))

{

array_push($_smarty_data_grid_column_stack,$params);

$counter = 1;

}

//var_dump($params['item']);

//var_dump( $_smarty_data_grid['data'] );

//exit(var_dump($_smarty_data_grid['data'][$params['item']] ));

//array_push($_smarty_data_grid_column_stack,$_smarty_data_grid['data'][$params['item']]);

return ("\t\t\t<td >".$_smarty_data_grid['data'][$params['item']] .'</td>');

}else

{

$smarty->trigger_error("column: params title or itme no set");

}

}else

{

$smarty->trigger_error("column: column must be in {data_grid} tag");

}

}

/* vim: set expandtab: */

?>

file:sample.tpl

{data_grid id="name" width="100" border=1 datasource=$you summary="&#20320;&#22909;&#34920;&#26684;" caption="&#20154;&#21592;&#21517;&#21333;"}

{column title="&#22995;&#21517;" item='name'}

{column title="&#22995;&#21517;" item='title'}

{/data_grid}

file:index.php

$tpl = new Smarty();

$tpl->assign('you',array(array('name'=>'tick','title'=>'nick'),array('name'=>'bug','title'=>'anny'),array('name'=>'lxb','title'=>'zmy')));

$tpl->display('datagrid.tpl');