Meteor 有条件地显示嵌套模板

Meteor Conditionally displaying nested templates

我有一个包含多个子嵌套模板的模板,这些模板应根据保存在 TemplateC 集合中的数据有条件地显示,如下所示,因此我在模板中使用了 if 条件,如下所示,但我始终拥有所有子尽管条件 return 为真或假,仍显示模板。有人可以检查我的代码并告诉我我在这里缺少什么吗?谢谢

        var templateArray = ['false', 'false'];

        Template.formBuilderPreview.created = function() {

            var cursor = TemplatesC.find({}, { sort: { templateCode: 1 }});    
            if (!cursor.count()) return;

            cursor.forEach(function (row) {
              //only case 1 return in the switch below as case 2 never exist
                switch(row.templateCode) {
                    case 1: templateArray[0] = true; break;
                    case 2: templateArray[1] = true; break;
                    default: templateArray[0] = true;
                }

            });
        };


        Template.formBuilderPreview.helpers({

            template1box: function(){      
                console.log(templateArray[0]);  //This returns true
                return templateArray[0];
            },
            template2box: function(){    
                console.log(templateArray[1]); //This returns false
                return templateArray[1];
            }

        });

模板:

    <template name="formBuilderPreview">

        <div id="fullpage">
            {{#if template1box}}                
                {{> temp01}}
            {{/if}}

            {{#if template2box}}                        
                {{> temp02}}
            {{/if}}            
        </div>

    </template>

把那些帮手放在一起。

Template.formBuilderPreview.helpers({
     template1box: function(){      
        if(templateArray[1]){
             return true;
          }else{
             return false;
          }
      });

现在模板应该如下所示。

<template name="formBuilderPreview">
     {{#if template1box}}  
     <!-- If helper return TRUE this temp01 will be showed. -->              
            {{> temp01}}
        {{else}}
     <!-- If helper return FALSE this temp01 will be showed. --> 
            {{> temp02}}
      {{/if}}
 </template>

你得到助手的想法,只在 1 个助手上实现,退休 true/false

你定义了一个字符串数组,我认为这是造成问题的原因,所以我建议你更改

var templateArray = ['false', 'false'];

var templateArray = [false, false];

而且会很顺利