2015-06-17 76 views
0

比较我有这个条件语句在我.jade模板:玉:条件语句不能正常工作:不是在所有

- var merchant = "{{merchant}}" 
if merchant == "The Restaurant" 
    h1 It worked! 
else 
    h1 "{{merchant}}" 

当我打印“{{商家}}”在我else语句,说到正如“餐厅”一样。因此,条件应该评估为真。

当我重新工作,我的代码,所以:

- var merchant = "The Restaurant" 
if merchant == "The Restaurant" 
    h1 It worked! 
else 
    h1 "{{merchant}}" 

它的实际工作。我不明白为什么if条件仍然评估为false。有什么建议么?谢谢!

+0

尝试删除第一行'成功合作 - VAR商人=“ {{merchant}}“' –

+0

好的,删除它。然后,我用{{merchant}}替换了if语句中的商家,但现在它给了我一个语法错误。建议? –

+0

如果你只是删除第一行会发生什么,没有别的? –

回答

0

如果您使用Express.js并通过res.render方法提供merchant变量,则不需要再次声明该变量。它将在渲染环境中可用。

Jade也使用#{}进行字符串插值,而不是{{}},像Handlebars。所以,你的代码更改为以下内容,它会正常工作

if merchant == "The Restaurant" 
    h1 It worked! 
else 
    h1 #{merchant} 

我能得到这个具有以下快递航线

router.get('/', function(req, res, next) { 
    res.render('index', { merchant: 'The Restaurant' }); 
});