Handlebarsjs检查字符串是否等于值

col*_*all 33 string logical-operators handlebars.js

是否可以在Handlebars中检查字符串是否等于另一个值而不注册帮助程序?我似乎无法在Handlebars参考中找到与此相关的任何内容.

例如:

{{#if sampleString == "This is a string"}}
...do something
{{/if}}
Run Code Online (Sandbox Code Playgroud)

Mih*_*ail 45

看来你不能"直接"做到这一点

尝试使用助手,为什么不呢?

在您的javascript代码中注册助手:

Handlebars.registerHelper('ifEquals', function(arg1, arg2, options) {
    return (arg1 == arg2) ? options.fn(this) : options.inverse(this);
});
Run Code Online (Sandbox Code Playgroud)

在模板中使用:

{{#ifEquals sampleString "This is a string"}}
    Your HTML here
{{/ifEquals}}
Run Code Online (Sandbox Code Playgroud)

这里有更多细节: handlebars.js {{#if}}条件中的逻辑运算符

UPD: 另一种方式:

假设您的数据是:

var data = {
    sampleString: 'This is a string'
};
Run Code Online (Sandbox Code Playgroud)

然后(使用jQuery):

$.extend(data, {isSampleString: function() {
    return this.sampleString ==  'This is a string';}
});
Run Code Online (Sandbox Code Playgroud)

使用模板:

{{#if isSampleString}}
    Your HTML here
{{/if}}
Run Code Online (Sandbox Code Playgroud)


Ker*_*ael 14

上一个带有match 的答案对我不起作用,我在if语句中出现错误(类似于“必须只有一个参数”)。

但是,我只是在这里找到了解决方案而无需编写更多帮助程序:

{{#if (eq person "John")}} hello {{/if}}
Run Code Online (Sandbox Code Playgroud)

  • 但我相信要使“eq”起作用,您需要拥有 Ember Truth Helper 插件,如 [此处](/sf/answers/2037070031/) 所述。就我而言,我没有那个插件。 (8认同)
  • 这会导致 `message: "Missing helper: "eq"`。因此,您首先需要注册此助手。 (4认同)

小智 8

我会像这样使用帮手:

Handlebars.registerHelper('ifeq', function (a, b, options) {
    if (a == b) { return options.fn(this); }
    return options.inverse(this);
});

Handlebars.registerHelper('ifnoteq', function (a, b, options) {
    if (a != b) { return options.fn(this); }
    return options.inverse(this);
});
Run Code Online (Sandbox Code Playgroud)

然后在你的代码中:

{{#ifeq variable "string"}} 
    ... do this ... 
{{/ifeq}}
{{#ifnoteq variable "string"}} 
    ... do this ... 
{{/ifnoteq}}
Run Code Online (Sandbox Code Playgroud)


小智 7

对于不需要依赖项的单行解决方案,您可以使用:

Handlebars.registerHelper('eq', (a, b) => a == b)
Run Code Online (Sandbox Code Playgroud)

然后将其用作其他示例所示的嵌套调用:

{{#if (eq sampleString "This is a string"}}
...do something
{{/if}}
Run Code Online (Sandbox Code Playgroud)


Vic*_*yew 6

使用handlebars-helpers,它提供了eq助手


小智 5

刚刚从谷歌搜索到如何检查一个字符串是否等于另一个字符串的这篇文章。

我在NodeJS服务器端使用HandlebarsJS,但我也在前端使用相同的模板文件使用浏览器版本的HandlebarsJS来解析它。这意味着如果我想要一个自定义助手,我必须在 2 个不同的地方定义它,或者为有问题的对象分配一个函数 - 太费力了!!

人们忘记的是某些对象具有可以在 mustache 模板中使用的继承函数。在字符串的情况下:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/match

包含整个匹配结果和任何括号捕获的匹配结果的数组;如果没有匹配项,则为 null。

我们可以使用此方法返回匹配数组,或者null如果未找到匹配项。这是完美的,因为查看 HandlebarsJS 文档http://handlebarsjs.com/builtin_helpers.html

您可以使用 if 帮助器有条件地渲染块。如果它的参数返回 false、undefined、null、""、0 或 [],Handlebars 将不会渲染块。

所以...

{{#if your_string.match "what_youre_looking_for"}} 
String found :)
{{else}}
No match found :(
{{/if}}
Run Code Online (Sandbox Code Playgroud)

更新:

在所有浏览器上测试后,这在 Firefox 上不起作用。HandlebarsJS 将其他参数传递给函数调用,这意味着当 String.prototype.match 被调用时,第二个参数(即上述文档中匹配函数调用的 Regexp 标志)似乎正在被传递。Firefox 认为这是不推荐使用的 String.prototype.match,因此中断。

一种解决方法是为 String JS object 声明一个新的功能原型,并使用它:

if(typeof String.includes !== 'function') {
    String.prototype.includes = function(str) {
        if(!(str instanceof RegExp))
            str = new RegExp((str+'').escapeRegExp(),'g');
        return str.test(this);
    }
}
Run Code Online (Sandbox Code Playgroud)

确保运行 Handlebars.compile() 函数之前包含此 JS 代码,然后在模板中...

{{#your_string}}
    {{#if (includes "what_youre_looking_for")}} 
        String found :)
    {{else}}
        No match found :(
    {{/if}}
{{/your_string}}
Run Code Online (Sandbox Code Playgroud)