~function(){
/*
微型模板引擎 tmpl

@示例：
<script type="text/tmpl" id="tmpl-demo">
	<ol title="<%=data.name%>">
		<% for (var i = 0, l = data.list.length; i < l; i ++) { %>
			<li><%=data.list[i]%></li>
		<% } %>
	</ol>
</script>

方式一：
var render = Q.tmpl(document.getElementById('tmpl-demo').innerHTML);
var bb = render({name: 'demo data', list: [202, 96, 133, 134]});

方式二：
var bb = Q.tmpl(document.getElementById('tmpl-demo').innerHTML,{name: 'demo data123', list: [2102, 96, 133, 134]});

*/
window.tmpl = (function (cache){
    var r = /(?:^|%>)([\s|\S]*?)(<%(?!\=)|$)/g, 
        z = /(\"|\\)/g,
		m = /<%=([\s\S]*?)%>/g;
    return function (s,data,fillto){
    	if(document.getElementById(s))s=document.getElementById(s).innerHTML;
		if (!(s in cache)){
			cache[s] = s.replace(r,function(a, b){        
				return ';s.push("'+b.replace(z,"\\$1").replace(m, function (e, f){
					return '",'+f.replace(/\\"/g, '"')+',"';
				})+'");';
			}).replace(/\r|\n/g,"");                 
		}
        var $fn = Function('data',"var s=[];"+ cache[s] +" return s.join('');");
		if(fillto){
			return $(fillto).html($fn(data));
		}else{
			return data ? $fn(data) : $fn;
		}
    };
})({});

}();

