Imagine you have an application that calculates some server-side values, such as a list of chat rooms.
Then you have 2 reasonable solutions:
- send this data as JSON so that some side of the JavaScript client builds a list.
- create a server side list HTML
A template system is useful for this second solution.
HTML-, , nodejs HTML, .
JS (, ):
app.get('/rooms', ensureAuthenticated, ensureCompleteProfile, function(req, res){
db.on(req.user.id)
.then(function(uid){
return [
this.listAccessibleRooms(uid),
this.fetchUserPingRooms(uid, 0)
]
}).spread(function(accessibleRooms, pings){
var rooms = {public:[], private:[]};
accessibleRooms.forEach(function(r) {
r.path = roomPath(r);
rooms[r.private?'private':'public'].push(r);
});
res.render('rooms.jade', { rooms:rooms, pings:pings });
}).finally(db.off);
});
Jade :
table.list
each room, i in rooms.public
tr
th: a(href=room.path) #{room.name}
td.rendered #{room.description}
if room.auth
td.role= room.auth
HTML:
<table class="list">
<tr>
<th><a href="path1">Room 1</a></th>
<td class="rendered">Description of room 1</td>
<td class="role">Admin</td>
</tr>
<tr>
<th><a href="path2">Room 2</a></th>
<td class="rendered">Description of room 2</td>
</tr>
</table>
, . , HTML, , , , HTML.
Miaou . Jade rooms.jade.