我正在玩Nodejs,我想知道我是否可以使用玉样式速记(即
a(href='/page?name=contact')
作为传递给jade文件的参数的一部分。
比如说,我想写一些内容(来自Ninja商店的例子):
var name = req.query.name;
var contents = {
about: 'a(href="/page?name=about") Ninja Store \n sells the coolest ninja stuff in the world. Anyone shopping here is cool.',
contact: 'You can contact us at <address><strong>Ninja Store</strong>,<br>1, World Ninja Headquarters,<br>Ninja Avenue,<br>NIN80B7-JP,<br>Nihongo.</address>'
};
res.render('page', { title: 'Ninja Store - ' + name, username: req.session.username, content:contents[name] });
我可以使用一种机制来编写这种代码吗?
答案 0 :(得分:2)
var jade = require('jade');
// ...
var contents = {
about: jade.compile('a(href="/page?name=about") ...')(),
contact: 'You can contact us at ...'
};
但是,您只需在视图之间切换,而不是将其存储在contents
:
res.render(name, {
title: 'Ninja Store - ' + name,
username: req.session.username
});
Aand,您仍然可以将其page.jade
与template inheritance一起包含在内。
page.jade:
#container
block contents
about.jade:
inherits page
block contents
a(href="/page?name=about") Ninja Store
span sells the coolest ninja stuff in the world. Anyone shopping here is cool.