根据文档中的值在Meteor和Iron Router中呈现模板

根据文档中的值在Meteor和Iron Router中呈现模板,meteor,iron-router,Meteor,Iron Router,我试图根据文档中字段的值呈现模板 我尝试在助手中使用开关盒,但返回值不正确 单位列表.html unit_item.html 我不是走错了路就是错过了一些基本的东西 我删掉了很多代码来关注这个问题 有没有关于如何使其工作的想法,或者关于如何更好地工作的建议?执行时,您不能从JS返回未编译的空格键字符串 您可以使用Router.path在模板帮助器中获取路由的路径: Template.unitItem.helpers({ unitType: function() { var unitT

我试图根据文档中字段的值呈现模板

我尝试在助手中使用开关盒,但返回值不正确

单位列表.html unit_item.html 我不是走错了路就是错过了一些基本的东西

我删掉了很多代码来关注这个问题


有没有关于如何使其工作的想法,或者关于如何更好地工作的建议?

执行时,您不能从JS返回未编译的空格键字符串

您可以使用
Router.path
在模板帮助器中获取路由的路径:

Template.unitItem.helpers({
  unitType: function() {
    var unitType = this.unitType;
    switch(unitType){
      case 'first':
        return Router.path('unitPageFirst', this);
      case 'second':
        return Router.path('unitPageSecond', this);
    }
  }
});
或者,您可以通过声明模板帮助程序来使用普通空格键,以对照
unitType
进行检查

HTML


看看《Iron router指南》中的渲染模板,特别是this.render('xyz')语句


尝试替换
返回“{{pathFor'unitPageFirst'}”返回Router.path('unitPageFirst')
,并添加
break
语句以切换大小写是否可以发布出版物?Meteor.publish('units',function(){return units.find();});谢谢你的回复。我遇到的问题是,现在它正在寻找名为“unitPageFirst”的路由。如果不使用开关盒,则可以进入特定单元的页面,路径为/units/dsifuh786s8df76(id号)。如果我创建路线,然后我需要传递单元的_id吗?我已经创建了路由,现在它正确地路由到/unitPageFirst/_id。然而,我的最终目标是让用户填写类型字段,应用程序使用该值创建路由,并根据用户在表单上填写的其他字段动态创建布局模板。
Template.unitsList.helpers({
  units: function() {
    return Units.find({}, {sort: {name: 1}});
  }
});
<template name="unitItem">
  <a href="{{unitType}}">{{name}}</a>
</template>
Template.unitItem.helpers({
  unitType: function() {
    var unitType = this.unitType;
    switch(unitType){
      case 'first': return "{{pathFor 'unitPageFirst'}}";
      case 'second': return "{{pathFor 'unitPageSecond'}}";
    }
  }
});
Template.unitItem.helpers({
  unitType: function() {
    var unitType = this.unitType;
    switch(unitType){
      case 'first':
        return Router.path('unitPageFirst', this);
      case 'second':
        return Router.path('unitPageSecond', this);
    }
  }
});
<template name="unitItem">
  {{#if unitTypeIs 'unitTypeFirst'}}
    <a href="{{pathor 'unitTypeFirst'}}">{{name}}</a>
  {{/if}}
  {{#if unitTypeIs 'unitTypeSecond'}}
    <a href="{{pathor 'unitTypeSecond'}}">{{name}}</a>
  {{/if}}
</template>
Template.unitItem.helpers({
  unitTypeIs: function(unitType){
    return this.unitType == unitType;
  }
});