express-hbs 2.5.0 → 3.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -9,50 +9,50 @@ var url = require('url');
9
9
  var pages = [
10
10
  {
11
11
  id: 1,
12
- title: 'Title 1'
12
+ title: 'Title 1',
13
13
  },
14
14
  {
15
15
  id: 2,
16
- title: 'Title 2'
16
+ title: 'Title 2',
17
17
  },
18
18
  {
19
19
  id: 3,
20
- title: 'Title 3'
21
- }
22
- ]
20
+ title: 'Title 3',
21
+ },
22
+ ];
23
23
 
24
24
  var comments = [
25
25
  {
26
26
  id: 1,
27
27
  page: 1,
28
28
  subject: 'Title 1 Comment 1',
29
- auther: 'JT'
29
+ auther: 'JT',
30
30
  },
31
31
  {
32
32
  id: 2,
33
33
  page: 1,
34
34
  subject: 'Title 1 Comment 2',
35
- auther: 'Anna'
35
+ auther: 'Anna',
36
36
  },
37
37
  {
38
38
  id: 3,
39
39
  page: 1,
40
40
  subject: 'Title 1 Comment 3',
41
- auther: 'Jane'
41
+ auther: 'Jane',
42
42
  },
43
43
  {
44
44
  id: 4,
45
45
  page: 1,
46
46
  subject: 'Title 1 Comment 4',
47
- auther: 'Bob'
47
+ auther: 'Bob',
48
48
  },
49
49
  {
50
50
  id: 5,
51
51
  page: 4,
52
52
  subject: 'This should not show!',
53
- auther: 'Jill'
54
- }
55
- ]
53
+ auther: 'Jill',
54
+ },
55
+ ];
56
56
 
57
57
  function getRandomNumber(min, max) {
58
58
  return Math.random() * (max - min) + min;
@@ -64,10 +64,13 @@ function create(hbs, env) {
64
64
  var viewsDir = path.join(__dirname, 'views');
65
65
 
66
66
  // Hook in express-hbs and tell it where known directories reside
67
- app.engine('hbs', hbs.express4({
68
- defaultLayout: path.join(viewsDir, 'layout.hbs'),
69
- restrictLayoutsTo: viewsDir
70
- }));
67
+ app.engine(
68
+ 'hbs',
69
+ hbs.express4({
70
+ defaultLayout: path.join(viewsDir, 'layout.hbs'),
71
+ restrictLayoutsTo: viewsDir,
72
+ }),
73
+ );
71
74
  app.set('view engine', 'hbs');
72
75
  app.set('views', viewsDir);
73
76
 
@@ -76,7 +79,7 @@ function create(hbs, env) {
76
79
  app.get('/', function (req, res) {
77
80
  res.render('index', {
78
81
  message: 'Hello,',
79
- username: req.cookies.user
82
+ username: req.cookies.user,
80
83
  });
81
84
  });
82
85
 
@@ -84,42 +87,51 @@ function create(hbs, env) {
84
87
  res.render('failer');
85
88
  });
86
89
 
87
- hbs.registerAsyncHelper('user', function(username, resultcb) {
88
- setTimeout(function() {
89
- resultcb(username);
90
- }, getRandomNumber(100, 900))
90
+ hbs.registerAsyncHelper('user', function (username, resultcb) {
91
+ setTimeout(
92
+ function () {
93
+ resultcb(username);
94
+ },
95
+ getRandomNumber(100, 900),
96
+ );
91
97
  });
92
98
 
93
- hbs.registerAsyncHelper('pages', function(options, resultcb) {
99
+ hbs.registerAsyncHelper('pages', function (options, resultcb) {
94
100
  var self = this;
95
- setTimeout(function() {
96
- var result = [];
97
- for(var i = 0; i < pages.length; i++) {
98
- options.data.page = pages[i];
99
- result.push(options.fn.call(self, pages[i], options));
100
- }
101
- resultcb(result.join(''));
102
- }, getRandomNumber(100, 900))
101
+ setTimeout(
102
+ function () {
103
+ var result = [];
104
+ for (var i = 0; i < pages.length; i++) {
105
+ options.data.page = pages[i];
106
+ result.push(options.fn.call(self, pages[i], options));
107
+ }
108
+ resultcb(result.join(''));
109
+ },
110
+ getRandomNumber(100, 900),
111
+ );
103
112
  });
104
113
 
105
- hbs.registerAsyncHelper('comments', function(options, resultcb) {
114
+ hbs.registerAsyncHelper('comments', function (options, resultcb) {
106
115
  var self = this;
107
- setTimeout(function() {
108
- var result = [];
109
- for(var i = 0; i < comments.length; i++) {
110
- if (options.hash.page === comments[i].page) {
111
- result.push(options.fn(comments[i]));
116
+ setTimeout(
117
+ function () {
118
+ var result = [];
119
+ for (var i = 0; i < comments.length; i++) {
120
+ if (options.hash.page === comments[i].page) {
121
+ result.push(options.fn(comments[i]));
122
+ }
112
123
  }
113
- }
114
- resultcb(result.join(''));
115
- }, getRandomNumber(100, 300))
124
+ resultcb(result.join(''));
125
+ },
126
+ getRandomNumber(100, 300),
127
+ );
116
128
  });
117
129
 
118
- hbs.registerAsyncHelper('failer', function(_, resultcb) {
119
- setTimeout(function() {
130
+ hbs.registerAsyncHelper('failer', function (_, resultcb) {
131
+ setTimeout(function () {
120
132
  resultcb(options.fn());
121
133
  }, 100);
122
- })
134
+ });
123
135
 
124
136
  return app;
125
137
  }
@@ -7,7 +7,6 @@
7
7
  *
8
8
  */
9
9
 
10
-
11
10
  function create(hbs, env) {
12
11
  'use strict';
13
12
 
@@ -26,14 +25,17 @@ function create(hbs, env) {
26
25
  i18n.configure({
27
26
  locales: ['en', 'fr'],
28
27
  cookie: 'locale',
29
- directory: __dirname + '/locales'
28
+ directory: __dirname + '/locales',
30
29
  });
31
30
 
32
31
  // Hook in express-hbs and tell it where known directories reside
33
- app.engine('hbs', hbs.express3({
34
- i18n: i18n,
35
- restrictLayoutsTo: viewsDir
36
- }));
32
+ app.engine(
33
+ 'hbs',
34
+ hbs.express3({
35
+ i18n: i18n,
36
+ restrictLayoutsTo: viewsDir,
37
+ }),
38
+ );
37
39
  app.set('view engine', 'hbs');
38
40
  app.set('views', viewsDir);
39
41
 
@@ -45,7 +47,7 @@ function create(hbs, env) {
45
47
 
46
48
  app.get('/', function (req, res) {
47
49
  res.render('index', {
48
- array: [1, 2]
50
+ array: [1, 2],
49
51
  });
50
52
  });
51
53
 
@@ -10,7 +10,7 @@ function makeUserRequest(app, user, cb) {
10
10
  request(app)
11
11
  .get('/')
12
12
  .set('Cookie', 'user=' + user)
13
- .end(function(err, res) {
13
+ .end(function (err, res) {
14
14
  if (err) cb(err);
15
15
  if (res.text.search('Hello, ' + user) <= 0) {
16
16
  return cb(new Error('Wrong template send for user ' + user + ': ' + res.text), user);
@@ -21,18 +21,18 @@ function makeUserRequest(app, user, cb) {
21
21
 
22
22
  function requestAll(app, users, cb) {
23
23
  var status = {};
24
- for(var i = 0; i < users.length; i++) {
24
+ for (var i = 0; i < users.length; i++) {
25
25
  status[users[i]] = 'Pending';
26
- makeUserRequest(app, users[i], function(err, user) {
26
+ makeUserRequest(app, users[i], function (err, user) {
27
27
  if (err) {
28
28
  status[user] = 'Error: ' + err.message;
29
29
  } else {
30
- status[user] = 'Completed'
30
+ status[user] = 'Completed';
31
31
  }
32
32
  });
33
33
  }
34
- var checkTimer = setInterval(function() {
35
- for(var i = 0; i < users.length; i++) {
34
+ var checkTimer = setInterval(function () {
35
+ for (var i = 0; i < users.length; i++) {
36
36
  if (status[users[i]] === 'Pending') {
37
37
  return;
38
38
  }
@@ -42,10 +42,10 @@ function requestAll(app, users, cb) {
42
42
  }, 100);
43
43
  }
44
44
 
45
- describe('async', function() {
46
- it('should render all async helpers', function(done) {
45
+ describe('async', function () {
46
+ it('should render all async helpers', function (done) {
47
47
  var app = asyncApp.create(hbs.create(), 'production');
48
- requestAll(app, ['jt','anna','joe','jeff','jane'], function(results) {
48
+ requestAll(app, ['jt', 'anna', 'joe', 'jeff', 'jane'], function (results) {
49
49
  assert.equal(results.jt, 'Completed');
50
50
  assert.equal(results.anna, 'Completed');
51
51
  assert.equal(results.joe, 'Completed');
@@ -55,14 +55,14 @@ describe('async', function() {
55
55
  });
56
56
  });
57
57
 
58
- it('should render nested async helpers', function(done) {
58
+ it('should render nested async helpers', function (done) {
59
59
  var app = asyncApp.create(hbs.create(), 'production');
60
- makeUserRequest(app, 'jt', function(err, user, results) {
60
+ makeUserRequest(app, 'jt', function (err, user, results) {
61
61
  if (err) {
62
62
  return done(err);
63
63
  }
64
64
  assert.equal(false, resolver.hasResolvers(results));
65
- assert.equal(-1, results.search('This should not show!'))
65
+ assert.equal(-1, results.search('This should not show!'));
66
66
  done();
67
67
  });
68
68
  });
@@ -4,7 +4,6 @@ var rewire = require('rewire');
4
4
  var assert = require('assert');
5
5
  var fs = require('fs');
6
6
 
7
-
8
7
  /**
9
8
  * Creates instance of example app using an injected version of express-hbs to track the number of times a
10
9
  * file is read. Additionally, the $NODE_ENV environment variable may be set.
@@ -16,7 +15,7 @@ function createApp(env) {
16
15
  var readCounts = {};
17
16
  var hbs = rewire('../lib/hbs');
18
17
  hbs.__set__('fs', {
19
- readFileSync: function(filename, encoding) {
18
+ readFileSync: function (filename, encoding) {
20
19
  if (typeof readCounts[filename] === 'undefined') {
21
20
  readCounts[filename] = 1;
22
21
  } else {
@@ -26,7 +25,7 @@ function createApp(env) {
26
25
  return fs.readFileSync(filename, encoding);
27
26
  },
28
27
 
29
- readFile: function(filename, encoding, cb) {
28
+ readFile: function (filename, encoding, cb) {
30
29
  if (typeof readCounts[filename] === 'undefined') {
31
30
  readCounts[filename] = 1;
32
31
  } else {
@@ -35,33 +34,31 @@ function createApp(env) {
35
34
 
36
35
  fs.readFile(filename, encoding, cb);
37
36
  },
38
- existsSync: function(filename, encoding) {
37
+ existsSync: function (filename, encoding) {
39
38
  return fs.existsSync(filename, encoding);
40
- }
39
+ },
41
40
  });
42
41
 
43
42
  // used mocked hbs in example
44
43
  var example = require('../example/app');
45
44
  var app = example.create(hbs, env);
46
- return {app: app, readCounts: readCounts};
45
+ return { app: app, readCounts: readCounts };
47
46
  }
48
47
 
49
-
50
- describe('express-hbs', function() {
51
- describe('cache', function() {
52
-
53
- it('should not cache layout in `development`', function(done) {
48
+ describe('express-hbs', function () {
49
+ describe('cache', function () {
50
+ it('should not cache layout in `development`', function (done) {
54
51
  var mock = createApp('development');
55
52
 
56
53
  request(mock.app)
57
54
  .get('/')
58
- .end(function(err) {
55
+ .end(function (err) {
59
56
  assert.ifError(err);
60
57
 
61
58
  request(mock.app)
62
59
  .get('/')
63
60
  .expect(/DEFAULT LAYOUT/)
64
- .end(function(err) {
61
+ .end(function (err) {
65
62
  assert.ifError(err);
66
63
 
67
64
  var filename = path.resolve(__dirname, '../example/views/layout/default.hbs');
@@ -71,19 +68,19 @@ describe('express-hbs', function() {
71
68
  });
72
69
  });
73
70
 
74
- it('should cache layout in `production` reading file once', function(done) {
71
+ it('should cache layout in `production` reading file once', function (done) {
75
72
  var mock = createApp('production');
76
73
 
77
74
  // reads layout from fs once
78
75
  request(mock.app)
79
76
  .get('/')
80
- .end(function(err) {
77
+ .end(function (err) {
81
78
  assert.ifError(err);
82
79
 
83
80
  request(mock.app)
84
81
  .get('/')
85
82
  .expect(/DEFAULT LAYOUT/)
86
- .end(function(err, res) {
83
+ .end(function (err, res) {
87
84
  assert.ifError(err);
88
85
 
89
86
  var filename = path.resolve(__dirname, '../example/views/layout/default.hbs');
@@ -93,18 +90,18 @@ describe('express-hbs', function() {
93
90
  });
94
91
  });
95
92
 
96
- it('should not cache partials in `development`', function(done) {
93
+ it('should not cache partials in `development`', function (done) {
97
94
  var mock = createApp('development');
98
95
 
99
96
  request(mock.app)
100
97
  .get('/veggies')
101
98
  .expect(/just a comment/)
102
- .end(function(err) {
99
+ .end(function (err) {
103
100
  assert.ifError(err);
104
101
  request(mock.app)
105
102
  .get('/veggies')
106
103
  .expect(/just a comment/)
107
- .end(function(err) {
104
+ .end(function (err) {
108
105
  assert.ifError(err);
109
106
 
110
107
  var filename = path.resolve(__dirname, '../example/views/partials/sub/comment.hbs');
@@ -120,12 +117,12 @@ describe('express-hbs', function() {
120
117
  request(mock.app)
121
118
  .get('/veggies')
122
119
  .expect(/just a comment/)
123
- .end(function(err) {
120
+ .end(function (err) {
124
121
  assert.ifError(err);
125
122
  request(mock.app)
126
123
  .get('/veggies')
127
124
  .expect(/just a comment/)
128
- .end(function(err) {
125
+ .end(function (err) {
129
126
  assert.ifError(err);
130
127
 
131
128
  var filename = path.resolve(__dirname, '../example/views/partials/sub/comment.hbs');
@@ -4,33 +4,32 @@ var hbs = require('..');
4
4
  var path = require('path');
5
5
  var H = require('./helpers');
6
6
 
7
-
8
- // MEANJS is using custom extension .server.view.html instead of .hbs
7
+ // MEANJS is using custom extension .server.view.html instead of .hbs
9
8
  // https://github.com/meanjs/mean
10
- describe('custom extension for partials view', function() {
9
+ describe('custom extension for partials view', function () {
11
10
  var dirname = path.join(__dirname, 'views/customExtension');
12
11
  var render = hbs.create().express4({
13
- extname: '.server.view.html',
14
- partialsDir: dirname + '/partialsDir',
15
- restrictLayoutsTo: dirname
12
+ extname: '.server.view.html',
13
+ partialsDir: dirname + '/partialsDir',
14
+ restrictLayoutsTo: dirname,
16
15
  });
17
16
 
18
- it('should allow rendering multiple partials with custom extension', function(done) {
17
+ it('should allow rendering multiple partials with custom extension', function (done) {
19
18
  function check(err, html) {
20
19
  assert.ifError(err);
21
20
  assert.equal(
22
- '<html>' +
23
- '<subpartial>1</subpartial>' +
24
- '<partial>1</partial>' +
25
- '<subpartial>2</subpartial>' +
26
- '<partial>2</partial>' +
27
- '</html>',
28
- H.stripWs(html));
21
+ '<html>' +
22
+ '<subpartial>1</subpartial>' +
23
+ '<partial>1</partial>' +
24
+ '<subpartial>2</subpartial>' +
25
+ '<partial>2</partial>' +
26
+ '</html>',
27
+ H.stripWs(html),
28
+ );
29
29
  done();
30
30
  }
31
31
 
32
- var options = {cache: true, settings: {views: dirname }};
32
+ var options = { cache: true, settings: { views: dirname } };
33
33
  render(dirname + '/template.server.view.html', options, check);
34
34
  });
35
-
36
35
  });
@@ -2,90 +2,87 @@ var request = require('supertest');
2
2
  var assert = require('assert');
3
3
  var hbs = require('..');
4
4
 
5
- describe('express-hbs', function() {
6
-
7
- describe('defaults', function() {
5
+ describe('express-hbs', function () {
6
+ describe('defaults', function () {
8
7
  var app;
9
8
 
10
- beforeEach(function() {
9
+ beforeEach(function () {
11
10
  var example = require('../example/app');
12
11
  app = example.create(hbs.create());
13
12
  });
14
13
 
15
-
16
- it('should render using default layout', function(done) {
14
+ it('should render using default layout', function (done) {
17
15
  request(app)
18
16
  .get('/')
19
17
  .expect(/DEFAULT LAYOUT/, done);
20
18
  });
21
19
 
22
- it('should render layout declared in markup', function(done) {
20
+ it('should render layout declared in markup', function (done) {
23
21
  request(app)
24
22
  .get('/fruits')
25
23
  .expect(/DECLARATIVE LAYOUT/, done);
26
24
  });
27
25
 
28
- it('should render nested declarative layouts correctly', function(done) {
26
+ it('should render nested declarative layouts correctly', function (done) {
29
27
  request(app)
30
28
  .get('/fruits/apple')
31
29
  .expect(/DECLARATIVE LAYOUT/)
32
30
  .expect(/NESTED LAYOUT/, done);
33
31
  });
34
32
 
35
- it('should render layout specified as locals', function(done) {
33
+ it('should render layout specified as locals', function (done) {
36
34
  request(app)
37
35
  .get('/veggies')
38
36
  .expect(/PROGRAMMATIC LAYOUT/, done);
39
37
  });
40
38
 
41
- it('should render nested layouts correctly when first layout is specified as locals', function(done) {
39
+ it('should render nested layouts correctly when first layout is specified as locals', function (done) {
42
40
  request(app)
43
41
  .get('/veggies/carrot')
44
42
  .expect(/PROGRAMMATIC LAYOUT/)
45
43
  .expect(/NESTED LAYOUT/, done);
46
44
  });
47
45
 
48
- it('should render partial', function(done) {
46
+ it('should render partial', function (done) {
49
47
  request(app)
50
48
  .get('/veggies')
51
49
  .expect(/jquery\.js/)
52
50
  .expect(/Other partial/, done);
53
51
  });
54
52
 
55
- it('should render sub partial', function(done) {
53
+ it('should render sub partial', function (done) {
56
54
  request(app)
57
55
  .get('/veggies')
58
56
  .expect(/just a comment/, done);
59
57
  });
60
58
 
61
- it('should render block', function(done) {
59
+ it('should render block', function (done) {
62
60
  request(app)
63
61
  .get('/')
64
62
  .expect(/color: blue/, done);
65
63
  });
66
64
 
67
- it('should render block default content', function(done) {
65
+ it('should render block default content', function (done) {
68
66
  request(app)
69
67
  .get('/')
70
68
  .expect(/Default block content/, done);
71
69
  });
72
70
 
73
- it('should render block content instead of default content when contentFor is declared', function(done) {
71
+ it('should render block content instead of default content when contentFor is declared', function (done) {
74
72
  request(app)
75
73
  .get('/replace')
76
74
  .expect(/Non-default block content/, done);
77
75
  });
78
76
 
79
- it('should replace {{body}}', function(done) {
77
+ it('should replace {{body}}', function (done) {
80
78
  request(app)
81
79
  .get('/')
82
80
  .expect(/Vegetables/, done);
83
81
  });
84
-
85
82
  });
86
83
 
87
- describe('instances', function() {
88
- it('should create isolated instances', function() {
84
+ describe('instances', function () {
85
+ it('should create isolated instances', function () {
89
86
  var hbs2 = hbs.create();
90
87
  var hbs3 = hbs.create();
91
88
 
@@ -2,55 +2,57 @@ var assert = require('assert');
2
2
  var hbs = require('..');
3
3
  var H = require('./helpers');
4
4
 
5
- describe('helpers', function() {
6
-
7
- describe('sync', function() {
5
+ describe('helpers', function () {
6
+ describe('sync', function () {
8
7
  var dirname = __dirname + '/views/helpers';
9
8
 
10
9
  function sync(s, options) {
11
- return new hbs.SafeString("-sync-" + s);
10
+ return new hbs.SafeString('-sync-' + s);
12
11
  }
13
12
 
14
- it ('should register functions', function(done) {
13
+ it('should register functions', function (done) {
15
14
  var hb = hbs.create();
16
- hb.registerHelper("sync", sync);
15
+ hb.registerHelper('sync', sync);
17
16
  var render = hb.express3({
18
17
  viewsDir: dirname,
19
- restrictLayoutsTo: dirname
18
+ restrictLayoutsTo: dirname,
20
19
  });
21
20
  var locals = H.createLocals('express3', dirname);
22
21
 
23
- render(dirname + '/home/index.hbs', locals, function(err, html) {
22
+ render(dirname + '/home/index.hbs', locals, function (err, html) {
24
23
  assert.equal('<default>index-sync-index</default>', H.stripWs(html));
25
24
  done();
26
25
  });
27
26
  });
28
27
  });
29
28
 
30
- describe('async', function() {
29
+ describe('async', function () {
31
30
  var dirname = __dirname + '/views/helpers';
32
- it ('should register functions', function(done) {
31
+ it('should register functions', function (done) {
33
32
  var hb = hbs.create();
34
33
  function async(s, cb) {
35
- setTimeout(function() {
36
- cb(new hb.SafeString("-async-" + s));
37
- }, Math.floor((Math.random()*10)+1));
34
+ setTimeout(
35
+ function () {
36
+ cb(new hb.SafeString('-async-' + s));
37
+ },
38
+ Math.floor(Math.random() * 10 + 1),
39
+ );
38
40
  }
39
- hb.registerAsyncHelper("async", async);
41
+ hb.registerAsyncHelper('async', async);
40
42
 
41
43
  var render = hb.express3({
42
44
  viewsDir: dirname,
43
- restrictLayoutsTo: dirname
45
+ restrictLayoutsTo: dirname,
44
46
  });
45
47
  var locals = H.createLocals('express3', dirname);
46
48
 
47
- render(dirname + '/home/async.hbs', locals, function(err, html) {
48
- assert.equal('<default>asynctemplate-async-foo-async-bar-async-bah-async-baz</default>', H.stripWs(html));
49
+ render(dirname + '/home/async.hbs', locals, function (err, html) {
50
+ assert.equal(
51
+ '<default>asynctemplate-async-foo-async-bar-async-bah-async-baz</default>',
52
+ H.stripWs(html),
53
+ );
49
54
  done();
50
55
  });
51
56
  });
52
-
53
-
54
57
  });
55
-
56
58
  });
package/test/helpers.js CHANGED
@@ -7,7 +7,7 @@ function createLocals(which, viewsDir, locals) {
7
7
  var opts = {};
8
8
  if (which === 'express3' || which === 'express4') {
9
9
  opts.settings = {
10
- views: viewsDir
10
+ views: viewsDir,
11
11
  };
12
12
  opts.cache = process.env.NODE_ENV === 'production';
13
13
  for (var k in locals) {
@@ -20,5 +20,5 @@ function createLocals(which, viewsDir, locals) {
20
20
 
21
21
  module.exports = {
22
22
  createLocals: createLocals,
23
- stripWs: stripWs
24
- };
23
+ stripWs: stripWs,
24
+ };