/
partials.js
executable file
·72 lines (49 loc) · 1.55 KB
/
partials.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
'use strict';
// Load modules
const Hapi = require('hapi');
const Mustache = require('mustache');
const Vision = require('../..');
// Declare internals
const internals = {};
const rootHandler = function (request, reply) {
reply.view('index', {
title: 'examples/views/mustache/partials.js | Hapi ' + request.server.version,
message: 'Index - Hello World!'
});
};
internals.main = function () {
const server = new Hapi.Server();
server.connection({ port: 8000 });
server.register(Vision, (err) => {
if (err) {
throw err;
}
const partials = {};
server.views({
engines: {
html: {
compile: function (template) {
Mustache.parse(template);
return function (context) {
return Mustache.render(template, context, partials);
};
},
registerPartial: function (name, src) {
partials[name] = src;
}
}
},
relativeTo: __dirname,
path: 'templates/withPartials',
partialsPath: 'templates/withPartials/partials'
});
server.route({ method: 'GET', path: '/', handler: rootHandler });
server.start((err) => {
if (err) {
throw err;
}
console.log('Server is listening at ' + server.info.uri);
});
});
};
internals.main();