Meteor 将值传递给流星部分

Meteor 将值传递给流星部分,meteor,Meteor,我正在学习meteor以快速构建网站原型。 我试图理解如何生成一组值来填充站点模板和部分 我有一个layout.html模板 <template name="layout"> <div class="container"> <header role="banner"> {{>site-header}} </header> <h1>This is {{siteLogo}}</h1>

我正在学习meteor以快速构建网站原型。 我试图理解如何生成一组值来填充站点模板和部分

我有一个layout.html模板

<template name="layout">
  <div class="container">
    <header role="banner">
      {{>site-header}}
    </header>
    <h1>This is {{siteLogo}}</h1>
    <main role="main">
      {{ yield }}
    </main>
    <footer role="contentinfo">
      {{> site-footer }}
    </footer>
  </div>
</template>
有了这个,我可以将siteLogo的值传递给layout.html

我有一个site-header.html部分

<template name="site-header">
  <h1>{{siteLogo}}</h1>
</template>

更新
我有点困惑,我一定是做错了什么。 我已经创建了一个新的meteor应用程序来测试这一点

我有main.html

<head>
  <title>handlebar-helper</title>
</head>

<body>
  {{> header}}
  {{> hello}}
  {{> footer}}
</body>

<template name="hello">
  <h1>Hello World!</h1>
  {{greeting}}
  <input type="button" value="Click" />
</template>

<template name="header">
  <header>
    <h1>{{ headline }}</h1>
    <p>tagline</p>
  </header>
</template>

<template name="footer">
  <footer role="contentinfo">
    <h1>{{ headline }}</h1>
    <small>copyright</small>
  </footer>
</template>
我仍然无法将
headline
的值传递到
>footer的
>header

如果我试图将
Session.set
放入
Meteor.isServer
块,我会得到一个语法错误,
Session未定义



干杯

您是否有为
站点徽标
声明的
模板.站点标题.助手
功能?如果没有,它将无法工作-您不能使用来自其他模板的帮助器。如果您需要在各种地方使用
sitebogo
,最好使用,因为任何模板都可以访问这些

更新

车把辅助对象的外观如下所示:

Handlebars.registerHelper('siteLogo', function() {
   return Session.get('siteLogo');
});
Session.set('myDict', {foo: 1, bar: 2});

Handlebars.registerHelper('myDict', function(key) {
   return Session.get('myDict') ? Session.get('myDict')[key] : null;
});
但是,如果在
站点标题
模板中已经有
站点徽标
帮助程序,则表明存在其他错误,例如模板或帮助程序名称中的键入错误。控制台中是否有错误

更新2

如果要使用字典式结构存储反应数据,可以执行以下操作:

Handlebars.registerHelper('siteLogo', function() {
   return Session.get('siteLogo');
});
Session.set('myDict', {foo: 1, bar: 2});

Handlebars.registerHelper('myDict', function(key) {
   return Session.get('myDict') ? Session.get('myDict')[key] : null;
});
然后在模板中使用它:
{{myDict'foo'}}
。显然,上面的格式在tempate助手中也可以很好地工作,但它只能从该模板中访问。三值运算符只是检查
myDict
是否已初始化,然后让模板尝试查找其中一个键,这是页面加载中常见的流星问题


顺便说一句,如果您发现会话变量是处理反应式字典式数据结构的一种很麻烦的方法,那么您很容易使用自己的会话变量。这是最好的介绍。

我添加了一个Template.site-header.helpers。但它仍然没有呈现部分中的值。所以我不确定我是否理解。你能举一个把手块法的例子吗?Cheesquick的问题现在已经开始工作了-我是否需要单独启动每个变量,或者是否可以生成json列表并获得key:values对?再次更新以响应您的上一个问题。谢谢!这真的很有帮助。我会看看视频链接。我尝试添加一个嵌套值(phone:{human:'01',machine:'00441010101}),然后通过{{myDict'phone.human}访问它它当然不起作用。有可能吗?如果更新helper函数,当然有可能。只需按
拆分字符串,然后使用生成的数组进行递归查找,如果在任何阶段遇到null/未定义,则返回null,否则返回上一个查找值。更新的代码有一个打字错误
手柄s、 regesterHelper()
在下面的richsilv答案中是正确的。我是个白痴,我重读了一遍,不知道读了多少遍。。。
Session.set('myDict', {foo: 1, bar: 2});

Handlebars.registerHelper('myDict', function(key) {
   return Session.get('myDict') ? Session.get('myDict')[key] : null;
});