Handlebarsjs检查字符串是否等于值 [英] Handlebarsjs check if a string is equal to a value

查看:736
本文介绍了Handlebarsjs检查字符串是否等于值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Is it possible in Handlebars to check if a string is equal to another value without registering a helper? I can't seem to find anything relevant to this in the Handlebars reference.

例如:

{{#if sampleString == "This is a string"}}
...do something
{{/if}}

推荐答案

看来您不能直接"完成

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

Try use helper, why not?

在您的javascript代码中注册帮助程序:

Register helper in your javascript code:

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

在模板中使用:

{{#ifEquals sampleString "This is a string"}}
    Your HTML here
{{/ifEquals}}

此处有更多详细信息: handlebars.js中的逻辑运算符{{#if}}有条件

More details here: Logical operator in a handlebars.js {{#if}} conditional

更新: 另一种方式:

让我们假设,您的数据是:

lets assume, your data is:

var data = {
    sampleString: 'This is a string'
};

然后(使用jQuery):

Then (using jQuery):

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

使用模板:

{{#if isSampleString}}
    Your HTML here
{{/if}}

这篇关于Handlebarsjs检查字符串是否等于值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆