/**
 *模板替换
 *@author	stone
 *@data		2011-04-04
 */


var GBase = GBase || {};
GBase.Template={
	/**
	 *使用JSON数据填充模板
	 *@param {object}	json	json对象
	 *@param {string}	tpl		模板字符串
	 *@example	
	 *@return	替换结果字符串
	 */
	parse : function(json,tpl,splitStr)
	{
		var blocks=[];	
		/*
		for(var i=0,len=json.length; i<len; i++){			
			blocks.push(tpl.replace(/{(\w+)}/g,function(a, b){
				return json[i][b] !== undefined ? json[i][b] : a;
			}));
		}*/
		$.each(json,function(ei,el){			
			blocks.push(tpl.replace(/{(\w+)}/g,function(a, b){
				return el[b] !== undefined ? el[b] : a;
			}));								   
		})
		return blocks.join(splitStr || '');
	},

	/**
	 *
	 *@param {object}	json	json对象
	 *@param {string}	target	目标填充元素id
	 *@param {string}	tpl		模板textarea的id
	 */
	stuff : function(json,target,tpl,splitStr)
	{
		tpl = $(tpl).text();
		target = $(target);
		var m = tpl.match(/<#([\s\S]+)#>/);
		var blockTpl= m[1];
		target.html(tpl.replace(m[0], this.parse(json, blockTpl, splitStr)));
		target.css('visibility','visible');
	}
};
