-
-
Notifications
You must be signed in to change notification settings - Fork 23
/
template_example.js
71 lines (59 loc) · 2.03 KB
/
template_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
var sys = require('sys'),
dj = require('./djangode'),
template_system = require('./template/template');
template_loader = require('./template/loader');
// set template path
template_loader.set_path('template-demo');
// context to use when rendering template. In a real app this would likely come from a database
var test_context = {
person_name: 'Thomas Hest',
company: 'Tobis A/S',
ship_date: new Date('12-02-1981'),
item: 'XXX',
item_list: [ 'Giraf', 'Fisk', 'Tapir'],
ordered_warranty: true,
ship: {
name: 'M/S Martha',
nationality: 'Danish'
}
};
// make app
var app = dj.makeApp([
['^/$', function(req, res) {
dj.respond(res, '<h1>djangode template demo</h1> \
<ul> \
<li><a href="/template">The raw template</a></li> \
<li><a href="/context">The test context</a></li> \
<li><a href="/text">The template rendered as text</a></li> \
<li><a href="/html">The template rendered as html</a></li> \
</ul> \
');
}],
['^/template$', function (req, res) {
dj.serveFile(req, res, 'template-demo/template.html');
}],
['^/context$', function (req, res) {
dj.respond(res, sys.inspect(test_context), 'text/plain');
}],
['^/text$', function (req, res) {
template_loader.load_and_render('template.html', test_context, function (error, result) {
if (error) {
dj.default_show_500(req, res, error);
} else {
dj.respond(res, result, 'text/plain');
}
});
}],
['^/html$', function (req, res) {
template_loader.load_and_render('template.html', test_context, function (error, result) {
if (error) {
dj.default_show_500(req, res, error);
} else {
dj.respond(res, result, 'text/plain');
}
});
}],
['^/(template-demo/.*)$', dj.serveFile],
]);
dj.serve(app, 8009);
process.djangode_urls = app.urls;