Permalink
Browse files

add test for layoutsDir; bump version

  • Loading branch information...
mgutz committed Jan 16, 2013
1 parent d598656 commit 1c5c222c419109dbc2bfcb7b3874757f57ed973a
View
@@ -0,0 +1,59 @@
+// npm install express express-hbs
+
+var express = require('express');
+var app = express();
+var hbs = require('..'); // should be `require('express-hbs')` outside of this example
+
+app.use(express.static(__dirname + '/public'));
+
+// Hook in express-hbs and tell it where known directories reside
+app.engine('hbs', hbs.express3({
+ partialsDir: __dirname + '/views/partials',
+ layoutsDir: __dirname + '/views/layout',
+ defaultLayout: __dirname + '/views/layout/default.hbs'
+}));
+app.set('view engine', 'hbs');
+app.set('views', __dirname + '/views');
+
+var fruits = [
+ {name: 'apple'},
+ {name: 'orange'},
+ {name: 'pear'}
+];
+
+
+var veggies = [
+ {name: 'asparagus'},
+ {name: 'carrot'},
+ {name: 'spinach'}
+];
+
+app.get('/', function(req, res) {
+ res.render('index', {
+ title: 'express-hbs example'
+ });
+});
+
+app.get('/fruits', function(req, res) {
+ res.render('fruits/index-layoutsDir', {
+ title: 'My favorite fruits',
+ fruits: fruits
+ });
+});
+
+app.get('/veggies', function(req, res) {
+ res.render('veggies', {
+ title: 'My favorite veggies',
+ veggies: veggies,
+ layout: 'layout/veggie'
+ });
+});
+
+
+if (require.main === module) {
+ app.listen(3000);
+ console.log('Express server listening on port 3000');
+}
+else {
+ module.exports = app;
+}
View
@@ -6,10 +6,9 @@ var hbs = require('..'); // should be `require('express-hbs')` outside of this e
app.use(express.static(__dirname + '/public'));
-// Hook in express-hbs and tell it where partials are found
+// Hook in express-hbs and tell it where known directories reside
app.engine('hbs', hbs.express3({
partialsDir: __dirname + '/views/partials',
- layoutsDir: __dirname + '/views/layout',
defaultLayout: __dirname + '/views/layout/default.hbs'
}));
app.set('view engine', 'hbs');
@@ -0,0 +1,18 @@
+<!-- name of layout -->
+{{!< fruits}}
+
+{{#contentFor "pageStyles"}}
+<style>
+ li {
+ color: #800;
+ }
+</style>
+{{/contentFor}}
+
+
+<h1>{{title}}</h1>
+<ul>
+ {{#each fruits}}
+ <li>{{this.name}}</li>
+ {{/each}}
+</ul>
@@ -1,5 +1,5 @@
<!-- name of layout -->
-{{!< fruits}}
+{{!< ../layout/fruits}}
{{#contentFor "pageStyles"}}
<style>
View
@@ -129,7 +129,7 @@ function cachePartials() {
/**
* Express 3.x template engine compliance.
*
- * @param {Object} options - {
+ * @param {Object} options = {
* handlebars: "override handlebars",
* defaultLayout: "path to default layout",
* partialsDir: "absolute path to partials",
@@ -287,7 +287,7 @@ var _express3 = function(filename, options, cb) {
if (err) return cb(err);
renderIt(layoutTemplate);
});
-
+
} else {
// if the value is falsey, behave as if no layout should be used - suppress defaults
renderIt(null);
View
@@ -1,6 +1,6 @@
{
"name": "express-hbs",
- "version": "0.1.2-pre",
+ "version": "0.1.3-pre",
"description": "Express 3 handlebars template engine complete with multple layouts, partials and blocks.",
"keywords": "express3 express handlebars layout partials",
"main": "index.js",
View
@@ -1,9 +1,10 @@
var request = require('supertest');
-var app = require('../example/app');
+var app;
describe('express-hbs', function() {
- describe('File provider', function() {
+ describe('defaults', function() {
+ app = require('../example/app');
it('should render using default layout', function(done) {
request(app)
@@ -43,4 +44,14 @@ describe('express-hbs', function() {
});
+ describe('layoutsDir', function() {
+ var app = require('../example/app-layoutsDir');
+
+ it('should render layout declared in markup', function(done) {
+ request(app)
+ .get('/fruits')
+ .expect(/DECLARATIVE LAYOUT/, done);
+ });
+ });
+
});

0 comments on commit 1c5c222

Please sign in to comment.