From 7dadd20fa765b34c0da2cdeba52da7d4cc139aae Mon Sep 17 00:00:00 2001 From: Benjamin Kiessling Date: Fri, 27 Jan 2012 03:13:47 +0100 Subject: Use a swig as a templating system --- config.js | 4 ++- frontend.js | 101 ++++++++++++++++++++++++++++-------------------------------- main.js | 2 +- 3 files changed, 51 insertions(+), 56 deletions(-) diff --git a/config.js b/config.js index af59e85..d0ea917 100644 --- a/config.js +++ b/config.js @@ -1,8 +1,10 @@ exports.modelBackend = 'fs'; exports.Config = { rootDir: 'slideshows' , + listTemplate: 'index.html', templateFile: 'template.html', descriptionFile: 'desc.js', eventFile: 'events.js', - ignoreDotFiles: 'false' + ignoreDotFiles: 'false', + port: 8080 }; 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 +'
  • '; - } - if(slides['slides'][slide]['head'].length > 0) { - var head = slides['slides'][slide]['head']; - slideStr = slideStr+'
    '+head+'
    '; - if(slides['slides'][slide]['text'].length > 0) { - var text = slides['slides'][slide]['text']; - slideStr = slideStr+'
    '+text+'
    '; - } - slideStr = slideStr + '
    '; + 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 + '
  • \n'; } - var eventStr = ''; - for(var date in slides['events']) { - if(!slides['events'].hasOwnProperty(date)) { continue; } - eventStr = eventStr + '
    '+date+'
    '; + 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 = '

    Slideshows:

    '; - for(var slideset in slidesets) { - if(!slidesets.hasOwnProperty(slideset)) { continue; } - body = body + ''+slidesets[slideset]+'
    '; - } - 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); }; diff --git a/main.js b/main.js index 8ba061e..f52d1ad 100644 --- a/main.js +++ b/main.js @@ -10,7 +10,7 @@ process.chdir(__dirname); var config = require('./config'); var express = require('express'); -var app = express.createServer(); +var app = express(); app.use(express.logger()); app.use(express.staticCache()); -- cgit v1.2.1