有没有办法将变量传递到 Meteor 的模板中? [英] Is there a way to pass variables into templates in Meteor?
问题描述
我一直在尝试 Meteor,但遇到了一些我无法弄清楚的问题.为了好玩,我试图制作一台老虎机.我有以下 HTML:
{{>投币口}}{{>投币口}}{{>投币口}}<模板名称=插槽"><div class="slot"><div class="number"><span>{{ number }}</span></div><div class="divider"></div>
模板>
我想为每个插槽设置不同的编号.是否可以将变量传递给模板?像这样:
{{>插槽 1}}{{>插槽 2}}{{>插槽 3}}<模板名称=插槽"><div class="slot"><div class="number"><span>{{number i}}</span></div><div class="divider"></div>
模板>
也许我的想法是错误的,但有更好的方法.
之前的所有答案都有些矫枉过正或过时了.从 Meteor 0.8.x 开始,您可以通过以下方式将静态参数直接从 HTML+空格键代码传递到模板中:
{{>插槽编号="1"}}{{>插槽编号="2"}}...<模板名称=插槽"><div class="number"><span>{{number}}</span></div>模板>
您所要做的就是在 {{> 中传递
包含调用:key="value"
参数.模板}}
{{>插槽编号="1"}}
在 Spacebars Secrets: Exploring Meteor Templates 了解更多信息.
<小时>如果你想将调用者模板的数据传递给子/嵌套/被调用模板,这里是如何做的:什么都不传递.相反,从嵌套模板访问父数据上下文,../
:
{{>插槽编号="1"}}{{>插槽编号="2"}}...<模板名称=插槽"><div>机器名称:{{../name}}</div><div class="number"><span>{{number}}</span></div>模板>
I've been experimenting with Meteor and ran into something I couldn't figure out. For fun, I was trying to make a slot machine. I had the following 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>
I want to have a different number for each slot. Is it possible to pass variables into template? Something like this:
<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>
Maybe I'm thinking about this the wrong way and there's a better way.
All of the previous answers are overkill or outdated. Here's how you can pass static parameters into templates, directly from HTML+Spacebars code, as of 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>
All you have to do is pass key="value"
parameters in the {{> template}}
inclusion call:
{{> slot number="1"}}
Learn more at Spacebars Secrets: Exploring Meteor Templates.
If you want to pass the caller template's data to the child/nested/called template, here's how to do it: pass nothing. Instead, from the nested template, access the parent data context, ../
:
<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>
这篇关于有没有办法将变量传递到 Meteor 的模板中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!