【问题标题】:Jade: Conditional statement not working correctly: not comparing at allJade:条件语句无法正常工作:根本不比较
【发布时间】:2015-06-17 17:05:33
【问题描述】:

我的 .jade 模板中有这个条件语句:

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

当我在 else 语句中打印 '{{merchant}}' 时,它的输出与“The Restaurant”完全一样。因此,条件应该评估为真。

当我重新编写代码时:

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

它确实有效。我不明白为什么 if 条件仍然评估为假。有什么建议么?谢谢!

【问题讨论】:

  • 尝试删除第一行- var merchant = "{{merchant}}"
  • 好的,删除它。然后我用 {{merchant}} 替换了 if 语句中的商家,但现在它给了我一个语法错误。有什么建议吗?
  • 如果你只删除第一行会发生什么?
  • 什么都没有发生。没有语法错误,什么都没有。我的意思是,它应该如何知道商家的价值,因为它没有在 .jade 文件中的任何地方定义?
  • @Aweary 我试过了。仍然进入 else 条件。

标签: node.js pug


【解决方案1】:

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

Jade 还使用 #{} 进行字符串插值,而不是像 Handlebars 那样使用 {{}}。因此,将您的代码更改为以下内容,它将正常工作

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

我能够通过以下 Express 路线成功完成这项工作

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

【讨论】:

    猜你喜欢
    • 2013-07-18
    • 1970-01-01
    • 2022-07-16
    • 2016-11-23
    • 1970-01-01
    • 1970-01-01
    • 2016-05-08
    • 2016-03-05
    • 2019-07-10
    相关资源
    最近更新 更多