为什么If / Else Spacebars标签不起作用?

时间:2015-04-16 09:44:59

标签: meteor spacebars

嘿,由于某种原因,我的模板中的if / else语句不起作用:

<div class="col-md-4">
  <div class="row">
    <div id="custom-search-input">
      <div class="form-group col-md-12">
        <div class="input-group col-md-12">
          {{#if addingSubreddit}}
            <input class="form-control input-lg" type="text">
            <span class="input-group-btn" style="width: 2%;">
              <button class="btn btn-info btn-lg" id="addSubreddit" type="submit">
                <i class="glyphicon glyphicon-plus"></i>
              </button>
          {{else}}
            <button id="newSubreddit" class="btn btn-info btn-lg" type="submit">
              <i class="glyphicon glyphicon-plus"></i>
            </button>
          {{/if}}
          </span>
        </div>
      </div>
    </div>
  </div>
</div>

它给了我这个错误:

Your app is crashing. Here's the latest log.

Errors prevented startup:

While building the application:
client/views/subreddits.html:14: Unexpected {{else}}
...n>           {{else}}           hey      ...
                        ^

Your application has errors. Waiting for file change.

我无法弄清楚为什么这不起作用?

2 个答案:

答案 0 :(得分:4)

您的span标签错误。您已在span标记之外编码{{#if}},但在标记内编码了{{else}},这是错误的。

相反,这可能会解决问题:

<div class="col-md-4">
 <div class="row">
  <div id="custom-search-input">
   <div class="form-group col-md-12">
    <div class="input-group col-md-12">
      {{#if addingSubreddit}}
        <input class="form-control input-lg" type="text">
        <span class="input-group-btn" style="width: 2%;">
          <button class="btn btn-info btn-lg" id="addSubreddit" type="submit">
            <i class="glyphicon glyphicon-plus"></i>
          </button>
        </span>
      {{else}}
        <span class="input-group-btn" style="width: 2%;">
          <button id="newSubreddit" class="btn btn-info btn-lg" type="submit">
            <i class="glyphicon glyphicon-plus"></i>
          </button>
        </span>
      {{/if}}
    </div>
  </div>
</div>

答案 1 :(得分:1)

你不能让标签在封锁助手里面公开,所以你必须像这样重写你的模板代码:

<div class="col-md-4">
  <div class="row">
    <div id="custom-search-input">
      <div class="form-group col-md-12">
        <div class="input-group col-md-12">
          {{#if addingSubreddit}}
            <input class="form-control input-lg" type="text">
          {{/if}}
          <span class="input-group-btn" style="width: 2%;">
            {{#if addingSubreddit}}
              <button class="btn btn-info btn-lg" id="addSubreddit" type="submit">
                <i class="glyphicon glyphicon-plus"></i>
              </button>
            {{else}}
              <button id="newSubreddit" class="btn btn-info btn-lg" type="submit">
                <i class="glyphicon glyphicon-plus"></i>
              </button>
            {{/if}}
          </span>
        </div>
      </div>
    </div>
  </div>
</div>