Javascript Meteor:如何仅在模板存在时才包含模板?
我正在创建一个Meteorite包,用于呈现特定集合的表。我希望通过添加可选的自定义列作为模板来自定义此表:Javascript Meteor:如何仅在模板存在时才包含模板?,javascript,meteor,spacebars,Javascript,Meteor,Spacebars,我正在创建一个Meteorite包,用于呈现特定集合的表。我希望通过添加可选的自定义列作为模板来自定义此表: <template name="myPluginTable"> <table> <thead> <th>Column 1</th> <th>Column 2</th> {{> customHeaders}} </thead>
<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>
{{#if customHeadersExist}}
{{> customHeaders}}
{{/if}}
在模板中:
<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>
{{#if customHeadersExist}}
{{> customHeaders}}
{{/if}}
及
现在可以了,但是对于这样一个简单的任务,解决方案似乎过于复杂了——我真的需要为每个可选模板声明帮助函数吗?所以我的问题是:
使包自定义模板成为可选模板的最佳方法是什么?是这样,还是有更好的方法?我想到的最简单的解决方案是实现自定义帮助器:
UI.registerHelper('safeRender', function () {
var component = Template[this.name];
if (component) {
return component.extend({data: this.data});
}
return UI.Component;
});
您可以在模板中使用以下内容:
{{> safeRender name='customHeaders' data=.}}
data=。
部分用于确保在相同的数据上下文中呈现模板。我想到的最简单的解决方案是实现自定义帮助器:
UI.registerHelper('safeRender', function () {
var component = Template[this.name];
if (component) {
return component.extend({data: this.data});
}
return UI.Component;
});
您可以在模板中使用以下内容:
{{> safeRender name='customHeaders' data=.}}
data=。
部分用于确保模板将在相同的数据上下文中呈现。Nice,这很有效,谢谢。关于包含操作符的这种行为的空格键文档有点缺乏信息(或者说我只是个傻瓜),但我想这就是你为玩一个未完成的框架所付出的代价。然后我可以停止抱怨,自己为改进文档做出贡献。很好,这很有效,谢谢。关于包含操作符的这种行为的空格键文档有点缺乏信息(或者说我只是个傻瓜),但我想这就是你为玩一个未完成的框架所付出的代价。然后,我可以不再抱怨,自己为改进文档做出贡献。