-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathexample.js
133 lines (107 loc) · 2.73 KB
/
example.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
'use strict';
var path = require('path');
var red = require('ansi-red');
var templates = require('./');
templates.on('templates.preInit', function(app) {
// console.log(app);
});
templates.on('templates.postInit', function(app) {
// console.log(app);
});
/**
* Create our `app`
*/
var app = templates();
/**
* Listen for errors
*/
app.use(function(app) {
app.on('error', function(err) {
console.log('app:', red(err));
});
return function(collection) {
collection.on('error', function(err) {
console.log('collection:', red(err));
});
return function(view) {
view.on('error', function(err) {
console.log('view:', red(err));
});
};
};
});
/**
* Engine
*/
app.engine('*', require('engine-base'));
app.engine('html', require('engine-base'));
app.option('engine', '*');
/**
* Collections and rendering
*/
app.preRender(/./, function(file, next) {
file.data.title = 'foo';
next();
});
app.data({title: 'Home'});
app.create('pages')
.engine('*', require('engine-base'))
.addView('home', {content: 'The <%= title %> page'})
.set('locals.title', 'HOOMMMME!')
.render(function(err, res) {
if (err) throw err;
console.log(res.content);
});
/**
* Plugins
*/
app.use(function(app) {
app.section = app.create;
return function(views) {
views.on('addView', function() {
// console.log(views._callbacks)
});
// create a custom `.foo()` method on the collection
views.define('foo', views.addView);
return function(view) {
// also add `.foo()` to the view instance for chaining
view.define('foo', views.foo.bind(views));
};
};
});
app.section('articles')
// this first `.foo` is from the collection instance
.foo('one.html', {content: 'The <%= title %> page'})
.set('locals.title', 'One')
.render(function(err, res) {
if (err) throw err;
})
// this `.foo` is from a `view` instance
.foo('two.html', {content: 'The <%= title %> page'})
.set('locals.title', 'Two')
.render(function(err, res) {
if (err) throw err;
// console.log(res.content);
});
/**
* Events
*/
var posts = app.create('posts');
posts.engine('html', require('engine-base'));
posts.option('engine', 'html');
var view = posts.addView('home.html', {content: 'The <%= title %> page'})
.render({title: 'Home'}, function(err, res) {
if (err) throw err;
// console.log(res.content);
});
var collection = app.collection();
collection
.option('renameKey', function(key) {
return path.basename(key);
})
.addView('foo/bar/baz/a.md', {content: '...'})
.addView('foo/bar/baz/b.md', {content: '...'})
.addView('foo/bar/baz/c.md', {content: '...'});
var list = app.list(collection)
// console.log(list)
// console.log(collection.views);