Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- jade = require 'jade'
- dive = require 'diveSync'
- compiledTemplates = ''
- compileTemplates = (callback) ->
- compiledTemplates = 'window.templates = {};'
- baseDirectory = __dirname + '/templates'
- dive baseDirectory, (err, file) ->
- if err
- throw err
- if path.extname(file) == '.jade'
- name = file.replace(/\//g, '.')
- name = name.substring( 0, name.length - '.jade'.length )
- name = name.substring( baseDirectory.length + 1 )
- compiledTemplates += 'templates.' + name + ' = ' + jade.compile( fs.readFileSync( file, 'utf-8' ), { client: true, compileDebug: false } ).toString() + ';'
- # Compile the templates once on startup
- compileTemplates()
- app.get '/javascripts/jade.js', (req, res) ->
- # Serve the minified Jade runtime to the client
- fs.readFile __dirname + '/node_modules/jade/runtime.min.js', 'utf-8', (err, data) ->
- res.writeHead 200, 'Content-Type': 'text/javascript'
- res.end data
- app.get '/javascripts/templates.js', (req, res) ->
- # If we aren't in production, recompile the templates to ensure they are up to date
- if !process.env.PRODUCTION
- compileTemplates()
- res.writeHead 200, 'Content-Type': 'text/javascript'
- res.end compiledTemplates
Advertisement
Add Comment
Please, Sign In to add comment