凌的博客

您现在的位置是: 首页 > 学无止境 > PHP > 

PHP

使用Smarty block块函数

2015-05-19 PHP 826
/** 自定义块函数 */ function smarty_block_myblock($params, $content, &$smarty, &$repeat, $template){ extract($params); //注册 block 索引 $block_index = md5(serialize($params)); $smarty-&
/**
自定义块函数
*/
function smarty_block_myblock($params, $content, &$smarty, &$repeat, $template){
	extract($params);
	//注册 block 索引
	$block_index = md5(serialize($params));
	$smarty->block_data[$block_index] = array();
	if(empty($smarty->block_data[$block_index])){
		$lists = D()->field($field)->from('sort')->where(" id in ('".implode("','",explode(',',$id)  )."' )")->order($order)->select();
		$smarty->block_data[$block_index] = $lists;
	}
	//注册block 循环键值索引
	if(method_exists($smarty,'get_template_vars'))
		$_bindex = $smarty->get_template_vars('_bindex');
	else
		$_bindex = $smarty->getVariable('_bindex')->value;
	if(!$_bindex)
		$_bindex = array();
		
	if($name)
		if(!isset($_bindex[$name]))
			$_bindex[$name] = 0;
		else
			$_bindex[$name] ++;

	$smarty->assign('_bindex',$_bindex);
	
	//打开循环
	$repeat = true;
	//直接使用数组键值 遍历
	$item = $smarty->block_data[$block_index][$_bindex[$name]];
	$smarty->assign($name,$item);
	//当没有数据的时候
	if(!$item){
		//关闭循环
		$repeat = false;
		if($name){
			//重载block 循环键值索引
			unset($_bindex[$name]);
			$smarty->assign('_bindex',$_bindex);
		}
	}

	echo $content;
}	
/*{myblock name="nav" id="1,2,3,4"}
    {$nav.name}
        {myblock name=$nav['id'] id="5,6,7,8"}
        {$nav.name}
        {/myblock}
{/myblock}*/

文章评论

0条评论