javascript - 想要比较 Node js应用程序中 Handlebars View 中的两个字符串

标签 javascript node.js express compare handlebars.js

我想在express-nodejs应用程序的 Handlebars 模板中进行一些比较。

它看起来像这样:

{{# if(x==y)}}
    equal string
 {{else}}
    not equal string
 {{/if}}

我已经安装了 Handlebars 和express-handlebars 软件包。我读到了有关 Handlebars 助手的信息,但无法找到正确使用它们的方法。我尝试在应用程序的 app.js 文件中添加帮助程序,但无法在 View 中的模板文件中使用它们。

任何帮助都会有帮助和感激。

谢谢

最佳答案

您可以像这样使用 is 函数:

 {{#is x "my_string"}}
     x is "my_string"
 {{else}}
     x isn't "my_string"
 {{/is}}

否则,您可以使用这个著名的助手:

Handlebars.registerHelper('if_equal', function(a, b, opts) {
    if (a == b) {
        return opts.fn(this)
    } else {
        return opts.inverse(this)
    }
})

像这样使用它:

{{#if_equal x "my_string"}}
     x is "my_string"
{{else}}
     x isn't "my_string"
{{/if_equal}}

关于javascript - 想要比较 Node js应用程序中 Handlebars View 中的两个字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40956767/

相关文章:

javascript - 将 API 中的数据插入 DOM

javascript - 函数装饰器中return语句的作用是什么

node.js - 如何安全使用 Apache Cassandra?

javascript - 配置 JSON 响应

javascript - 按日期范围的 Firebase 查询

javascript - mousedown 事件本质上比 mouseup 事件慢吗?

node.js - 使用Webpack构建ReactJS项目导致graceful-js报告错误 "can' t解析 'fs'”

node.js - 使用 sequelize 原始查询批量插入到 postgres

node.js - 如何知道文件下载何时完成?

node.js - 特定端点上的 Socket.io