【问题标题】:Meteor blaze, Use helper / variable in Content Blocks (contentFor)Meteor blaze,在内容块 (contentFor) 中使用帮助程序/变量
【发布时间】:2016-04-07 09:28:01
【问题描述】:

我有一个模板 (navBarContent),当我插入它时,我会收到一个“标题”。 然后我可以在该模板中访问 {{title}},但无法在嵌入在 navBarContent 模板中的 {{#contentFor}} 块中访问它:

<template name="map">
   {{>navBarContent title="MAP"}}

   ... other content ...
<template>


<template name="navBarContent ">
    {{title}}

    {{#contentFor "headerTitle"}}
        <h1 class="title">{{title}}</h1>
    {{/contentFor}}
</template>

我已经尝试“转发”标题:

<template name="navBarContent ">
    {{title}}

    {{#contentFor "headerTitle" title="MAP"}}
        <h1 class="title">{{title}}</h1>
    {{/contentFor}}
</template>

这会产生以下错误: 第一个参数必须是一个函数,才能在其余参数上调用;

编辑:

好的。我认为数据范围如下:

<template name="layout">
    {{> yield "headerTitleRenderedInLayout"}}
    {{> yield}}               
</template>

<template name='map'>
    {{> yield "headerTitleRenderedInTemplate"}}
    {{>navBarContent title="PARAMETER_TITLE"}}
</template>

<template name="navBarContent">
    {{title}}   <!-- output: PARAMETER_TITLE -->

    {{#contentFor "headerTitleRenderedInLayout"}}
        <h1 class="title">{{title}}</h1> <!-- output: LAYOUT_DATA_TITLE -->
    {{/contentFor}}

    {{#contentFor "headerTitleRenderedInTemplate"}}
        <h1 class="title">{{title}}</h1> <!-- output: TEMPLATE_DATA_TITLE -->
    {{/contentFor}}
</template>

当我使用以下路由器选项时,会产生以上输出:

Router.route('/map', function () {
    this.layout("layout", {
        data: function() {
            return { title:  "LAYOUT_DATA_TITLE" }
        }
    });

    this.render('map', {
        data: function() {
            return { title:  "TEMPLATE_DATA_TITLE" }
        }
    });
});

我的应用程序有一个导航栏,它在我的主布局中定义,因此我需要为我的路线中的布局设置数据上下文。到目前为止一切顺利,但我想根据我传递的值设置该数据上下文:

{{>navBarContent title="PARAMETER_TITLE"}}

这只是一个装饰性的东西,因为我更喜欢在不同的模板而不是路由中定义我的导航栏内容。

【问题讨论】:

  • 为什么要在模板中将数据传递给defaultNavBar?当您使用 Iron Router 提供数据上下文时,它应该可以工作:iron-meteor.github.io/iron-router/…
  • 因为我不通过 IronRouter 调用 defaultNavBar。它在另一个模板中使用。路线前往:&lt;template name="map"&gt; {{&gt;defaultNavBar title="MAP"}} .... other content ... &lt;/template&gt;
  • 您是否将另一个数据上下文传递给route 函数中的map 模板?
  • 不,我没有传递任何数据上下文:Router.route('/map');
  • 我认为它仅在您将 { title: 'MAP' } 作为数据上下文传递给 route 函数时才有效,因为 Iron Router 会处理 contentFor 块的呈现。

标签: meteor handlebars.js iron-router meteor-blaze spacebars


【解决方案1】:

当您将数据上下文传递给 render 函数时它会起作用,因为 Iron Router 处理 contentFor 块:

Router.route('/', function() {
  this.render('map', {
    data: function() {
      return { title: 'MAP' }
    }
  });
});

那么下面的模板显示两次MAP

<template name="map">
  {{> navBarContent }}
  {{> yield 'anotherBlock'}}
</template>

<template name="navBarContent">
  <div>
    In "map": {{title}}
  </div>

  {{#contentFor 'anotherBlock'}}
    <div>
      In contentFor: {{title}}
    </div>
  {{/contentFor}}
</template>

【讨论】:

  • 好吧,看来我们越来越近了。只要 {{> yield}} 在地图模板中,我就可以传递数据上下文。不幸的是,导航栏是在地图模板之外定义的。 IE。 contentFor 指的是不属于地图数据上下文的元素。
  • 您可以在问题中添加更多代码吗?我不确定我明白你的意思。数据到底是在哪里定义的?
  • 我认为不可能像这样传递标题,因为从模板参数构建的数据上下文总是被 Iron Router 替换。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-03-12
  • 2016-12-24
  • 1970-01-01
  • 1970-01-01
  • 2011-10-10
  • 1970-01-01
相关资源
最近更新 更多