Templates 如何获得;根“;Blaze模板或模板辅助对象中的数据
让我们假设我必须创建包含相同嵌套模板的页面: 第1页:Templates 如何获得;根“;Blaze模板或模板辅助对象中的数据,templates,meteor,meteor-blaze,Templates,Meteor,Meteor Blaze,让我们假设我必须创建包含相同嵌套模板的页面: 第1页: >rootTeplate >fooTemplate >barTemplate 第2页: >rootTeplate >barTemplate 如何编写一个模板助手,允许我在barTemplate中访问rootTemplate的数据?我知道我可以使用../访问父上下文,但在这种情况下,barTemplate不知道必须返回多少级别。您需要确定要返回多少级别。在barTemplate中的帮助程序中,
>rootTeplate
>fooTemplate
>barTemplate
第2页:
>rootTeplate
>barTemplate
如何编写一个模板助手,允许我在barTemplate中访问rootTemplate的数据?我知道我可以使用../访问父上下文,但在这种情况下,barTemplate不知道必须返回多少级别。您需要确定要返回多少级别。在
barTemplate
中的帮助程序中,您可以使用Template.parentData
i、 e
您需要确定要访问多少级别,并且可以将此数字插入
Template.parentData
如果您不确定需要返回多少级别,则可能不值得使用此范例。考虑从路由器中使用<代码>数据< /代码>上下文,或者使用<代码>会话散列代替。 在这种情况下,您可以从路由器:Currand().DATA()/<
路由器的数据上下文就是我要寻找的。如何在不知道根模板的情况下在模板帮助函数中访问它?@MarcS82使用
数据参数设置它(),并使用此在帮助函数中访问它@MarcS82以下是一个示例:
Template.barTemplate.helpers({
something: function() {
var parentDatas = Template.parentData()
//If there are two levels
var parentDatas = Template.parentData(2)
}
});
Template.barTemplate.helpers({
var data = Router.current() && Router.current().data();
if(data)
// use it