Skip to content

Instantly share code, notes, and snippets.

@PastorBones
Created January 11, 2012 06:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save PastorBones/1593359 to your computer and use it in GitHub Desktop.
Save PastorBones/1593359 to your computer and use it in GitHub Desktop.
NodeJS & Express easy page builder
var express = require('express')
, app = express.createServer()
// Global stylesheets, scripts, keywords
var Page = require('Page')({
styles: ['global']
, scripts: ['/scripts/main.js']
, keywords: ['test','nodejs','express']
})
app.get('/', function(req, res){
Page.init(res, {
title: 'Home Page'
, styles: ['form']
}).build()
res.render('index')
})
app.get('/test', function(req, res){
Page.init(res)
// Do some stuff
Page.addStyle('test')
// Do more stuff
Page.build()
res.render('test')
})
app.listen(3000)
!!!
html
head
- each style in styles
link(rel='stylesheet',type='text/css', href= style)
- each script in scripts
script(type='text/javascript', src= script)
- if(keywords.length > 0)
meta(name='keywords', content= keywords.join(', '))
- if(pageTitle)
title= pageTitle
body!= body
var Page = function(opt){
var self = this
self.global = {
styles: []
, scripts: []
, keywords: []
}
self.init = function(res, opt){
self.res = res
self.local = {
styles: []
, scripts: []
, keywords: []
}
if(typeof(opt) === 'undefined'){
opt = {title: ''}
}
if(typeof(opt.title) !== 'undefined'){
res.app.settings['view options'].pageTitle = opt.title
} else {
res.app.settings['view options'].pageTitle = ''
}
if(typeof(opt.scripts) !== 'undefined'){
self.addScripts(opt.scripts)
}
if(typeof(opt.styles) !== 'undefined'){
self.addStyles(opt.styles)
}
if(typeof(opt.keywords) !== 'undefined'){
self.addKeywords(opt.keywords)
}
return self
}
self.setPageTitle = function(title){
return this.res.app.settings['view options'].pageTitle = title
}
self.add = function(type, file, g){
if(g) {
self.global[type].push(file)
} else {
self.local[type].push(file)
}
}
self.addStyles = function(styles, g){
if(!g) { g = null }
styles.forEach(function(style){
self.addStyle(style, g)
})
return self
}
self.addStyle = function(style, g){
self.add('styles', '/stylesheets/' + style + '.css', g)
return self
}
self.addScripts = function(scripts, g){
if(!g) { g = null }
scripts.forEach(function(script){
self.addScript(script, g)
})
return self
}
self.addScript = function(script, g){
self.add('scripts', script, g)
return self
}
self.addKeywords = function(words, g){
if(!g) { g = null }
words.forEach(function(word){
self.addKeyword(word, g)
})
return self
}
self.addKeyword = function(word, g){
self.add('keywords', word, g)
return self
}
self.build = function(res){
var styles = self.global.styles.concat(self.local.styles)
, scripts = self.global.scripts.concat(self.local.scripts)
, keywords = self.global.keywords.concat(self.local.keywords)
self.res.app.settings['view options'].styles = styles
self.res.app.settings['view options'].scripts = scripts
self.res.app.settings['view options'].keywords = keywords
}
Object.keys(opt).forEach(function(type){
var handle = 'add' + type[0].toUpperCase() + type.slice(1, type.length)
self[handle](opt[type], true)
})
return self
}
module.exports = function(opt){
return new Page(opt)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment