3

我正在尝试在动态模板中呈现动态模板。

主要布局:

<template name="template1">
    {{>navbar}}
    <div class="container">
        {{>Template.dynamic template=content}}
    </div>
</template>

子模板:

<template name="template2">
<div class="container">
    <div class="row">
        <h2>Data Input</h2>
    </div>
    <div class="row">
        <ul class="nav nav-pills">
            <li class="active"><a href="/input/inc">Inc</a></li>
            <li><a href="/input/exp">Exp</a></li>
        </ul>
    </div>
    <div class="row">
        {{>Template.dynamic template=dataSection}}
    </div>
</div>

子子模板:

<template name="template3">
<h2>Inc</h2>
</template>

下面是我的 FlowRouter 代码。这是错误的,但我认为它可能会让某人知道我正在尝试做什么。流路由器:

FlowRouter.route('/input/income', {
action: function () {
    BlazeLayout.render("template1", {
        content: "template2"
    });
    BlazeLayout.render("template2", {
        dataSection: "template3"
    });
 }
});

我正在尝试在模板 1 内渲染模板 2,然后我想在模板 2 内渲染模板 3。我是否必须以某种方式在模板 2 内渲染模板 3,然后才能在模板 1 内渲染该模板?

4

2 回答 2

4

您需要使用帮助程序呈现子模板。将您的路由器代码更改为:

action: function () {
    BlazeLayout.render("template1", {
        content: "template2"
    });
}

您只需要一次渲染调用,因为 template2 将自动渲染给定的动态模板。现在在 template2 创建助手:

Template.template2.helpers({
  dataSection() {
    return "template3";
  }
});

如果您愿意,您可以用不同的逻辑替换 return 语句,只要它返回模板的名称即可。

于 2016-03-09T22:14:14.767 回答
2

您也可以在没有模板助手的情况下执行此操作:

BlazeLayout.render('App_Body', {main: 'parent', sub: 'details' });

主模板的 HTML

<template name="App_body">
   {{> Template.dynamic template=main}}
</template>

父模板的 HTML

<template name="parent">
    Parent template
    {{> Template.dynamic template=sub}}
</template>

子模板的 HTML

<template name="details">
    Sub template
</template>

与路线进行深度链接变得更加容易:

FlowRouter.route('/todos/show/:id', {
    name: 'Todo.show',
    action(params, queryParams) {
        BlazeLayout.render('App_body', {main: 'parent', sub: 'details' });
    }
});

然后在细节模板JS部分你可以得到id:

Template.todo.onCreated(function todoOnCreated() {
   var id = FlowRouter.getParam('id');
   // ...
});
于 2016-10-16T09:03:07.720 回答