私は Meteor にかなり慣れていませんが、ネストされたビュー (別名、動的インクルードまたはサブテンプレート) が必要であることがすぐにわかりました。これがあなたの言いたいことかどうかはわかりませんが、これが私の解決策です。
サブテンプレートの作成に使用できる次のハンドルバー ヘルパーを作成しました。
Handlebars.registerHelper('subTemplate', function(container, property, context, options) {
if (container && container.hasOwnProperty(property)) {
var subTemplate = container[property];
if (typeof subTemplate === 'function') {
return new Handlebars.SafeString(subTemplate(context || this));
}
else if (typeof subTemplate === 'string') {
return new Handlebars.SafeString(Template[subTemplate](context || this));
}
}
});
これは、私がジェネリック テンプレートと呼んでいるものの中で使用できます。たとえば、リストを作成するには:
<template name="item_list">
<ul class="items-list">
{{#each items}}
<li class="listview-item">
{{subTemplate .. 'listItem' this}}
</li>
{{/each}}
</ul>
</template>
この汎用テンプレートを呼び出すには、'listItem' プロパティがそのコンテキスト内に存在する必要があります。これは、サブテンプレートの名前を含む文字列、またはサブテンプレートのインライン定義のいずれかです。以下の例は、両方のオプションを示しています。
<template name="my_list">
{{! First option, referring to the sub-template by name:}}
<div>
{{#with listData listItem="my_list_item"}}
{{> item_list}}
{{/with}}
</div>
{{! Second option, inlining the sub-template:}}
<div>
{{#with listData}}
{{#assignPartial 'listItem'}}
<span>{{name}}</span>
{{/assignPartial}}
{{> item_list}}
{{/with}}
</div>
</template>
<template name="my_list_item">
<span>{{name}}</span>
</template>
Template.my_list.listData = function() {
return {
items: collections.people.find()
};
};
2 番目のオプションでは、追加のハンドルバー ヘルパーが必要です。
Handlebars.registerHelper('assignPartial', function(prop, options) {
this[prop] = options.fn;
return '';
});
私はこれらの種類の便利なヘルパーをさらに作成しました。いつか GitHub で共有する予定です。