特定のコレクションのテーブルをレンダリングする Meteorite パッケージを作成しています。オプションのカスタム列をテンプレートとして追加して、このテーブルをカスタマイズ可能にしたい:
<template name="myPluginTable">
<table>
<thead>
<th>Column 1</th>
<th>Column 2</th>
{{> customHeaders}}
</thead>
<tbody>
{{#each objects}}
<tr>
<td>{{firstValue}}</td>
<td>{{secondValue}}</td>
{{> customFields}}
</tr>
{{/each}}
</tbody>
</table>
</template>
テンプレートがアプリのどこかに指定されcustomHeaders
ている限り、これは問題なく機能します。customFields
ただし、これらのテンプレートを省略すると、エラーがスローされます。ダミー テンプレートをパッケージに追加しても、上書きしようとするとエラーが発生するため、これを回避することはできません。
私が思いついた解決策:
Template.myPluginTable.helpers({
customHeadersExist: function() {
return typeof(Template.customHeaders) === "object";
},
customFieldsExist: function(user) {
return typeof(Template.customFields) === "object";
},
});
そしてテンプレートで:
{{#if customHeadersExist}}
{{> customHeaders}}
{{/if}}
と
{{#if customFieldsExist}}
{{> customFields}}
{{/if}}
これで動作しますが、このような単純なタスクの場合、ソリューションは明らかに複雑に見えます。オプションのテンプレートごとにヘルパー関数を宣言する必要があるのでしょうか? だから私の質問は本当にです:
パッケージ カスタマイズ テンプレートをオプションにする最善の方法は何ですか? これですか、それとももっと良い方法はありますか?