Permalink
Browse files

Moved templating to ejs.

  • Loading branch information...
1 parent 7d7f060 commit 5cbbf44e22c48fd34a70045817abbda428f7f4a4 @twentyrogersc committed May 15, 2012
Showing with 20 additions and 12 deletions.
  1. +15 −8 index.js
  2. +2 −2 package.json
  3. +2 −1 test/teller.js
  4. +1 −0 test/template.ejs
  5. +0 −1 test/template.html
View
@@ -1,28 +1,35 @@
var crossroads = require('crossroads')
+var ejs = require('ejs')
var formidable = require('formidable')
-var mu = require('mu2')
+var fs = require('fs')
var http = require('http')
var qs = require('querystring')
var url = require('url')
-var util = require('util')
var app = {}
+var dir = '/'
var routes = {
get: crossroads.create(),
post: crossroads.create()
}
-app.setTemplateDir = function(dir) {
- mu.root = dir
+app.setTemplateDir = function(newDir) {
+ dir = newDir
return app
}
var render = function(template, data, code) {
- var head = { 'Content-Type': 'text/html' }
- this.writeHead(code === undefined ? 200 : code, head)
- var stream = mu.compileAndRender(template, data)
- util.pump(stream, this)
+ var res = this
+ template = [dir, template].join('/')
+ fs.readFile(template, function (err, buff) {
+ if (err) throw err
+ var html = ejs.render(buff.toString(), data)
+ code = code === undefined ? 200 : code
+ var head = { 'Content-Type': 'text/html' }
+ res.writeHead(code, head)
+ res.end(html)
+ })
}
var json = function(obj, code) {
View
@@ -6,8 +6,8 @@
"author" : "Craig Rogers <hello@crai.gr>",
"dependencies": {
"crossroads": "0.8.0",
- "formidable": "1.0.9",
- "mu2": "0.5.13"
+ "ejs": "0.7.1",
+ "formidable": "1.0.9"
},
"devDependencies": {
"mocha": "*",
View
@@ -16,7 +16,7 @@ require('../index')
})
.get('/render', function(req, res) {
var data = { title: req.query.code }
- res.render('template.html', data, req.query.code)
+ res.render('template.ejs', data, req.query.code)
})
.get('/redirect', function(req, res) {
res.redirect('/render', req.query.code)
@@ -33,6 +33,7 @@ require('../index')
.setTemplateDir(__dirname)
.listen(1234)
+
describe('app', function() {
describe('get()', function() {
View
@@ -0,0 +1 @@
+<h1><%= title %></h1>
View
@@ -1 +0,0 @@
-<h1>{{title}}</h1>

0 comments on commit 5cbbf44

Please sign in to comment.