forked from mqyqingfeng/Blog
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fc5c42c
commit 8d81ae0
Showing
10 changed files
with
421 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
|
||
<head> | ||
<title>template</title> | ||
</head> | ||
|
||
<body> | ||
<div id="container"></div> | ||
<script type="text/html" id="user_tmpl"> | ||
<%for ( var i = 0; i < users.length; i++ ) { %> | ||
<li> | ||
<a href="<%=users[i].url%>"> | ||
<%=users[i].name%> | ||
</a> | ||
</li> | ||
<% } %> | ||
</script> | ||
<script src="template.js"></script> | ||
</body> | ||
|
||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/** | ||
* 这是文章中的原版实现,文章中对代码进行了简化 | ||
*/ | ||
(function() { | ||
var cache = {}; | ||
|
||
this.tmpl = function tmpl(str, data) { | ||
// Figure out if we're getting a template, or if we need to | ||
// load the template - and be sure to cache the result. | ||
var fn = !/\W/.test(str) ? | ||
cache[str] = cache[str] || | ||
tmpl(document.getElementById(str).innerHTML) : | ||
|
||
// Generate a reusable function that will serve as a template | ||
// generator (and which will be cached). | ||
new Function("obj", | ||
"var p=[],print=function(){p.push.apply(p,arguments);};" + | ||
|
||
// Introduce the data as local variables using with(){} | ||
"with(obj){p.push('" + | ||
|
||
// Convert the template into pure JavaScript | ||
str | ||
.replace(/[\r\t\n]/g, " ") | ||
.split("<%").join("\t") | ||
.replace(/((^|%>)[^\t]*)'/g, "$1\r") | ||
.replace(/\t=(.*?)%>/g, "',$1,'") | ||
.split("\t").join("');") | ||
.split("%>").join("p.push('") | ||
.split("\r").join("\\'") + | ||
"');}return p.join('');"); | ||
|
||
// Provide some basic currying to the user | ||
return data ? fn(data) : fn; | ||
}; | ||
})(); | ||
|
||
var results = document.getElementById("container"); | ||
|
||
var data2 = { | ||
users: [ | ||
{ "name": "Byron", "url": "http://localhost" }, | ||
{ "name": "Casper", "url": "http://localhost" }, | ||
{ "name": "Frank", "url": "http://localhost" } | ||
] | ||
} | ||
|
||
var compiled = tmpl("user_tmpl", data2); | ||
results.innerHTML = compiled; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,53 +1,62 @@ | ||
(function(){ | ||
var cache = {}; | ||
|
||
this.tmpl = function tmpl(str, data){ | ||
// Figure out if we're getting a template, or if we need to | ||
// load the template - and be sure to cache the result. | ||
var fn = !/\W/.test(str) ? | ||
cache[str] = cache[str] || | ||
tmpl(document.getElementById(str).innerHTML) : | ||
|
||
// Generate a reusable function that will serve as a template | ||
// generator (and which will be cached). | ||
new Function("obj", | ||
"var p=[],print=function(){p.push.apply(p,arguments);};" + | ||
|
||
// Introduce the data as local variables using with(){} | ||
"with(obj){p.push('" + | ||
|
||
// Convert the template into pure JavaScript | ||
str | ||
.replace(/[\r\t\n]/g, " ") | ||
.split("<%").join("\t") | ||
.replace(/((^|%>)[^\t]*)'/g, "$1\r") | ||
.replace(/\t=(.*?)%>/g, "',$1,'") | ||
.split("\t").join("');") | ||
.split("%>").join("p.push('") | ||
.split("\r").join("\\'") | ||
+ "');}return p.join('');"); | ||
|
||
// Provide some basic currying to the user | ||
return data ? fn( data ) : fn; | ||
}; | ||
})(); | ||
/** | ||
* 模板引擎第五版 | ||
*/ | ||
|
||
var settings = { | ||
// 求值 | ||
evaluate: /<%([\s\S]+?)%>/g, | ||
// 插入 | ||
interpolate: /<%=([\s\S]+?)%>/g, | ||
}; | ||
|
||
var escapes = { | ||
"'": "'", | ||
'\\': '\\', | ||
'\r': 'r', | ||
'\n': 'n', | ||
'\u2028': 'u2028', | ||
'\u2029': 'u2029' | ||
}; | ||
|
||
var escapeRegExp = /\\|'|\r|\n|\u2028|\u2029/g; | ||
|
||
|
||
var template = function(text) { | ||
|
||
var source = "var __p='';\n"; | ||
source = source + "with(obj){\n" | ||
source = source + "__p+='"; | ||
|
||
var main = text | ||
.replace(escapeRegExp, function(match) { | ||
return '\\' + escapes[match]; | ||
}) | ||
.replace(settings.interpolate, function(match, interpolate){ | ||
return "'+\n" + interpolate + "+\n'" | ||
}) | ||
.replace(settings.evaluate, function(match, evaluate){ | ||
return "';\n " + evaluate + "\n__p+='" | ||
}) | ||
|
||
source = source + main + "';\n }; \n return __p;"; | ||
|
||
console.log(source) | ||
|
||
var render = new Function('obj', source); | ||
|
||
return render; | ||
}; | ||
|
||
var results = document.getElementById("container"); | ||
|
||
var data2 = { | ||
var data = { | ||
users: [ | ||
{ "name": "Byron", "url": "http://localhost" }, | ||
{ "name": "Casper", "url": "http://localhost" }, | ||
{ "name": "Frank", "url": "http://localhost" } | ||
] | ||
} | ||
|
||
|
||
// var compiled = tmpl("user_tmpl"); | ||
// results.innerHTML = compiled(data2); | ||
// | ||
// | ||
var compiled = tmpl("user_tmpl", data2); | ||
results.innerHTML = compiled; | ||
var text = document.getElementById("user_tmpl").innerHTML | ||
var compiled = template(text); | ||
results.innerHTML = compiled(data); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
|
||
<head> | ||
<title>template</title> | ||
</head> | ||
|
||
<body> | ||
<div id="container"></div> | ||
<script type="text/html" id="user_tmpl"> | ||
<% for ( var i = 0; i < users.length; i++ ) { %> | ||
<li><a href="<%=users[i].url%>"><%=users[i].name%></a></li> | ||
<% } %> | ||
</script> | ||
<script src="template.js"></script> | ||
</body> | ||
|
||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/** | ||
* 模板引擎第六版 | ||
*/ | ||
|
||
var settings = { | ||
// 求值 | ||
evaluate: /<%([\s\S]+?)%>/g, | ||
// 插入 | ||
interpolate: /<%=([\s\S]+?)%>/g, | ||
}; | ||
|
||
var escapes = { | ||
"'": "'", | ||
'\\': '\\', | ||
'\r': 'r', | ||
'\n': 'n', | ||
'\u2028': 'u2028', | ||
'\u2029': 'u2029' | ||
}; | ||
|
||
var escapeRegExp = /\\|'|\r|\n|\u2028|\u2029/g; | ||
|
||
|
||
var template = function(text) { | ||
|
||
var source = "var __t, __p='';\n"; | ||
source = source + "with(obj){\n" | ||
source = source + "__p+='"; | ||
|
||
var main = text | ||
.replace(escapeRegExp, function(match) { | ||
return '\\' + escapes[match]; | ||
}) | ||
.replace(settings.interpolate, function(match, interpolate){ | ||
return "'+\n((__t=(" + interpolate + "))==null?'':__t)+\n'" | ||
}) | ||
.replace(settings.evaluate, function(match, evaluate){ | ||
return "';\n " + evaluate + "\n__p+='" | ||
}) | ||
|
||
source = source + main + "';\n }; \n return __p;"; | ||
|
||
console.log(source) | ||
|
||
var render = new Function('obj', source); | ||
|
||
return render; | ||
}; | ||
|
||
var results = document.getElementById("container"); | ||
|
||
var data = { | ||
users: [ | ||
{ "url": "http://localhost" }, | ||
{ "name": "Casper", "url": "http://localhost" }, | ||
{ "name": "Frank", "url": "http://localhost" } | ||
] | ||
} | ||
|
||
var text = document.getElementById("user_tmpl").innerHTML | ||
var compiled = template(text); | ||
results.innerHTML = compiled(data); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
|
||
<head> | ||
<title>template</title> | ||
</head> | ||
|
||
<body> | ||
<div id="container"></div> | ||
<script type="text/html" id="user_tmpl"> | ||
<% for ( var i = 0; i < users.length; i++ ) { %> | ||
<li><a href="<%=users[i].url%>"><%=users[i].name%></a></li> | ||
<% } %> | ||
</script> | ||
<script src="template.js"></script> | ||
</body> | ||
|
||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
/** | ||
* 模板引擎第七版 | ||
*/ | ||
|
||
var settings = { | ||
// 求值 | ||
evaluate: /<%([\s\S]+?)%>/g, | ||
// 插入 | ||
interpolate: /<%=([\s\S]+?)%>/g, | ||
}; | ||
|
||
var escapes = { | ||
"'": "'", | ||
'\\': '\\', | ||
'\r': 'r', | ||
'\n': 'n', | ||
'\u2028': 'u2028', | ||
'\u2029': 'u2029' | ||
}; | ||
|
||
var escapeRegExp = /\\|'|\r|\n|\u2028|\u2029/g; | ||
|
||
|
||
var template = function(text) { | ||
|
||
var matcher = RegExp([ | ||
(settings.interpolate).source, | ||
(settings.evaluate).source | ||
].join('|') + '|$', 'g'); | ||
|
||
var index = 0; | ||
var source = "__p+='"; | ||
|
||
text.replace(matcher, function(match, interpolate, evaluate, offset) { | ||
|
||
source += text.slice(index, offset).replace(escapeRegExp, function(match) { | ||
return '\\' + escapes[match]; | ||
}); | ||
|
||
index = offset + match.length; | ||
|
||
if (interpolate) { | ||
source += "'+\n((__t=(" + interpolate + "))==null?'':__t)+\n'"; | ||
} else if (evaluate) { | ||
source += "';\n" + evaluate + "\n__p+='"; | ||
} | ||
|
||
return match; | ||
}); | ||
|
||
source += "';\n"; | ||
|
||
source = 'with(obj||{}){\n' + source + '}\n' | ||
|
||
source = "var __t, __p='';" + | ||
source + 'return __p;\n'; | ||
|
||
console.log(source) | ||
|
||
var render = new Function('obj', source); | ||
|
||
return render; | ||
}; | ||
|
||
var results = document.getElementById("container"); | ||
|
||
var data = { | ||
users: [ | ||
{ "url": "http://localhost" }, | ||
{ "name": "Casper", "url": "http://localhost" }, | ||
{ "name": "Frank", "url": "http://localhost" } | ||
] | ||
} | ||
|
||
var text = document.getElementById("user_tmpl").innerHTML | ||
var compiled = template(text); | ||
results.innerHTML = compiled(data); |
Oops, something went wrong.