diff options
author | Benjamin Kiessling <mittagessen@l.unchti.me> | 2012-01-27 03:13:47 +0100 |
---|---|---|
committer | Benjamin Kiessling <mittagessen@l.unchti.me> | 2012-01-27 03:13:47 +0100 |
commit | 7dadd20fa765b34c0da2cdeba52da7d4cc139aae (patch) | |
tree | f3a78c22e2b4ad97a1254c2d1d664606d857bc9d /frontend.js | |
parent | 84da8d4b9bfab520a1bc0844773334b7131794c4 (diff) |
Use a swig as a templating system
Diffstat (limited to 'frontend.js')
-rw-r--r-- | frontend.js | 101 |
1 files changed, 47 insertions, 54 deletions
diff --git a/frontend.js b/frontend.js index 8c3d4d6..ee811cd 100644 --- a/frontend.js +++ b/frontend.js @@ -1,7 +1,6 @@ var path = require('path'); var fs = require('fs'); -var sanitize =require('validator').sanitize; - +var cons = require('consolidate'); var model; exports.setModel = function(m) { @@ -9,70 +8,64 @@ exports.setModel = function(m) { }; exports.start = function(config) { - config.app.get('/slides/:id', function(req, res) { - var slideset; + var app = config.app; + app.engine('html', cons.swig); + + // set .html as the default extension + app.set('view engine', 'html'); + app.set('views', path.join(__dirname, config.rootDir)); + + app.get('/slides/:id', function(req, res) { slideset = req.params.id; model.getSlides(slideset, function(err, slides) { if(err) { throw err; } - fs.readFile(path.join(__dirname, config.rootDir, slides['template']) , function(err,data) { - if(err) { res.end(body); } - var body = data.toString(); - var slideStr = ''; - for(var slide in slides['slides']) { - if(!slides['slides'].hasOwnProperty(slide)) { continue; } - var media = sanitize(slides['slides'][slide]['file']).entityEncode(); - if(slides['slides'][slide]['type'] === 'image') { - slideStr = slideStr +'<li><img src="/'+media+'">'; - } - if(slides['slides'][slide]['head'].length > 0) { - var head = slides['slides'][slide]['head']; - slideStr = slideStr+'<div class="slideDesc"><div class="slideHead">'+head+'</div>'; - if(slides['slides'][slide]['text'].length > 0) { - var text = slides['slides'][slide]['text']; - slideStr = slideStr+'<div class="slideText">'+text+'</div>'; - } - slideStr = slideStr + '</div>'; + var slidesAr = []; + for(var slide in slides['slides']) { + if(!slides['slides'].hasOwnProperty(slide)) { continue; } + if(slides['slides'][slide]['type'] === 'image') { + slidesAr.push({ + image: slides['slides'][slide]['file'] + }); + } + if(slides['slides'][slide]['head'].length > 0) { + slidesAr[slidesAr.length - 1]['description'] = slides['slides'][slide]['head']; + if(slides['slides'][slide]['text'].length > 0) { + slidesAr[slidesAr.length - 1]['text'] = slides['slides'][slide]['text']; } - slideStr = slideStr + '</img></li>\n'; } - var eventStr = ''; - for(var date in slides['events']) { - if(!slides['events'].hasOwnProperty(date)) { continue; } - eventStr = eventStr + '<div class="event"><div class="eventDate">'+date+'</div><ul class="eventList">'; - for(var ev in slides['events'][date]) { - if(!slides['events'][date].hasOwnProperty(ev)) { continue; } - eventStr = eventStr + '<li class="eventEl">'; - for(var evF in slides['events'][date][ev]) { - if(!slides['events'][date][ev].hasOwnProperty(evF)) { continue; } - if(typeof slides['events'][date][ev][evF] == 'string') { - var evClass = evF; - var evField = sanitize(slides['events'][date][ev][evF]).entityEncode(); - var evField = evField.replace(/\\n/g, '<br/>'); - eventStr = eventStr + '<div class="eventField ' + evClass + '">' + evField + '</div>'; - } + } + var eventAr = []; + for(var date in slides['events']) { + if(!slides['events'].hasOwnProperty(date)) { continue; } + eventAr.push({ + date: date, + events: [] + }); + for(var ev in slides['events'][date]) { + if(!slides['events'][date].hasOwnProperty(ev)) { continue; } + var eventObj = {}; + for(var evF in slides['events'][date][ev]) { + if(!slides['events'][date][ev].hasOwnProperty(evF)) { continue; } + if(typeof slides['events'][date][ev][evF] == 'string') { + eventObj[evF] = slides['events'][date][ev][evF]; } - eventStr = eventStr + '</li>'; } - eventStr = eventStr + '</ul></div>'; + console.log(eventObj); + eventAr[eventAr.length - 1]['events'].push(eventObj); } - body = body.replace("$IMAGES", slideStr); - body = body.replace("$EVENTS", eventStr); - res.setHeader('Content-Length', body.length); - res.end(body); + } + res.render(path.join(slideset, config.templateFile), { + slides: slidesAr, + events: eventAr }); }); }); - config.app.get('*', function(req, res) { + app.get('*', function(req, res){ model.getAllSlides(function(slidesets) { - var body = '<head></head><body><h3>Slideshows:</h3>'; - for(var slideset in slidesets) { - if(!slidesets.hasOwnProperty(slideset)) { continue; } - body = body + '<a href="/slides/' + slidesets[slideset] + '">'+slidesets[slideset]+'</a><br/>'; - } - body = body + '</body>'; - res.setHeader('Content-Length', body.length); - res.end(body); + res.render(config.listTemplate, { + slides: slidesets + }); }); }); - config.app.listen(8080); + app.listen(config.port); }; |