forked from assemble/assemble
-
Notifications
You must be signed in to change notification settings - Fork 0
/
partials.js
204 lines (170 loc) · 6.84 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
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
'use strict';
require('mocha');
require('should');
var assert = require('assert');
var support = require('./support');
var App = support.resolve();
var app, pages;
describe('partials', function() {
beforeEach(function() {
app = new App();
app.engine('tmpl', require('engine-base'));
app.engine('hbs', require('engine-handlebars'));
app.create('partials', { viewType: 'partial' });
app.create('include', { viewType: 'partial' });
app.create('layouts', { viewType: 'layout' });
pages = app.create('page');
});
it('should inject a partial with a helper:', function(cb) {
app.include('base', {path: 'base.tmpl', content: 'xyz'});
app.pages('a.tmpl', {path: 'a.tmpl', content: 'a <%= include("base") %> c'});
var page = app.pages.getView('a.tmpl');
app.render(page, function(err, view) {
if (err) return cb(err);
assert.equal(typeof view.content, 'string');
assert.equal(view.content, 'a xyz c');
cb();
});
});
it('should inject a partial with a helper on a collection:', function(cb) {
app.include('base', {path: 'base.tmpl', content: 'xyz'});
pages.engine('.tmpl', require('engine-handlebars'));
pages.helpers(app._.helpers.sync);
pages.asyncHelpers(app._.helpers.async);
pages.addView('a.tmpl', {path: 'a.tmpl', content: 'a {{include "base" }} c'});
var page = pages.getView('a.tmpl');
pages.render(page, function(err, view) {
if (err) return cb(err);
assert.equal(typeof view.content, 'string');
assert.equal(view.content, 'a xyz c');
cb();
});
});
it('should use handlebars partial with a helper on a collection:', function(cb) {
app.include('base', {path: 'base.tmpl', content: 'xyz'});
pages.engine('.tmpl', require('engine-handlebars'));
pages.helpers(app._.helpers.sync);
pages.asyncHelpers(app._.helpers.async);
pages.addView('a.tmpl', {path: 'a.tmpl', content: 'a {{> base }} c'});
var page = pages.getView('a.tmpl');
var locals = app.mergePartials(this.options);
pages.render(page, locals, function(err, view) {
if (err) return cb(err);
assert.equal(typeof view.content, 'string');
assert.equal(view.content, 'a xyz c');
cb();
});
});
it('should use layouts with partials:', function(cb) {
app.layout('default', {path: 'a.tmpl', content: 'a {% body %} c'});
app.include('b', {path: 'b.tmpl', content: 'b', layout: 'default'});
app.pages('a.tmpl', {path: 'c.tmpl', content: '<%= include("b") %>'});
var page = app.pages.getView('a.tmpl');
app.render(page, function(err, view) {
if (err) return cb(err);
assert.equal(typeof view.content, 'string');
assert.equal(view.content, 'a b c');
cb();
});
});
it('should add `layoutApplied` after layout is applied:', function(cb) {
app.layout('default', {path: 'a.tmpl', content: 'a {% body %} c'});
app.include('b', {path: 'b.tmpl', content: 'b', layout: 'default'});
app.pages('a.tmpl', {path: 'c.tmpl', content: '<%= include("b") %>'});
var page = app.pages.getView('a.tmpl');
app.render(page, function(err, view) {
if (err) return cb(err);
assert.equal(typeof view.content, 'string');
assert.equal(app.layouts.getView('default').options.layoutApplied);
assert.equal(view.content, 'a b c');
cb();
});
});
it('should pass partials to handlebars:', function(cb) {
app.onMerge(/\.hbs$/, function(view, next) {
app.applyLayout(view);
next();
});
app.layout('default', {path: 'a.hbs', content: 'a {% body %} c'});
app.include('foo', {path: 'foo.hbs', content: 'foo', layout: 'default'});
app.pages('a.hbs', {path: 'c.hbs', content: '{{> foo }}'});
var page = app.pages.getView('a.hbs');
app.render(page, function(err, view) {
if (err) return cb(err);
assert.equal(typeof view.content, 'string');
assert.equal(view.content, 'a foo c');
cb();
});
});
it('should only merge in the specified viewTypes:', function(cb) {
app.onMerge(/\.hbs$/, function(view, next) {
app.applyLayout(view);
next();
});
app.layout('default', {path: 'a.hbs', content: 'a {% body %} c'});
app.option('mergeTypes', ['includes']);
app.partial('foo', {path: 'bar.hbs', content: 'bar', layout: 'default'});
app.include('foo', {path: 'foo.hbs', content: 'foo', layout: 'default'});
app.pages('a.hbs', {path: 'c.hbs', content: '{{> foo }}'});
app.pages.getView('a.hbs')
.render(function(err, view) {
if (err) return cb(err);
assert.equal(typeof view.content, 'string');
assert.equal(view.content, 'a foo c');
cb();
});
});
it('should merge the specified viewTypes in the order defined:', function(cb) {
app.onMerge(/\.hbs$/, function(view, next) {
app.applyLayout(view);
next();
});
app.layout('default', {path: 'a.hbs', content: 'a {% body %} c'});
app.option('mergeTypes', ['includes', 'partials']);
app.partial('foo', {path: 'bar.hbs', content: 'bar', layout: 'default'});
app.include('foo', {path: 'foo.hbs', content: 'foo', layout: 'default'});
app.pages('a.hbs', {path: 'c.hbs', content: '{{> foo }}'});
app.pages.getView('a.hbs')
.render(function(err, view) {
if (err) return cb(err);
assert.equal(typeof view.content, 'string');
assert.equal(view.content, 'a bar c');
cb();
});
});
it('should not merge in partials with `options.nomerge` defined:', function(cb) {
app.onMerge(/\.hbs$/, function(view, next) {
app.applyLayout(view);
next();
});
app.layout('default', {path: 'a.hbs', content: 'a {% body %} c'});
app.option('mergeTypes', ['includes', 'partials']);
app.partial('foo', {path: 'bar.hbs', content: 'bar', layout: 'default', options: {nomerge: true}});
app.include('foo', {path: 'foo.hbs', content: 'foo', layout: 'default'});
app.pages('a.hbs', {path: 'c.hbs', content: '{{> foo }}'});
app.pages.getView('a.hbs')
.render(function(err, view) {
if (err) return cb(err);
assert.equal(typeof view.content, 'string');
assert.equal(view.content, 'a foo c');
cb();
});
});
it('should emit an `onMerge` event:', function(cb) {
app.on('onMerge', function(view) {
app.applyLayout(view);
});
app.layout('default', {path: 'a.hbs', content: 'a {% body %} c'});
app.option('mergeTypes', ['includes', 'partials']);
app.partial('foo', {path: 'bar.hbs', content: 'bar', layout: 'default'});
app.include('foo', {path: 'foo.hbs', content: 'foo', layout: 'default'});
app.pages('a.hbs', {path: 'c.hbs', content: '{{> foo }}'});
app.pages.getView('a.hbs')
.render(function(err, view) {
if (err) return cb(err);
assert.equal(typeof view.content, 'string');
assert.equal(view.content, 'a bar c');
cb();
});
});
});