Permalink
Browse files

Initial commit

  • Loading branch information...
0 parents commit 75e1d26c6c7ac395142ae493c8f642fc0221b1e0 @creationix committed Apr 7, 2010
Showing with 92 additions and 0 deletions.
  1. +49 −0 README.markdown
  2. +43 −0 lib/tmpl.js
49 README.markdown
@@ -0,0 +1,49 @@
+# MicroTemplates
+
+MicroTemplates is a super simple templating library written by John Resig of jQuery fame. This is a port to be used with server-side javascripts like node.js. The library exports a single function called "`tmpl`" that compiles a template and optionally executes it or returns a function that can be reused.
+
+## Syntax
+
+Basically the syntax is pure HTML with embedded javascript code. You can use `<%...%>` for raw code insertion and `<%=...%>` for expressions that you want interpolated.
+
+Assume you have a file called "`item.tmpl`" containing the following:
+
+ <div id="<%=id%>" class="<%=(i % 2 == 1 ? " even" : "")%>">
+ <div class="grid_1 alpha right">
+ <img class="righted" src="<%=profile_image_url%>"/>
+ </div>
+ <div class="grid_6 omega contents">
+ <p><b><a href="/<%=from_user%>"><%=from_user%></a>:</b> <%=text%></p>
+ </div>
+ </div>
+
+Also you have another file called "`users.tmpl`" with the following:
+
+ <% for (var i = 0, l = users.length; i < l; i++) { %>
+ <li><a href="<%= users[i].url %>"><%= users[i].name %></a></li>
+ <% } %>
+
+Notice that you can inline javascript for your logic.
+
+## Usage
+
+Then the code (in node.JS) to render the "`item.tmpl`" template could look like this:
+
+ var fs = require('fs'), // Load node's file system library
+ tmpl = require('tmpl'); // Load this library (assuming it's installed)
+
+ var code = fs.readFileSync("item.tmpl");
+ var show_user = tmpl(code);
+ var html = "";
+ for (var i = 0, l = users.length; i < l; i++) {
+ html += show_user(users[i]);
+ }
+
+
+Also you could execute a template immediately instead of caching the compiled template:
+
+ var fs = require('fs'), // Load node's file system library
+ tmpl = require('tmpl'); // Load this library (assuming it's installed)
+
+ var code = fs.readFileSync("users.tmpl");
+ var html = tmpl(fs.readFileSync(code), users);
43 lib/tmpl.js
@@ -0,0 +1,43 @@
+// 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>
+
+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) :
+
+ // 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;
+}

0 comments on commit 75e1d26

Please sign in to comment.