-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Simplify the code since we can assume no browser.
- Loading branch information
1 parent
75e1d26
commit c4e7928
Showing
1 changed file
with
23 additions
and
39 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 |
---|---|---|
@@ -1,43 +1,27 @@ | ||
// A simple templating solution using <code>with(){}</code> for simplified templates. | ||
// John Resig - http://ejohn.org/ - MIT Licensed | ||
// Modified for CommonJS by Tim Caswell <tim@creationix.com> | ||
// Modified for node.JS by Tim Caswell <tim@creationix.com> | ||
|
||
var tmpl; | ||
(function(){ | ||
var cache = {}; | ||
|
||
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) : | ||
module.exports = function tmpl(str, data){ | ||
// Generate a reusable function that will serve as a template | ||
// generator (and which will be cached). | ||
var fn = new Function("obj", | ||
"var p=[],print=function(){p.push.apply(p,arguments);};" + | ||
|
||
// 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; | ||
}; | ||
})(); | ||
|
||
// Hook for CommonJS | ||
if (typeof module !== 'undefined' && "exports" in module) { | ||
module.exports = tmpl; | ||
} | ||
// 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; | ||
} |