【发布时间】: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。它在另一个模板中使用。路线前往:
<template name="map"> {{>defaultNavBar title="MAP"}} .... other content ... </template> -
您是否将另一个数据上下文传递给
route函数中的map模板? -
不,我没有传递任何数据上下文:
Router.route('/map'); -
我认为它仅在您将
{ title: 'MAP' }作为数据上下文传递给route函数时才有效,因为 Iron Router 会处理contentFor块的呈现。
标签: meteor handlebars.js iron-router meteor-blaze spacebars