这似乎是一个基本的,但我无法让Iron Router将我的模板渲染到页面上的正确位置。
在我的路由器控制器中,我有:
Router.configure({
loadingTemplate: 'loading',
notFoundTemplate: 'notFound'
});
Router.map(function () {
this.route('home', {
path: '/',
template: 'home',
layoutTemplate: 'layout'
});
this.route('posts', {
});
this.route('post', {
path: '/posts/:_id'
});
});
在布局html页面中,我有:
<body>
<!-- some other static page stuff here -->
<div class="container">
<template name="layout">
{{yield}}
</template>
</div>
</body>
主页模板的基本版本如下:
<template name="home">
<h1>Home Page</h1>
</template>
我尝试了一些变体但主页模板总是在结束体标记之前的布局模板底部而不是在div.container中呈现
答案 0 :(得分:20)
您已经错误地放置了标签。
模板需要独立完成。 body标签应为空:
<body>
<!-- There isn't anything here really -->
</body>
<template name="layout">
<div class="container">
{{>yield}}
</div>
</template>
这适用于您的home
路由,但不适用于您的post
路由,因为您尚未为其提供布局模板。
您可以设置通用布局,以便它可以在posts
和home
上工作(如果您没有在那里设置布局模板 - 这将覆盖下面的布局模板):
Router.configure({
loadingTemplate: 'loading',
notFoundTemplate: 'notFound',
layoutTemplate: 'layout'
});
因此,iron-router会将layoutTemplate放在体内。