Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
458 views
in Technique[技术] by (71.8m points)

if statement - Check for equality in Spacebars?

I am trying to do what I think should be a very simple task, but have been failing to do so in the past hour. I want to select a select option by default if the user property matches the value.

<select name="myName">
  {{#each addKeys myTable}} <!-- addKeys creates variables for keys and values -->
    <option value="{{key}}" {{#if currentUser.property === key}}selected="selected"{{/if}}>{{value}}</option>
  {{/each}}
</select>

Now I thought this was straightforward enough to be implemented. But it turns out that Spacebars do not allow conditional operators other than the negation exclamation mark, so equal signs are out of question. I then tried something horrible for the sake of trying:

In template myTemplate:

<select name="myName">
  {{#each addKeys myTable}}
    <option value="{{key}}" {{isSelected currentUser.property key}}>{{value}}</option>
  {{/each}}
</select>

In mytemplate.js :

Template.myTemplate.helpers({
  isSelected: function(v1, v2) {
    if (v1 === v2)
      return "selected="selected"";
    return '';
  }
});

Not only is this code terrible, terrible to look at, it does not work:

Exception in Meteor UI: String contains an invalid character

I don't understand why something that simple seems so impossible to achieve. Am I missing something there?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Here's an overview of {{#if}} statements in Spacebars

Property

Of course the simplest possible implementation is when the scoped object has a property that evaluates to a boolean

For example if you had:

var item = {
  text: 'hello',
  checked: false
};

Then you could evaluate an if block like this:

class="{{#if checked}}checked{{/if}}"

Function

We could also evaluate a function here as well. Rather than add a function to the item, we can add a function to the helper and it will inherit the datacontext of the item being passed to it. So if we had the following helper:

Template.item.helpers({
    saysHi: function() {
      return this.text === "hi";
    }
});

Then we could run the following code:

<template name="item">
   {{text}}
   {{#if saysHi}} - Hi Back {{/if}}
</template>

Note: The helper's implementation can access the current data context as this.

Function with Parameters

You can also pass any number of parameters to helper functions like this:

Template: {{frob a b c verily=true}}
Helper Call: frob(a, b, c, Spacebars.kw({verily: true}))

When applied to our if block, we can do the following:

{{#if equals owner currentUser._id}}
    <button class="delete">&times;</button>
{{/if}}

Then add equals to our template helper like this:

Template.item.helpers({
    equals: function(v1, v2) {
        return (v1 === v2);
    }
});

Universal Helpers

Since this is a generic method that could be useful anywhere, we should add it to every template instead of recreating it.

Note: To create a helper that can be used in any template, use Template.registerHelper.

Template.registerHelper('equals',
    function(v1, v2) {
        return (v1 === v2);
    }
);

Here's a working Demo in MeteorPad that includes one of each of the types of IF's constructed here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...