Skip to content
This repository was archived by the owner on Aug 30, 2021. It is now read-only.

Commit f6e5797

Browse files
authored
feat(express): introducing handlebars nodejs template system (#1461)
This commit introduces handlebars template system and completely replacing Swig and the Consolidate project to handle multiple template systems. Fixes #1286
1 parent 07a860f commit f6e5797

File tree

8 files changed

+24
-27
lines changed

8 files changed

+24
-27
lines changed

config/env/default.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ module.exports = {
99
},
1010
port: process.env.PORT || 3000,
1111
host: process.env.HOST || '0.0.0.0',
12-
templateEngine: 'swig',
1312
// Session Cookie settings
1413
sessionCookie: {
1514
// session expiration is set by default to 24 hours

config/lib/express.js

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ var config = require('../config'),
1616
cookieParser = require('cookie-parser'),
1717
helmet = require('helmet'),
1818
flash = require('connect-flash'),
19-
consolidate = require('consolidate'),
19+
hbs = require('express-hbs'),
2020
path = require('path'),
2121
_ = require('lodash'),
2222
lusca = require('lusca');
@@ -99,12 +99,11 @@ module.exports.initMiddleware = function (app) {
9999
* Configure view engine
100100
*/
101101
module.exports.initViewEngine = function (app) {
102-
// Set swig as the template engine
103-
app.engine('server.view.html', consolidate[config.templateEngine]);
104-
105-
// Set views path and view engine
102+
app.engine('server.view.html', hbs.express4({
103+
extname: '.server.view.html'
104+
}));
106105
app.set('view engine', 'server.view.html');
107-
app.set('views', './');
106+
app.set('views', path.resolve('./'));
108107
};
109108

110109
/**

modules/core/server/controllers/core.server.controller.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ exports.renderIndex = function (req, res) {
2424
}
2525

2626
res.render('modules/core/server/views/index', {
27-
user: safeUserObject
27+
user: JSON.stringify(safeUserObject)
2828
});
2929
};
3030

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
{% extends 'layout.server.view.html' %}
1+
{{!< layout}}
22

3-
{% block content %}
3+
{{#contentFor 'content'}}
44
<h1>Page Not Found</h1>
55
<div class="alert alert-danger" role="alert">
66
<span class="glyphicon glyphicon-exclamation-sign" aria-hidden="true"></span>
77
<span class="sr-only">Error:</span>
88
{{url}} is not a valid path.
99
</div>
10-
{% endblock %}
10+
{{/contentFor}}
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
{% extends 'layout.server.view.html' %}
1+
{{!< layout}}
22

3-
{% block content %}
3+
{{#contentFor 'content'}}
44
<h1>Server Error</h1>
55
<pre>
66
{{error}}
77
</pre>
8-
{% endblock %}
8+
{{/contentFor}}
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
{% extends 'layout.server.view.html' %}
1+
{{!< layout}}
22

3-
{% block content %}
3+
{{#contentFor 'content'}}
44
<section ui-view></section>
5-
{% endblock %}
5+
{{/contentFor}}

modules/core/server/views/layout.server.view.html

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<!DOCTYPE html>
1+
<!DOCTYPE html>
22
<html lang="en" ng-strict-di>
33
<head>
44
<meta charset="utf-8">
@@ -32,14 +32,14 @@
3232
<link href="{{favicon}}" rel="shortcut icon" type="image/x-icon">
3333

3434
<!-- Application CSS Files -->
35-
{% for cssFile in cssFiles %}<link rel="stylesheet" href="{{cssFile}}">{% endfor %}
35+
{{#each cssFiles}}<link rel="stylesheet" href="{{this}}">{{/each}}
3636
</head>
3737

3838
<body class="ng-cloak">
3939
<header ng-include="'/modules/core/client/views/header.client.view.html'" class="navbar navbar-fixed-top navbar-inverse"></header>
4040
<section class="content">
4141
<section class="container">
42-
{% block content %}{% endblock %}
42+
{{{block "content"}}}
4343
</section>
4444
</section>
4545

@@ -56,20 +56,20 @@
5656

5757
<!--Embedding The User Object-->
5858
<script type="text/javascript">
59-
var user = {{ user | json | safe }},
60-
env = "{{ env }}";
59+
var user = {{{ user }}};
60+
var env = "{{ env }}";
6161
</script>
6262

6363
<!--Load The Socket.io File-->
6464
<script type="text/javascript" src="/socket.io/socket.io.js"></script>
6565

6666
<!--Application JavaScript Files-->
67-
{% for jsFile in jsFiles %}<script type="text/javascript" src="{{jsFile}}"></script>{% endfor %}
67+
{{#each jsFiles}}<script type="text/javascript" src="{{this}}"></script>{{/each}}
6868

69-
{% if livereload %}
69+
{{#if livereload}}
7070
<!--Livereload script rendered -->
7171
<script type="text/javascript" src="{{host}}:35729/livereload.js"></script>
72-
{% endif %}
72+
{{/if}}
7373
</body>
7474

7575
</html>

package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,10 @@
4040
"compression": "~1.6.1",
4141
"connect-flash": "~0.1.1",
4242
"connect-mongo": "~1.1.0",
43-
"consolidate": "~0.14.0",
4443
"cookie-parser": "~1.4.1",
4544
"crypto": "0.0.3",
4645
"express": "~4.14.0",
46+
"express-hbs": "^1.0.2",
4747
"express-session": "~1.13.0",
4848
"file-stream-rotator": "~0.0.6",
4949
"generate-password": "~1.1.1",
@@ -73,7 +73,6 @@
7373
"phantomjs-prebuilt": "~2.1.4",
7474
"serve-favicon": "~2.3.0",
7575
"socket.io": "~1.4.5",
76-
"swig": "~1.4.2",
7776
"validator": "~5.1.0",
7877
"winston": "^2.2.0",
7978
"wiredep": "~4.0.0"

0 commit comments

Comments
 (0)