Permalink
Browse files

updated to node 0.1.100 and changed the defaultContexts keys to have …

…less weight than the manually entered data
  • Loading branch information...
1 parent 3812ddb commit 72c583d5bf29fdaf553b098d639a46fda53f5843 @Rixius Rixius committed with Jul 21, 2010
Showing with 31 additions and 28 deletions.
  1. +3 −2 README.markdown
  2. +28 −26 tmpl-node.js
View
@@ -1,6 +1,7 @@
tmpl-node.js
============
+NOTE2: This library has been updated to be compatible with the latest nodejs(0.1.100);
NOTE: This library is out of date with the current node.js API. I'm no longer using it, so please fork n' fix, and I'll pull the changes!
`tmpl-node` is a template module for [node.js](http://nodejs.org/), based on John Resig's approach to [micro-templating](http://ejohn.org/blog/javascript-micro-templating/). It's a lightweight way to add templating to any `node.js` app.
@@ -24,9 +25,9 @@ You can render templates with multiple contexts, which will all be combined into
You can also load templates in batch from a folder like this:
- tmpl.load("./myTemplates/")
+ tmpl.load("./myTemplates/", function(tmpl){})
-which creates a named template for each file. So if the folder had a file named `page.html`, you could render its contents like this:
+which creates a named template for each file, and takes a callback, so you can ensure everything is loaded properly. So if the folder had a file named `page.html`, you could render its contents like this:
tmpl[ "page.html" ]({ title: "Welcome!" })
View
@@ -1,19 +1,20 @@
// tmpl-node: a template module for node.js
-// Jed Schmidt - http://jedschmidt.com/
+// Jed Schmidt -
//
// inspired by John Resig's micro templates
// http://ejohn.org/blog/javascript-micro-templating/
-var posix = require( "posix" ),
+var fs = require( "fs" ),
http = require( "http" ),
concat = Array.prototype.concat,
- slice = Array.prototype.slice;
+ slice = Array.prototype.slice,
+ sys = require('sys');
+
+
+exports.compile = compile;
+exports.load = load;
+exports.defaultContexts = [];
-process.mixin( exports, {
- compile: compile,
- load: load,
- defaultContexts: []
-});
http.ServerResponse.prototype.render = function( name, c1, c2, etc ) {
var args = Array.prototype.slice.call( arguments ),
@@ -38,6 +39,7 @@ function compile( str, name ) {
function ret() {
var args = concat.apply( exports.defaultContexts, arguments ),
+ x = -1,
i = args.length,
context = {},
name,
@@ -46,8 +48,8 @@ function compile( str, name ) {
if ( i < 2 )
return fn( args[0] || context );
- while ( i )
- for ( name in ( cur = args[ --i ] ) )
+ while ( x<i )
+ for ( name in ( cur = args[ ++x ] ) )
context[ name ] = cur[ name ];
return fn( context );
@@ -59,22 +61,22 @@ function compile( str, name ) {
return ret;
};
-function load( dir, pattern ) {
- var promise = new process.Promise();
-
- posix.readdir( dir ).addCallback( function( files ) {
- var count = files.length;
- files.forEach( function( name ) {
- if ( pattern && !pattern.test( name ) )
- return --count;
+function load( dir, call ) {
+ var foo = {};
+ dir = dir.split('');
+ ( dir[dir.length-1] === '/' )||( dir.push('/') );
+ dir = dir.join('');
+
+ fs.readdir(dir,function(err,dat){
+ var x;
- posix.cat( dir + name ).addCallback( function( contents ) {
- compile( contents, name );
- if ( !--count )
- promise.emitSuccess();
- })
- });
+ (function walker(){
+ x = dat.pop();
+ fs.readFile(dir + x, function(e,data){
+ exports[x] = compile(data.toString());
+ ( ( dat.length ) && ( walker() ) ) || ( call(exports) );
+ });
+ return true;
+ }());
});
-
- return promise;
};

0 comments on commit 72c583d

Please sign in to comment.