最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Compare template helper values in Spacebars {{#if}} block - Stack Overflow

programmeradmin2浏览0评论

I need to pare two template helper values that are located in nested templates. I was wondering if there is an easy way to pare two template helpers (one from the parent template) in an {{#if}} statement like so:

{{#each bids}}
  {{#if bid.price===../job.price}}
    <span>some text</span>
  {{else}}
    <span>some other text</span>
  {{/if}}
{{/each}}

If you can't do this, I guess the other option is to use Template.parentData in a new template inside the each block? I'm not opposed to this, just the way I outlined above would be much faster and simpler if it's possible. Thanks.

I need to pare two template helper values that are located in nested templates. I was wondering if there is an easy way to pare two template helpers (one from the parent template) in an {{#if}} statement like so:

{{#each bids}}
  {{#if bid.price===../job.price}}
    <span>some text</span>
  {{else}}
    <span>some other text</span>
  {{/if}}
{{/each}}

If you can't do this, I guess the other option is to use Template.parentData in a new template inside the each block? I'm not opposed to this, just the way I outlined above would be much faster and simpler if it's possible. Thanks.

Share asked Feb 6, 2015 at 16:02 bgmasterbgmaster 2,3334 gold badges28 silver badges42 bronze badges 4
  • So I ended up getting my code to work by running bids.forEach(function(bid) { bid.match = bid.price===Template.currentData().job.price; }); in my bids helper, effectively adding the match condition to the bids helper. I'd still be interested to know if spacebars can handle parison logic or not. From everything I've searched for, it doesn't look like it does. – bgmaster Commented Feb 6, 2015 at 16:25
  • You could also just register an equals function Template.registerHelper('equals', function(a, b) {return a===b;}), but I like Peppe L-G's idea of registering underscore better. – user3557327 Commented Feb 6, 2015 at 17:13
  • Agreed, it's more elegant and flexible. – bgmaster Commented Feb 6, 2015 at 17:40
  • raix:handlebar-helpers serves a similar purpose – Dean Radcliffe Commented Aug 11, 2015 at 22:40
Add a ment  | 

2 Answers 2

Reset to default 11

Something like this might work for you:

Template.registerHelper('_', function(){
    return _
})
{{#each bids}}
  {{#if _.isEqual bid.price ../job.price}}
    <span>some text</span>
  {{else}}
    <span>some other text</span>
  {{/if}}
{{/each}}

As a bonus, you not only get _.isEqual, but all _.* functions.

I believe you are right, spacebars doesn't support logical statements in the #if condition. But you can easily define a new block helper that does:

HandlebarsRegister.registerHelper('ifx', function(conditional, options) {
    var truthValue = false;
    try {
        truthValue = eval(conditional);
    } catch (e) {
        console.log("Exception in #ifx evaluation of condition: ",
                    conditional, e);
    }
    if (truthValue) {
        return options.fn(this);
    } else {
        return options.inverse(this);
    }
});

and then use

{{#each bids}}
  {{#ifx "bid.price===../job.price"}}
    <span>some text</span>
  {{else}}
    <span>some other text</span>
  {{/ifx}}
{{/each}}

The only downside is that you will need to write your condition as a string and eval is not exactly elegant. But hey, it works.

发布评论

评论列表(0)

  1. 暂无评论