【问题标题】:Passing variables into Meteor templates and using said variables in handlebar if statements将变量传递到 Meteor 模板并在句柄 if 语句中使用所述变量
【发布时间】:2015-08-26 03:28:01
【问题描述】:

我正在尝试将变量传递给 Meteor.js 模板,然后在 if 语句(在所述模板中)中使用该变量。我可以做的一种解决方案是简单地使用三个单独的模板,而不是使用 if 语句将一个模板分开,但我想知道是否有更好的方法来执行它。我知道您不能在其他 {{}} 中使用 {{}},并且句柄 if 语句似乎也无法处理相等比较。

这是我的代码(idType 是我要传递的变量):

<template name="license">
    {{> idForm idType="license"}}
</template>

<template name="idForm">
    <form class="idForm">
        {{#if idType=="license"}}
            <span>Driver's License Details</span>
            <input type="text" id="su-licenseNo" placeholder="License Number" />
            <input type="text" id="su-surname" placeholder="Surname" />
            <input type="text" id="su-givenNames" placeholder="Given Names" />
            <input type="text" id="su-dateOfIssue" placeholder="Date Of Issue" />
            <input type="text" id="su-dateOfExpiry" placeholder="Date Of Expiry" />
            <input type="text" id="su-height" placeholder="Height" />
        {{/if}}

        {{#if idType=="passport"}}
            <span>Passport Details</span>
            <input type="text" id="su-type" placeholder="Type" />
            <input type="text" id="su-issuingCountry" placeholder="Issuing Country" />
            <input type="text" id="su-passportNo" placeholder="Passport Number" />
            <input type="text" id="su-surname" placeholder="Surname" />
            <input type="text" id="su-givenNames" placeholder="Given Names" />
            <input type="text" id="su-nationality" placeholder="Nationality" />
            <input type="text" id="su-dateOfIssue" placeholder="Date Of Issue" />
            <input type="text" id="su-dateOfExpiry" placeholder="Date Of Expiry" />
        {{/if}}

        {{#if idType=="sin"}}
            <span>SIN Details</span>
            <input type="text" id="su-surname" placeholder="Surname" />
            <input type="text" id="su-givenNames" placeholder="Given Names" />
            <input type="text" id="su-number" placeholder="SIN" />
        {{/if}}

        <button>Submit!</button>
    </form>
</template>

【问题讨论】:

    标签: meteor handlebars.js


    【解决方案1】:

    您无法在 {{#if ...}} 车把语句中进行比较,{{#if idType=="license"}} 不是有效的车把语法。

    您可以通过comparison helper 来解决此问题,或者您可以稍微改变您的用法并执行以下操作:

    <template name="license">
      {{> idForm isLicense=1}}
    </template>
    
    <template name="idForm">
      <form class="idForm">
        {{#if isLicense}}
          ...
        {{/if}}
        {{#if isPassport}} <!-- this will be false since it was omitted -->
          ...
        {{/if}}
      </form>
    </template>
    

    【讨论】:

    • 好主意!我很可能会采用这种方法。非常感谢。
    • 我刚刚在使用这个方法时遇到了一个问题——当我将变量 isLicense 添加到模板调用中时,它会覆盖通过帮助程序初始化的所有其他变量(所有返回的对象为空白)。有什么想法吗?
    • 您必须发布您的帮助代码。也不知道您的助手正在操作的数据上下文。检查你的助手 this 是否正确。 Docs on hash arguments
    • 是的,事实证明我正在覆盖数据上下文。我在这里找到了解决方案:stackoverflow.com/questions/28015971/…。感谢您的帮助。
    猜你喜欢
    • 1970-01-01
    • 2011-08-31
    • 1970-01-01
    • 2017-03-25
    • 2013-05-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多