Permalink
Browse files

Simplify the code since we can assume no browser.

  • Loading branch information...
creationix committed Apr 7, 2010
1 parent 75e1d26 commit c4e7928e8be104dfe20e44aaf5a2554ba712a708
Showing with 23 additions and 39 deletions.
  1. +23 −39 lib/tmpl.js
View
@@ -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;
+}

0 comments on commit c4e7928

Please sign in to comment.