<?php
set_time_limit(3);
error_reporting(E_ALL);
//var_dump("smarty_block_qkweb");
/*
function smarty_block_qkweb($params, $content, &$smarty, &$repeat) {
	$dataindex = md5(__FUNCTION__ . md5(serialize($params)));
	if(!isset($GLOBALS['blocksdata'][$dataindex])){
		$a= array(array("name"=>"qkweb"),array("name"=>"qkwessb"));
		$GLOBALS['blocksdata'][$dataindex]=$a;
	}
	if(is_array($GLOBALS['blocksdata'][$dataindex])){
		$item = array_shift($GLOBALS['blocksdata'][$dataindex]);
		$smarty->assign($params['name'], $item);
		if(count($GLOBALS['blocksdata'][$dataindex])==0){
			$GLOBALS['blocksdata'][$dataindex] = false;
		}
		$repeat= true;
	}else{
		$repeat = false;
	}
	echo $content;
}
*/

function smarty_block_qkweb($params, $content, &$smarty, &$repeat) {
	if(!isset($params['name']) || $params['name']==""){
		//输出时，需要定义一个变量进行存储
		//当检测参数中没有定义此名称时，可以使用 throw 来抛出一个 SmartyException 对象来停止程序执行
		//throw new SmartyException("Param 'name' is not defined");
		//也可以给它默认一个名字
		$params['name'] = "item";
		//本示例使用默认设置名称为 item 的方法
	}
	$dataindex = md5(__FUNCTION__ . md5(serialize($params)));
	if(!isset($smarty->block_data)){$smarty->block_data=array();}
	if(!isset($smarty->block_data[$dataindex])){
		$res = array(
			array("id"=>1,"name"=>"张三"),
			array("id"=>2,"name"=>"李四"),
			array("id"=>3,"name"=>"王五"),
			array("id"=>4,"name"=>"赵六")
		);
		// $res 要输出的结果；
		// 示例程序是直接定义了要输出的结果，通常是通过读取数据库来创建二维或多数组来实现
		$smarty->block_data[$dataindex]=$res;
	}
	if(is_array($smarty->block_data[$dataindex]) && count($smarty->block_data[$dataindex])>0){
		$item = array_shift($smarty->block_data[$dataindex]);
		$smarty->assign($params['name'], $item);
		if(count($smarty->block_data[$dataindex])==0){
			$smarty->block_data[$dataindex] = false;
			//unset($smarty->block_data[$dataindex]);
            //$repeat = false;
		}
		$repeat= true;
	}else{
		unset($smarty->block_data[$dataindex]);
		$repeat = false;
	}
	echo $content;
}
?>