Javascript Meteor.js如何多次使用同名模板
如何在不同的文件中多次使用相同的模板名称?我对每个页面都有相同的模板命名模式,问题是当另一个页面已经使用了Javascript Meteor.js如何多次使用同名模板,javascript,templates,meteor,meteor-blaze,Javascript,Templates,Meteor,Meteor Blaze,如何在不同的文件中多次使用相同的模板名称?我对每个页面都有相同的模板命名模式,问题是当另一个页面已经使用了时,它就不能再使用了 示例URL: /home /home/first /home/second /home/third homePage.html /template: homePage template: default template: first template: second template: third userPage.html /template:
时,它就不能再使用了
示例URL:
/home
/home/first
/home/second
/home/third
homePage.html
/template: homePage
template: default
template: first
template: second
template: third
userPage.html
/template: userPage
template: default
template: first
template: second
template: third
铁路由器代码:
// mainpage
Router.route('/home', function () {
this.render('homePage');
this.render('default', {to: 'content'});
});
// subpages
Router.route('/home/:content', function () {
this.render('homePage');
var templateName = this.params.content;
if(Template[templateName]){
this.render(templateName, {to: 'content'});
};
});
[更新]顺便说一下,这就是解决这个问题的方法:
<template name="CoolPageSubPageBSubPageB1LoremIpsum">
您需要将其包装在每页的一个父模板中。如果要重用的模板名为“defaultContent”,则可以使用{{>defaultContent}
将模板添加到HTML中的父模板中。不能使用相同的名称定义多个模板
Meteor在全局对象中定义模板,Template
(例如,Template.homePage
)。一个对象不能多次使用同一字段,因此多次定义一个模板将导致错误(可能是无声的错误)
另外,路由器怎么知道你说的是哪个defaultContent
你怎么能
您可以简单地定义多个模板(
Template.homefault
,Template.userDefault
),而不是隐藏模板,这样您就可以轻松地调试和引用它们。这正是我要解决的问题,方法是在模板名称前面加上页面名称,使其成为homeFirst、homeSecond、homeDefault,等等我为Iron router写了一条简单的规则,上面说:如果你打开url,比如:address.com/home/firstsubpage,那么只需查找模板firstsubpage。这太简单了,不可能是真的;)然而,这是事实,而且很容易。我使用它在不同的上下文中重用共享模板。我只是像这样给我的调用添加参数,{{>mySharedTemplate mode=“user”}
,但是模板不能包含在其他模板中,比如parent…nested
,我想实现的是为用户定义多个
,为homeSo定义另一个
,我想@Kyll解决方案是一条出路,即使我不明白你为什么要这样做