ThinkPHP 自定义标签测试 冰糖

测试功能:根据source(二维数组或文本[1,男|0,女])生成单选按钮组,
text:显示值 对于二维数组

val:取值 对于二维数组

文本:逗号前为值,后为显示值

<?php

/**
 +-----------------------------
 * @desc ThinkPHP Fcb自定义标签
 +-----------------------------
 * @author 710
 +-----------------------------
 * @date 2011-12-14
 +-----------------------------
 */
import("TagLib");
class TagLibFcb extends TagLib{
	
	protected $tags = array(
	  'radiolist'=>array('attr'=>'name,source,type,text,val,checked,style,class','close'=>0,'level'=>3,'alias'=>'rt,rl'),
	);
	
	/**
	 +-------------------------------
	 * @desc  fcb:radiolist 解析
	 * @param string $type=>数据源类型
	 * list:数组源,text:文本源(如:1,男|2,女)
	 +-------------------------------
	 */
	public function _radiolist($attr,$content,$type='list'){
		$tag = $this->parseXmlAttr($attr,'radiolist');
		$type = empty($tag['type']) ? $type : $tag['type'];
		$source = $tag['source'];
		$name = isset($tag['name']) ? 'radio'.date('is') : $tag['name'];
		$text = empty($tag['text']) ? 'text' : $tag['text'];
		$val = empty($tag['val']) ? 'id' : $tag['val'];
		$style = empty($tag['style']) ? '' : 'style="'. $tag['style'].'"';
		$class= empty($tag['class']) ? '' : 'class="'. $tag['class'].'"';
		$checked = isset($tag['checked']) ? $tag['checked'] : '';
		$result='';
		if($type=='list'){
			$result.='<?php foreach($'.$source.' as $key=>$val): ?>';
			$result.='<input type="radio" <?php if($val['.$val.']=="'.$checked.'"): echo "checked=\"checked\"";endif ?>   '.$class.' '.$style.' name="'.$name.'" value="<?php echo $val['.$val.'] ?>" /><?php echo $val['.$text.'] ?>';
			$result.='<?php endforeach ?>';
		}elseif($type=='text'){
			$list = explode("|",$source);
			foreach($list as $key=>$val){
				$item = explode(",",$val);
				$result .= '<input type="radio" '.($item[0]==$checked?"checked=\"checked\"":"").' value="'.$item[0].'" name="'.$name.'" '.$class.' '.$style.' />'.$item[1];
			}
		}
		return $result;
	}
	
	/**
	 +------------------------------
	 * radiolist 别名 => 数据源为文本
	 * <fcb:rt source="1,男|2,女" />
	 * 逗号前为取得值,后为显示值
	 +------------------------------
	 */
	public function _rt($attr,$content){
		return $this->_radiolist($attr, $content,'text');
	}
	
	/**
	 +-------------------------------
	 * radiolist 别名=> 数据源为数组
	 * <fcb:rl source="list" text="name" val="id" />
	 * text:显示值,val:取值
	 +-------------------------------
	 */
	public function _rl($attr,$content){
		return $this->_radiolist($attr, $content,'list');
	}
	
}
?>
原文地址:https://www.cnblogs.com/hxtgirq710/p/tplib.html