有没有办法将变量传递到Meteor的模板中?

时间:2012-04-12 16:47:17

标签: javascript templates 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>

我希望每个插槽都有不同的编号。是否可以将变量传递给模板?像这样:

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

也许我正在以错误的方式思考这个问题,并且有更好的方法。

9 个答案:

答案 0 :(得分:89)

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

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

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

您只需在key="value"包含调用中传递{{> template}}个参数:

{{> slot number="1"}}

Spacebars Secrets: Exploring Meteor Templates了解详情。


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

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

答案 1 :(得分:14)

原来还有另一种方式。

我试图通过谷歌搜索各种搜索来找到如何做到这一点,并发现这个问题,但没有任何适合我的目的。除非您希望将嵌套模板放在父模板中的不同位置,否则TomUnite的答案是有效的。

经过多次搜索后,我在流星代码库中找到了“答案”。 (不是说它是确定的答案,但确实有效)

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

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

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

这可以通过循环或者可以在slots对象上使用underscore.js函数_.extend来制作更简洁的代码。此外,我们可以将多个数据字段传递到这些对象中。

答案 2 :(得分:9)

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

只能将一个参数传递给模板:

{{> singleItemInfo arg1}}

此参数必须是以下对象:

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

可以通过键来访问参数值,并且可以切换范围以使用

获取子项目

{{#with numberComposite}}

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

&lt; html file&gt;

<body>
    {{ itemsView }}
</body>

<template name="itemsView">
    {{> singleItemInfo arg1}}
</template>

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

&lt; javascript file&gt;

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

输出:

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

答案 3 :(得分:5)

更好的答案:

可用于在新Blaze布局下使模板上下文敏感的两个解决方案是:

1)直接将参数传递给模板

{{> 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     
}

答案 4 :(得分:3)

这是我为实现它所做的。我对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
  });
}

希望这对你有所帮助!

答案 5 :(得分:2)

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

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}}

希望它能解决问题。

答案 6 :(得分:1)

只传递一个参数时使用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需要这样做。如果您需要的不仅仅是争论,请尝试Dan的方式。

答案 7 :(得分:0)

此信息已过期,Blaze布局引擎在此处详细描述了传递参数: https://www.discovermeteor.com/blog/spacebars-secrets-exploring-meteor-new-templating-engine/

答案 8 :(得分:0)

这里有很多好消息。我的具体情况是我也想传递一些模板数据。

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

这是儿童组件......

Grade.html

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

title可以在父级中进行硬编码,但等级来自db。 这是我如何对父母进行编码......

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+';
    }
});

就是这样。子组件根本不需要伸出手来获取其值。

相关问题