Created
September 8, 2011 05:54
-
-
Save FLYBYME/1202729 to your computer and use it in GitHub Desktop.
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
var build = function(buildFn, name, html, callBack) { | |
this.name = name; | |
this.html = html; | |
this.buildFn = buildFn; | |
this.callBack = callBack; | |
buildFn.apply(this, [this]) | |
} | |
build.prototype.render = function(name, callBack) { | |
return t.tmpl(name, callBack) | |
} | |
build.prototype.end = function(data) { | |
return this.callBack(data) | |
} | |
var tmpl = function() { | |
this.jqtpl = require("jqtpl"); | |
this.loadedTmpl = {} | |
} | |
tmpl.prototype.template = function(tmplate) { | |
var html = tmplate.html.join(''); | |
var name = tmplate.name; | |
var buildFn = tmplate.build; | |
var tmpl = this.jqtpl.template(name, html); | |
this.loadedTmpl[name] = { | |
name : name, | |
html : html, | |
buildFn : buildFn | |
} | |
} | |
tmpl.prototype._build = function(buildFn, name, html, callBack) { | |
var self = this; | |
new build(buildFn, name, html, function(data) { | |
callBack(self.jqtpl.tmpl(name, data)) | |
}) | |
} | |
tmpl.prototype.tmpl = function(name, callBack) { | |
if(this.loadedTmpl.hasOwnProperty(name)) { | |
var loaded = this.loadedTmpl[name]; | |
this._build(loaded.buildFn, loaded.name, loaded.html, callBack) | |
} | |
} | |
var t = new tmpl | |
var bodtTmpl = { | |
name : 'body-tmpl', | |
html : ['<html><body><div>', '${title}', '</div>', '<div>${header}</div>', '<div>${content}</div>', '<div>${footer}</div></html></body>'], | |
build : function(tmpl) { | |
tmpl.render('header-tmpl', function(headerHtml) { | |
tmpl.render('content-tmpl', function(contentHtml) { | |
tmpl.render('footer-tmpl', function(footerHtml) { | |
tmpl.end({ | |
title : 'something really good!', | |
header : headerHtml, | |
content : contentHtml, | |
footer : footerHtml | |
}) | |
}) | |
}) | |
}) | |
} | |
} | |
var headerTmpl = { | |
name : 'header-tmpl', | |
html : ['<div>', '${title}', '</div>'], | |
build : function(tmpl) { | |
tmpl.end({ | |
title : 'something really good!' | |
}) | |
} | |
} | |
var contentTmpl = { | |
name : 'content-tmpl', | |
html : ['<div>', '${title}', '</div>'], | |
build : function(tmpl) { | |
tmpl.end({ | |
title : 'something really good!' | |
}) | |
} | |
} | |
var footerTmpl = { | |
name : 'footer-tmpl', | |
html : ['<div>', '${title}', '</div>'], | |
build : function(tmpl) { | |
tmpl.end({ | |
title : 'something really good!' | |
}) | |
} | |
} | |
t.template(footerTmpl) | |
t.template(contentTmpl) | |
t.template(headerTmpl) | |
t.template(bodtTmpl) | |
t.tmpl('body-tmpl', function(html) { | |
console.log(html) | |
}) | |
var http = require('http'); | |
http.createServer(function(request, response) { | |
response.writeHead(200, { | |
'Content-Type' : 'text/html' | |
}); | |
t.tmpl('body-tmpl', function(html) { | |
console.log(html) | |
response.end(html); | |
}) | |
}).listen(80); | |
console.log('Server running at http://127.0.0.1:8124/'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment