将参数传递给Jade文件时,我可以(我如何)使用Jade速记?

时间:2012-04-24 19:44:27

标签: javascript node.js pug

我正在玩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] });

我可以使用一种机制来编写这种代码吗?

1 个答案:

答案 0 :(得分:2)

你必须compile the string

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.jadetemplate 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.