1 | 'use strict';
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 | var define = require('../controller').define,
|
12 | jade = require('jade'),
|
13 | fs = require('fs'),
|
14 | cache = {};
|
15 |
|
16 | module.exports = function (options) {
|
17 | options = options || {};
|
18 |
|
19 | |
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 | define('property', 'jadePath', '.');
|
26 |
|
27 | |
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 | define('method', 'jade', function (template) {
|
42 | var that = this;
|
43 |
|
44 | if (typeof template !== 'string') {
|
45 | throw new Error('Template expected to be string');
|
46 | }
|
47 |
|
48 | if (this.jadePath) {
|
49 | template = this.jadePath + '/' + template;
|
50 | }
|
51 |
|
52 | fs.lstat(template, function (error, result) {
|
53 | var cached = cache[template],
|
54 | mtime;
|
55 |
|
56 | if (error) {
|
57 |
|
58 | that.error(error, true);
|
59 | return;
|
60 | }
|
61 |
|
62 | mtime = +result.mtime;
|
63 |
|
64 |
|
65 | if (cached && cached.mtime === mtime) {
|
66 | that.body = cached(that.stash);
|
67 | that.render('html');
|
68 | return;
|
69 | }
|
70 |
|
71 |
|
72 | fs.readFile(template, function (error, result) {
|
73 | if (error) {
|
74 |
|
75 | that.error(error, true);
|
76 | return;
|
77 | }
|
78 |
|
79 |
|
80 | options.filename = template;
|
81 |
|
82 | try {
|
83 | cached = jade.compile(result, options);
|
84 | that.body = cached(that.stash);
|
85 | } catch (error) {
|
86 |
|
87 | that.error(error, true);
|
88 | return;
|
89 | }
|
90 |
|
91 | cached.mtime = mtime;
|
92 | cache[template] = cached;
|
93 |
|
94 | that.render('html');
|
95 | });
|
96 | });
|
97 |
|
98 | return this;
|
99 | });
|
100 | };
|