【问题标题】:Meteor: nested templates and a pseudo switch conditionalMeteor:嵌套模板和条件伪开关
【发布时间】:2023-03-11 22:19:01
【问题描述】:

我是新的流星,目前我正在测试嵌套模板。更具体地说,我正在尝试让这个伪开关工作。

我有一个 PARENT 模板,它从 template.helper 函数中获取数据,它获取 {{#each}} 的数据。

这是父模板

<template name="result">

    {{#each Tresult}}

        <div class="jow">

            <h3>{{name}}</h3>
            <p>{{type}}</p>

            <div>{{> Tstatus}}</div>

        </div>  

    {{/each}}

</template>

PARENT 还包含另一个模板 {{> Tstatus}}

这是 CHILD 模板

<template name="Tstatus">

    {{#status_is "green"}}
        {{> Tstatus_green}}
    {{/status_is}}

    {{#status_is "red"}}
        {{> Tstatus__red}}
    {{/status_is}}

    {{#status_is "orange"}}
        {{> Tstatus__orange}}
    {{/status_is}}

</template>

<template name="Tstatus_green">

    <span>green</span>

</template>


<template name="Tstatus_red">

    <span>red</span>

</template>


<template name="Tstatus_orange">

    <span>orange {{number}}</span>

</template>

此模板还可以包含 3 个其他模板:

  • Tstatus_green
  • Tstatus_red
  • Tstatus_orange

但问题是,我如何让这个伪开关工作。所以我只需要包含 3 个模板中的 1 个,基于它的状态颜色。

这是 PARENT 模板的辅助函数

Template.result.helpers({

    Tresult:function(){

        return Ttable.find()

    }


})

【问题讨论】:

  • 什么是status_is 助手?

标签: meteor spacebars


【解决方案1】:

我会这样做:

Template.Tstatus.helpers({
    getStatusColor:function()
    {
        //"this" will be the current Ttable document
        var color = getColorFunction(this)
        return Template["Tstatus_"+color]
    }
})


<template name="Tstatus">
    {{#with getStatusColor}}
        {{>.}}
    {{/with}}
</template>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-11-22
    • 2016-06-24
    • 2014-12-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多