有没有办法将变量传递到Meteor的模板中? [英] Is there a way to pass variables into templates in Meteor?

查看:131
本文介绍了有没有办法将变量传递到Meteor的模板中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在试验Meteor并遇到了一些我无法弄清楚的事情。为了好玩,我试图制作老虎机。我有以下HTML:

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.

推荐答案

所有以前的答案都是过度杀伤或过时的。以下是直接从HTML + Spacebars代码中将静态参数传递到模板中的方法,从Meteor 0.8.x开始:

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>

所有你要做的就是传递 key =value 中的参数{{>模板}} 包含电话:

All you have to do is pass key="value" parameters in the {{> template}} inclusion call:

{{> slot number="1"}}

了解更多信息 Spacebars Secrets:探索流星模板

如果要将调用者模板的数据传递给子/嵌套/调用模板,请按以下步骤操作:不传递任何内容。相反,从嵌套模板访问父数据上下文 ../

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆