forked from assemble/assemble
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcollection.render.js
140 lines (115 loc) · 4.2 KB
/
collection.render.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
'use strict';
require('mocha');
require('should');
var async = require('async');
var assert = require('assert');
var support = require('./support');
var App = support.resolve();
var List = App.List;
var Views = App.Views;
var pages;
describe('render', function() {
describe('rendering', function() {
beforeEach(function() {
pages = new Views();
pages.engine('tmpl', require('engine-base'));
});
it('should throw an error when no callback is given:', function() {
(function() {
pages.render({});
}).should.throw('Views#render is async and expects a callback function');
});
it('should throw an error when an engine is not defined:', function(cb) {
pages.addView('foo.bar', {content: '<%= name %>'});
var page = pages.getView('foo.bar');
pages.render(page, function(err) {
assert(err.message === 'Views#render cannot find an engine for: .bar');
cb();
});
});
it('should use helpers to render a view:', function(cb) {
var locals = {name: 'Halle'};
pages.helper('upper', function(str) {
return str.toUpperCase(str);
});
pages.addView('a.tmpl', {content: 'a <%= upper(name) %> b', locals: locals});
var page = pages.getView('a.tmpl');
pages.render(page, function(err, res) {
if (err) return cb(err);
assert(res.content === 'a HALLE b');
cb();
});
});
it('should use helpers when rendering a view:', function(cb) {
var locals = {name: 'Halle'};
pages.helper('upper', function(str) {
return str.toUpperCase(str);
});
pages.addView('a.tmpl', {content: 'a <%= upper(name) %> b', locals: locals});
var page = pages.getView('a.tmpl');
pages.render(page, function(err, res) {
if (err) return cb(err);
assert(res.content === 'a HALLE b');
cb();
});
});
it('should render a template when contents is a buffer:', function(cb) {
pages.addView('a.tmpl', {content: '<%= a %>', locals: {a: 'b'}});
var view = pages.getView('a.tmpl');
pages.render(view, function(err, view) {
if (err) return cb(err);
assert(view.contents.toString() === 'b');
cb();
});
});
it('should render a template when content is a string:', function(cb) {
pages.addView('a.tmpl', {content: '<%= a %>', locals: {a: 'b'}});
var view = pages.getView('a.tmpl');
pages.render(view, function(err, view) {
if (err) return cb(err);
assert(view.contents.toString() === 'b');
cb();
});
});
it('should render a view from its path:', function(cb) {
pages.addView('a.tmpl', {content: '<%= a %>', locals: {a: 'b'}});
pages.render('a.tmpl', function(err, view) {
if (err) return cb(err);
assert(view.content === 'b');
cb();
});
});
it('should use a plugin for rendering:', function(cb) {
pages.engine('tmpl', require('engine-base'));
pages.option('engine', 'tmpl');
pages.addViews({
'a': {content: '<%= title %>', locals: {title: 'aaa'}},
'b': {content: '<%= title %>', locals: {title: 'bbb'}},
'c': {content: '<%= title %>', locals: {title: 'ccc'}},
'd': {content: '<%= title %>', locals: {title: 'ddd'}},
'e': {content: '<%= title %>', locals: {title: 'eee'}},
'f': {content: '<%= title %>', locals: {title: 'fff'}},
'g': {content: '<%= title %>', locals: {title: 'ggg'}},
'h': {content: '<%= title %>', locals: {title: 'hhh'}},
'i': {content: '<%= title %>', locals: {title: 'iii'}},
'j': {content: '<%= title %>', locals: {title: 'jjj'}},
});
pages.use(function(collection) {
collection.option('pager', false);
collection.renderEach = function(cb) {
var list = new List(collection);
async.map(list.items, function(item, next) {
collection.render(item, next);
}, cb);
};
});
pages.renderEach(function(err, items) {
if (err) return cb(err);
assert(items[0].content === 'aaa');
assert(items[9].content === 'jjj');
assert(items.length === 10);
cb();
});
});
});
});