1 |
|
2 |
|
3 |
|
4 |
|
5 | var Readable = require('stream').Readable;
|
6 | var util = require('util');
|
7 | var Swig = require('swig').Swig;
|
8 | var loader = require('./lib/loader.js');
|
9 | var tags = [
|
10 | "script",
|
11 | "style",
|
12 | "html",
|
13 | "body",
|
14 | "require",
|
15 | "uri",
|
16 | "widget",
|
17 | "head",
|
18 | "feature",
|
19 | "featureelse",
|
20 | "spage"
|
21 | ];
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 | var SwigWrap = module.exports = function SwigWrap(options, layer) {
|
38 |
|
39 | if (!(this instanceof SwigWrap)) {
|
40 | return new SwigWrap(options, layer);
|
41 | }
|
42 |
|
43 |
|
44 | options.loader = options.loader || loader(layer, options.views);
|
45 |
|
46 | var swig = this.swig = new Swig(options);
|
47 | this.options = swig.options;
|
48 |
|
49 | tags.forEach(function (tag) {
|
50 | var t = require('./tags/' + tag);
|
51 | swig.setTag(tag, t.parse, t.compile, t.ends, t.blockLevel || false);
|
52 | });
|
53 |
|
54 | Readable.call(this, null);
|
55 | this.buzy = false;
|
56 | };
|
57 |
|
58 | util.inherits(SwigWrap, Readable);
|
59 |
|
60 | SwigWrap.prototype._read = function(n) {
|
61 | if (!this.buzy && this.options.view) {
|
62 | this.renderFile(this.options.view, this.options.locals);
|
63 | }
|
64 | };
|
65 |
|
66 |
|
67 |
|
68 |
|
69 | SwigWrap.prototype.renderFile = function(view, options) {
|
70 | var self = this;
|
71 |
|
72 | if (this.buzy) return;
|
73 | this.buzy = true;
|
74 |
|
75 |
|
76 | this.swig.renderFile(view, options, function(error, output) {
|
77 | if (error) {
|
78 | return self.emit('error', error);
|
79 | }
|
80 |
|
81 | self.push(output);
|
82 | self.push(null);
|
83 | });
|
84 | };
|
85 |
|
86 | SwigWrap.prototype.destroy = function() {
|
87 | this.swig = null;
|
88 | this.removeAllListeners();
|
89 | };
|
90 |
|
91 |
|
92 | Swig.prototype._w = Swig.prototype._widget = function(layer, id, attr, options) {
|
93 | var self = this;
|
94 | var pathname = layer.resolve(id);
|
95 |
|
96 | if (!layer.supportBigPipe() || !attr.mode || attr.mode === 'sync') {
|
97 | layer.load(id);
|
98 | return this.compileFile(pathname, options);
|
99 | }
|
100 |
|
101 | return function(locals) {
|
102 | var container = attr['container'] || attr['for'];
|
103 |
|
104 | layer.addPagelet({
|
105 | container: container,
|
106 | model: attr.model,
|
107 | id: attr.id,
|
108 | mode: attr.mode,
|
109 | locals: locals,
|
110 | view: pathname,
|
111 | viewId: id,
|
112 |
|
113 | compiled: function(locals) {
|
114 | var fn = self.compileFile(pathname, options);
|
115 | var layer = locals._yog;
|
116 | layer && layer.load(id);
|
117 | return fn.apply(this, arguments);
|
118 | }
|
119 | });
|
120 |
|
121 | return container ? '' : '<div id="' + attr.id + '"></div>';
|
122 | };
|
123 | }; |
\ | No newline at end of file |