-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlayout.js
More file actions
141 lines (116 loc) · 3.53 KB
/
layout.js
File metadata and controls
141 lines (116 loc) · 3.53 KB
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
'use strict';
const RE_MATCH_HEAD = /<head([^<>]*)>/;
const RE_MATCH_BODY = /<body([^<>]*)>/;
module.exports = (Grown, util) => {
function _renderSlot(opts, state) {
/* istanbul ignore else */
if (typeof opts === 'string') {
return opts;
}
/* istanbul ignore else */
if (typeof opts === 'function') {
return this._renderSlot(opts(state, this._buildvNode), state);
}
/* istanbul ignore else */
if (Array.isArray(opts)) {
return opts.map(x => this._renderSlot(x, state)).join('\n');
}
/* istanbul ignore else */
if (opts.tag) {
return this._buildHTML(opts, 0, state);
}
/* istanbul ignore else */
if (opts.meta) {
/* istanbul ignore else */
if (opts.httpEquiv) {
return this._buildHTML({
tag: 'meta',
data: {
'http-equiv': opts.meta,
content: opts.content,
},
});
}
return this._buildHTML({
tag: 'meta',
data: {
name: opts.meta,
content: opts.content,
},
});
}
throw new Error(`Unexpected view-slot, given '${util.inspect(opts)}'`);
}
function _onRender(ctx, template) {
const _layout = template.locals.layout || ctx.res.layout || this.default_layout;
/* istanbul ignore else */
if (template.locals.layout !== false && (_layout && _layout !== template.view)) {
let markup = '';
try {
markup = (this.partial(_layout, util.extendValues({}, template.locals, {
contents: template.contents,
})) || '').trim();
} catch (e) {
markup = e.message;
}
template.contents = markup.indexOf('<html') === 0
? `<!DOCTYPE html>\n${markup}`
: markup;
const before = {
body: this._renderSlot(ctx.chunks.before.body, template.locals),
head: this._renderSlot(ctx.chunks.before.head, template.locals),
};
const after = {
body: this._renderSlot(ctx.chunks.after.body, template.locals),
head: this._renderSlot(ctx.chunks.after.head, template.locals),
};
if (template.contents.indexOf('</head>') === -1) {
template.contents = template.contents.replace('<body', () => `<head>${before.head}${after.head}</head><body`);
} else {
template.contents = template.contents.replace(RE_MATCH_HEAD, (_, attrs) => `<head${attrs}>${before.head}`);
template.contents = template.contents.replace('</head>', () => `${after.head}</head>`);
}
template.contents = template.contents.replace(RE_MATCH_BODY, (_, attrs) => `<body${attrs}>${before.body}`);
template.contents = template.contents.replace('</body>', () => `${after.body}</body>`);
}
}
return Grown('Render.Layout', {
_renderSlot,
// default options
default_layout: '',
// render hooks
$before_render: _onRender,
$install() {
if (!this._buildvNode) {
throw new Error('Render.Layout depends on Render.Views, please include within');
}
},
$mixins() {
const _partials = {
before: {
head: [],
body: [],
},
after: {
head: [],
body: [],
},
};
return {
props: {
chunks: () => _partials,
},
methods: {
prepend(to, opts) {
_partials.before[to].unshift(opts);
return this;
},
append(to, opts) {
_partials.after[to].push(opts);
return this;
},
},
};
},
});
};