Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

add test for layoutsDir; bump version

  • Loading branch information...
commit 1c5c222c419109dbc2bfcb7b3874757f57ed973a 1 parent d598656
Mario Gutierrez authored
59  example/app-layoutsDir.js
... ...
@@ -0,0 +1,59 @@
  1
+// npm install express express-hbs
  2
+
  3
+var express = require('express');
  4
+var app = express();
  5
+var hbs = require('..'); // should be `require('express-hbs')` outside of this example
  6
+
  7
+app.use(express.static(__dirname + '/public'));
  8
+
  9
+// Hook in express-hbs and tell it where known directories reside
  10
+app.engine('hbs', hbs.express3({
  11
+    partialsDir: __dirname + '/views/partials',
  12
+    layoutsDir: __dirname + '/views/layout',
  13
+    defaultLayout: __dirname + '/views/layout/default.hbs'
  14
+}));
  15
+app.set('view engine', 'hbs');
  16
+app.set('views', __dirname + '/views');
  17
+
  18
+var fruits = [
  19
+  {name: 'apple'},
  20
+  {name: 'orange'},
  21
+  {name: 'pear'}
  22
+];
  23
+
  24
+
  25
+var veggies = [
  26
+  {name: 'asparagus'},
  27
+  {name: 'carrot'},
  28
+  {name: 'spinach'}
  29
+];
  30
+
  31
+app.get('/', function(req, res) {
  32
+  res.render('index', {
  33
+    title: 'express-hbs example'
  34
+  });
  35
+});
  36
+
  37
+app.get('/fruits', function(req, res) {
  38
+  res.render('fruits/index-layoutsDir', {
  39
+    title: 'My favorite fruits',
  40
+    fruits: fruits
  41
+  });
  42
+});
  43
+
  44
+app.get('/veggies', function(req, res) {
  45
+  res.render('veggies', {
  46
+    title: 'My favorite veggies',
  47
+    veggies: veggies,
  48
+    layout: 'layout/veggie'
  49
+  });
  50
+});
  51
+
  52
+
  53
+if (require.main === module) {
  54
+  app.listen(3000);
  55
+  console.log('Express server listening on port 3000');
  56
+}
  57
+else {
  58
+  module.exports = app;
  59
+}
3  example/app.js
@@ -6,10 +6,9 @@ var hbs = require('..'); // should be `require('express-hbs')` outside of this e
6 6
 
7 7
 app.use(express.static(__dirname + '/public'));
8 8
 
9  
-// Hook in express-hbs and tell it where partials are found
  9
+// Hook in express-hbs and tell it where known directories reside
10 10
 app.engine('hbs', hbs.express3({
11 11
     partialsDir: __dirname + '/views/partials',
12  
-    layoutsDir: __dirname + '/views/layout',
13 12
     defaultLayout: __dirname + '/views/layout/default.hbs'
14 13
 }));
15 14
 app.set('view engine', 'hbs');
18  example/views/fruits/index-layoutsDir.hbs
... ...
@@ -0,0 +1,18 @@
  1
+<!-- name of layout -->
  2
+{{!< fruits}}
  3
+
  4
+{{#contentFor "pageStyles"}}
  5
+<style>
  6
+  li {
  7
+    color: #800;
  8
+  }
  9
+</style>
  10
+{{/contentFor}}
  11
+
  12
+
  13
+<h1>{{title}}</h1>
  14
+<ul>
  15
+    {{#each fruits}}
  16
+    <li>{{this.name}}</li>
  17
+    {{/each}}
  18
+</ul>
2  example/views/fruits/index.hbs
... ...
@@ -1,5 +1,5 @@
1 1
 <!-- name of layout -->
2  
-{{!< fruits}}
  2
+{{!< ../layout/fruits}}
3 3
 
4 4
 {{#contentFor "pageStyles"}}
5 5
 <style>
4  lib/hbs.js
@@ -129,7 +129,7 @@ function cachePartials() {
129 129
 /**
130 130
  * Express 3.x template engine compliance.
131 131
  *
132  
- * @param {Object} options - {
  132
+ * @param {Object} options = {
133 133
  *   handlebars: "override handlebars",
134 134
  *   defaultLayout: "path to default layout",
135 135
  *   partialsDir: "absolute path to partials",
@@ -287,7 +287,7 @@ var _express3 = function(filename, options, cb) {
287 287
               if (err) return cb(err);
288 288
               renderIt(layoutTemplate);
289 289
             });
290  
-            
  290
+
291 291
           } else {
292 292
             // if the value is falsey, behave as if no layout should be used - suppress defaults
293 293
             renderIt(null);
2  package.json
... ...
@@ -1,6 +1,6 @@
1 1
 {
2 2
   "name": "express-hbs",
3  
-  "version": "0.1.2-pre",
  3
+  "version": "0.1.3-pre",
4 4
   "description": "Express 3 handlebars template engine complete with multple layouts, partials and blocks.",
5 5
   "keywords": "express3 express handlebars layout partials",
6 6
   "main": "index.js",
15  tests/handlebarsSpecs.js
... ...
@@ -1,9 +1,10 @@
1 1
 var request = require('supertest');
2  
-var app = require('../example/app');
  2
+var app;
3 3
 
4 4
 describe('express-hbs', function() {
5 5
 
6  
-  describe('File provider', function() {
  6
+  describe('defaults', function() {
  7
+    app = require('../example/app');
7 8
 
8 9
     it('should render using default layout', function(done) {
9 10
       request(app)
@@ -43,4 +44,14 @@ describe('express-hbs', function() {
43 44
 
44 45
   });
45 46
 
  47
+  describe('layoutsDir', function() {
  48
+    var app = require('../example/app-layoutsDir');
  49
+
  50
+    it('should render layout declared in markup', function(done) {
  51
+      request(app)
  52
+        .get('/fruits')
  53
+        .expect(/DECLARATIVE LAYOUT/, done);
  54
+    });
  55
+  });
  56
+
46 57
 });

0 notes on commit 1c5c222

Please sign in to comment.
Something went wrong with that request. Please try again.