Authored by weiqingting

模板2修改

... ... @@ -95,8 +95,6 @@ var util = {
return result;
},
__template2: function(str, source) {
// console.log(str);
var result = "";
var script = [];
script.push('var each=function(obj,fn){');
script.push(' var that=this;');
... ... @@ -112,8 +110,7 @@ var util = {
script.push(' }');
script.push(' return;');
script.push(' }');
script.push('}');
result = script.join('') + ";\n";
script.push('};');
var formstr = function(code) {
return "'" + code
... ... @@ -121,56 +118,73 @@ var util = {
.replace(/\r/g, '\\r')
.replace(/\n/g, '\\n') + "'";
}
//获取参数
var params = [];
var html = "";
html = str.replace(/\[\[(.*?)\]\]/igm, function(name, $1) {
var trim = $1.replace(/^\s+|\s+$/, '');
var formJs=function(code){
var trim = code.replace(/^\s+|\s+$/, '');
console.log(trim);
//each goodsSizeList as item __index
if (/^each/.test(trim)) {
trim = trim.replace(/^each\s+([a-zA-Z0-1.]+)\s+as\s+(\w+)\s+(\w+)/, function($0, $1, $2, $3) {
params.push($1);
return trim.replace(/^each\b([\w\W]+)\bas\b([\w\W]+)\b([\w\W]+)/, function($0, $1, $2, $3) {
return 'each\( ' + $1 + ',function(' + $3 + ',' + $2 + '){';
});
return "[[" + trim + "]]";
} else
if (/^\/each/.test(trim)) {
trim = trim.replace(/^\/each/, '});')
return "[[" + trim + "]]";
}else if (/^\/each/.test(trim)) {
return trim.replace(/^\/each/, '});')
} else if (/^if/.test(trim)) {
trim = trim.replace(/^if(.+)/, function($0, $1) {
params.push($1);
return trim.replace(/^if(.+)/, function($0, $1) {
return 'if(' + $1 + '){';
});
return "[[" + trim + "]]";
} else if (/^\/if/.test(trim)) {
trim = trim.replace(/^\/if/, '};')
return "[[" + trim + "]]";
return trim.replace(/^\/if/, '};')
} else if (/^else$/.test(trim)) {
trim = trim.replace(/else/, '}else{');
return "[[" + trim + "]]";
} else {
params.push(trim);
return "[[+(" + trim + ")+]]";
return trim.replace(/else/, '}else{');
} else{
return code;
}
}
// var PARAMS={};
var OBJECT=/\.\w+|'.*'|".*"/g;
var KEYWORDS='for,if,else,each,as';
var SPECHAR=/\W/g;
var NUMBER=/^\d[^,]*|,\d[^,]*/g;
var SPLIT2_RE = /^$|,+/;
var REGEXKEYWORDS=new RegExp("\\b"+[KEYWORDS.replace(/,/g,'\\b|\\b')].join('|'),'g');
var getVar=function(code){
var strArr= code.replace(OBJECT,'')
.replace(SPECHAR,',')
.replace(REGEXKEYWORDS,'')
.replace(NUMBER,'')
.split(SPLIT2_RE);
for(var key in strArr){
if(strArr[key]&&!source.hasOwnProperty(strArr[key])){
source[strArr[key]]="";
}
});
// console.log(params);
//.match(/(\w+)+/g)
for (var i in params) {
var aps = params[i].split(/\+|\-|\*|\/|\=|\!|\~|'|"|\||&/g);
for (var j in aps) {
var ap = aps[j].replace(/^\s+|\s+$/, '');
// console.log(":" + ap + ":");
if (!/^[0-9]+$/.test(ap) && !/^\s*$/.test(ap) && !/\./g.test(ap)) {
if (!source.hasOwnProperty(ap)) {
source[ap] = "";
// console.log(":" + ap + ":");
}
}
var code=[],params={};
var arr=str.split(/\[\[|\]\]/).map(function(item,index){
if(index%2){
return{
value:item,
regex:true
};
}else{
return{
value:item,
regex:false
};
}
});
arr.forEach(function(item,index){
if(item.regex){
getVar(item.value);
code.push(formJs(item.value));
}else{
code.push("result.push("+formstr(item.value)+");");
}
});
// var header="var result=[],";
// header+=Object.keys(source).join(',')+";"
var keys = [];
for (var key in source) {
... ... @@ -180,27 +194,15 @@ var util = {
keys.push(key + "=" + JSON.stringify(source[key]));
}
}
var header = "var " + keys.join(',') + ";";
var header = "var result=[]," + keys.join(',') + ";";
html = html.replace(/(^|\]\])(.*?)(\[\[|$)/igm, function(name, $1, $2) {
var trim = $2.replace(/^\s+|\s+$/, '');
return trim ? formstr($2) : '';
});
// var result="var result='';result+=";
var arr = html.split(/\n+/igm);
result += "var p=[];\n";
for (var i = 0; i < arr.length; i++) {
if (/^'/.test(arr[i])) {
result += "p.push(" + (arr[i]) + ");\n";
} else {
result += (arr[i]);
}
}
result += "return p.join('');\n";
var fn = new Function(header + result);
var b = fn.call(source);
return b;
var result=header + code.join('\n')+"return result.join('');";
console.log(result);
var fn = new Function(script.join('')+result);
return fn.call(source);
},
__input: function(id) {
return !!~$.trim($('#' + id).val()) ? $('#' + id).val() : ""
... ...