Meteor 动态选择要使用的正确模板
我正在浏览一个帖子列表,并根据内容类型(图像、文本、twitter帖子)选择正确的html handlebar模板。随着越来越多的模板类型的出现,这种情况变得相当糟糕:Meteor 动态选择要使用的正确模板,meteor,handlebars.js,Meteor,Handlebars.js,我正在浏览一个帖子列表,并根据内容类型(图像、文本、twitter帖子)选择正确的html handlebar模板。随着越来越多的模板类型的出现,这种情况变得相当糟糕: <template name="postItem"> {{#if isType "image"}} {{#if isSinglePost}} {{>postImageSingle}} {{else}} {{>postImage}} {{/if}}
<template name="postItem">
{{#if isType "image"}}
{{#if isSinglePost}}
{{>postImageSingle}}
{{else}}
{{>postImage}}
{{/if}}
{{/if}}
{{#if isType "rich"}}
{{#if isSinglePost}}
{{>postRichSingle}}
{{else}}
{{>postRich}}
{{/if}}
{{/if}}
{{#if isType "video"}}
{{#if isSinglePost}}
{{>postRichSingle}}
{{else}}
{{>postRich}}
{{/if}}
{{/if}}
{{#if isType "file"}}
{{#if isMimeType "audio/wav"}}
{{>postAudio}}
{{else}}
{{>postFile}}
{{/if}}
{{/if}}
{{#if isType "link"}}
{{#if isProviderName this "Twitter"}}
{{>postTwitter}}
{{else}}
{{#if isSinglePost }}
{{>postLinkSingle}}
{{else}}
{{>postLink}}
{{/if}}
{{/if}}
{{/if}}
{{#if isType "preview"}}
{{>postPreview}}
{{/if}}
{{#if isType "photo"}}
{{>postImage}}
{{/if}}
</template>
但这当然给了我:
Exception from Deps recompute: Error: No such template 'getTemplateName'
{{>template}
语法仅用于插入模板,而对于助手,您使用的是{{{helper}
,不带尖括号
。从助手调用中删除括号,并在助手中呈现所需的子模板:
Template.postItem.getTemplateName = function() {
return new Handlebars.safeString(Template.postImage());
};
在考虑了这个建议之后,我认为需要通过
this
:返回新的handlebar.safeString(Template.postImage(this))
来传递上下文。谢谢你的帮助,让我走上正轨。
Template.postItem.getTemplateName = function() {
return new Handlebars.safeString(Template.postImage());
};