Created
February 1, 2012 09:40
-
-
Save spikebrehm/1716171 to your computer and use it in GitHub Desktop.
Serving mustache templates using Node and jsdom
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 http = require('http'), | |
fs = require('fs'), | |
jsdom = require('jsdom'), | |
mustache = require('mustache'); | |
var global = this; | |
var trips = [ | |
{ | |
description: 'foobar' | |
}, | |
{ | |
description: 'asdfsdfasd adsf fsd f sd fds f sd' | |
} | |
]; | |
var templates = { | |
app_view: fs.readFileSync('templates/app_view.mustache','utf-8') | |
}; | |
http.createServer(function(request, response){ | |
global.request = request; | |
global.response = response; | |
renderPage(); | |
}).listen(8078); | |
function renderPage(){ | |
jsdom.env(templates.app_view, ['./jquery.js'], function(errors, window){ | |
var $ = window.$, | |
html = mustache.to_html(templates.app_view, {trips: trips}); | |
console.log(html) | |
$('html').html(html) | |
$('#trips').bind('click', function(e){ | |
console.log(e) | |
}); | |
global.response.writeHead(200, {'Content-Type': 'text/html'}); | |
global.response.end("<!DOCTYPE html>\n" + $('html').html()); | |
console.log('sent!') | |
}); | |
} | |
console.log('running') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment