gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 13:51:58 34 4
gpt4 key购买 nike

我想在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/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com