有没有办法将variables传递到meteor的模板?

我一直在试验meteor,遇到了一些我无法想象的东西。 为了好玩,我试图制作一台老虎机。 我有以下的HTML:

<div class="slot-wrapper"> {{> slot}} {{> slot}} {{> slot}} </div> <template name="slot"> <div class="slot"> <div class="number"><span>{{ number }}</span></div> <div class="divider"></div> </div> </template> 

我想为每个插槽有不同的编号。 是否有可能将variables传递到模板? 像这样的东西:

 <div class="slot-wrapper"> {{> slot 1}} {{> slot 2}} {{> slot 3}} </div> <template name="slot"> <div class="slot"> <div class="number"><span>{{ number i}}</span></div> <div class="divider"></div> </div> </template> 

也许我正在考虑这个错误的方法,还有更好的方法。

所有以前的答案都是过度的或过时的。 以下是如何将静态parameter passing到模板,直接从HTML +空格键代码,从Meteor 0.8.x:

 <div class="slot-wrapper"> {{> slot number="1"}} {{> slot number="2"}} ... </div> <template name="slot"> <div class="number"><span>{{number}}</span></div> </template> 

你所要做的就是在{{> template}}包含调用中传递key="value"参数:

 {{> slot number="1"}} 

了解更多Spacebars秘密:探索meteor模板 。


如果您想将调用者模板的数据传递给子/嵌套/被调用的模板,请执行以下操作:不传递任何东西。 相反,从嵌套模板访问父数据上下文../

 <div class="slot-wrapper"> {{> slot number="1"}} {{> slot number="2"}} ... </div> <template name="slot"> <div>Machine name: {{../name}}</div> <div class="number"><span>{{number}}</span></div> </template> 

原来还有另一种方法。

我试图找出如何做到这一点,search各种search,发现这个问题,但没有什么符合我的目的。 除非要将嵌套模板放在父模板的不同位置,否则TomUnite的答案是有效的。

所以经过多次search,我在meteor的代码库中find了答案。 (不是说这是确定的答案,但它确实有效)

 <template name="slots"> {{> slot one}} <div>..something else</div> {{> slot three}} {{> slot two}} </template> <template name="slot"> <div class="slot"> <div class="number"><span>{{number}}</span></div> <div class="divider"></div> </div> </template> 

如您所见,我们可以按任意顺序指定模板实例。 第二个参数实际上是一个应该定义的variables,所以:

 Template.slots.one = { number: 1 } Template.slots.two = { number: 2 } Template.slots.three = { number: 3 } 

这可以通过一个循环做成更简洁的代码,也可以使用underscore.js函数_.extend在slots对象上。 而且,我们可以将多个数据字段传递给这些对象。

我想留下这个评论,因为这只是Joc的答案的澄清,但不能,所以这里加上我与之合作的例子。

只有一个参数可以传递给模板:

 {{> singleItemInfo arg1}} 

这个论点必须是一个对象,如:

 { number: 1, number2: 2, numberComposite: { subnum1: 10, subnum2: 20 } }; 

参数值可以通过它们的键来访问,并且范围可以被切换以获得子项目

{{#with numberComposite}}

以下是该示例的完整代码:

<html文件>

 <body> {{ itemsView }} </body> <template name="itemsView"> {{> singleItemInfo arg1}} </template> <template name="singleItemInfo"> arg1 = {{ number }} arg2 = {{ number2 }} {{#with numberComposite}} subArg1 = {{ subnum1 }} subArg2 = {{ subnum2 }} {{/with}} </template> 

<javascript文件>

 Template.itemsView.arg1 = { number: 1, number2: 2, numberComposite: { subnum1: 10, subnum2: 20 } }; 

OUTPUT:

 arg1 = 1 arg2 = 2 subArg1 = 10 subArg2 = 20 

更好的答案:

在新的Blaze布局下,可用于创build模板上下文敏感的两个解决scheme是:

1)直接将parameter passing给模板

 {{> contextSensitiveTemplate context_1='x' context_2='y' }} 

2)在理解上下文的模板中使用助手。 像这样打电话给帮手:

 {{ contextHelperName ../.. .. this }} 

 Template.contextSensitiveTemplate.contextHelperName = function(parent_template, current_template, current_value_inside_each_loop) { return context_dependent_value_or_html } 

这就是我所做的。 我对meteor相当陌生,所以可能有更好的方法:

Slot.html:

 <head> <title>Slot</title> </head> <body> <div class="slot-wrapper"> {{> slots}} </div> </body> <template name="slots"> {{#each slots}} {{> slot}} {{/each}} </template> <template name="slot"> <div class="slot"> <div class="number"><span>{{number}}</span></div> <div class="divider"></div> </div> </template> 

Slot.js:

 if (Meteor.is_client) { Template.slots.slots = function () { var returnArray = new Array(); returnArray[0] = { 'number': 10 }; returnArray[1] = { 'number': 87 }; returnArray[2] = { 'number': 41 }; return returnArray; }; } if (Meteor.is_server) { Meteor.startup(function () { // code to run on server at startup }); } 

希望这对你有一些帮助!

我通常使用这两个手把助手:

 Handlebars.registerHelper('partial', function(templateName, options) { return new Handlebars.SafeString(Template[templateName](options.hash)); }); Handlebars.registerHelper('partialWithContext', function(templateName, context, options) { var extendedContext = _.extend(context, options.hash); return new Handlebars.SafeString(Template[templateName](context)); }); 

你可以像这样使用它(假设你有一个名为menuItem的模板):

 {{partial 'menuItem' command='Open'}} 

或者在一个迭代中(假设你有一个叫做userProfile的模板):

 {{#each regularUsers}} {{partialWithContext 'userProfile' . isAdmin=false}} {{/each}} {{#each admins}} {{partialWithContext 'userProfile' . isAdmin=true}} {{/each}} 

用Spacebars,你可以达到一个相似的行为。 在partial.js

 Template.partialWithContext.chooseTemplate = function (name) { return Template[name]; }; 

partial.html

 <template name="partialWithContext"> {{#with chooseTemplate name}} {{#with ../data}} {{> ..}} {{/with}} {{/with}} </template> 

像这样使用它:

 {{#each commands}} {{> partialWithContext name="commandListItem" data=this isAdmin=false}} {{/each}} {{#each adminCommands}} {{> partialWithContext name="adminCommandListItem" data=this isAdmin=true}} {{/each}} 

希望它能做到这一点。

当你传递一个参数时使用this

 <div class="slot-wrapper"> {{> slot 1}} {{> slot 2}} </div> <template name="slot"> <div class="slot"> <div class="number"><span>{{this}}</span></div> <div class="divider"></div> </div> </template> 

没有JavaScript需要做到这一点。 如果你需要更多的争论,请尝试丹的方式。

这些信息已经过时,传递参数在Blaze布局引擎中详细描述: https : //www.discovermeteor.com/blog/spacebars-secrets-exploring-meteor-new-templating-engine/

很多很好的信息在这里。 我的具体情况是我也想通过一些模板数据。

我想让孩子Blaze组件可重用,所以所有的数据都必须传入。举个例子,假设这个组件显示一个等级(即A,B,C等)。 在一个页面上,我想要使用这个组件两次:你的成绩和你的同学的平均成绩。

这是孩子的组成部分…

Grade.html

 <template name="Grade"> <h3>{{title}}</h3> <div>{{grade}}</h3> </template> 

标题可以在父级硬编码,但成绩来自数据库。 这是我如何编码父母…

GradePage.html

 <template name="GradePage"> {{> Grade grade=gradeYours title="Your Grade" }} {{> Grade grade=gradeClass title="Class Grade" }} </template> 

GradePage.js(在现实生活中,它是被动的,但在这里简化)

 Template.GradePage.helpers({ gradeYours: function () { return 'A-'; }, gradeClass: function () { return 'B+'; } }); 

而已。 孩子的组成部分根本不需要伸手去拿它的价值。