【问题标题】:passing values to meteor partials将值传递给流星部分
【发布时间】:2014-02-27 20:47:08
【问题描述】:

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

我有一个 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>

在 main.js 中我定义了以下内容:

Meteor.startup(function(){
  Session.set('siteLogo', 'the logo');
});
Template.site-header.helpers({
  siteLogo: function(){ return Session.get('siteLogo'); }
});
Template.layout.helpers({
  siteLogo: function(){ return Session.get('siteLogo'); }
});

这样我可以将 siteLogo 的值传递给 layout.html。

我有一个 site-header.html 部分

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

我似乎无法将 siteLogo 的值传递给部分。有没有办法做到这一点? 是否有必要创建一个 Session 变量来预填充一些值,或者我可以只创建一个 json 设置列表并全局访问该值吗?

main.js 中的内容,例如 jekyll 站点中的 yaml 配置文件:

siteSettings = [
   {
    siteLogo: "some brand name",
    otherValue: "something else"
   }
]


更新


我有点困惑,我一定是做错了什么。 我创建了一个快速的新流星应用程序来测试这一点。

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

还有 main.js

if (Meteor.isClient) {
  Template.hello.greeting = function () {
    return "Welcome to handlebar-helper.";
  };

  Template.hello.events({
    'click input' : function () {
      // template data, if any, is available in 'this'
      if (typeof console !== 'undefined')
        console.log("You pressed the button");
    }
  });
  Meteor.startup(function(){
    Session.set('headline', 'My fancy headline');
  });
  Handlebars.registerHelper('headline', function(){
    return Session.get('headline');
  });
}

if (Meteor.isServer) {
  // server code here
}

我仍然不能将headline 的值传递给&gt;header&gt;footer

如果我尝试将Session.set 放入Meteor.isServer 块中,则会出现语法错误Session is not defined


干杯

【问题讨论】:

  • 更新后的代码有错别字 - Handlebars.regesterHelper() 是正确的,如下面的richsilv 答案所示。
  • 我是个白痴,我重读了不知道多少遍......

标签: meteor


【解决方案1】:

你有为siteLogo 声明的Template.site-header.helpers 函数吗?如果不是,它将不起作用 - 您不能使用另一个模板中的帮助程序。如果您需要在各种地方使用siteLogo,最好使用Handlebars block helper,因为这些可以通过任何模板访问。

更新

Handlebars 助手看起来像这样:

Handlebars.registerHelper('siteLogo', function() {
   return Session.get('siteLogo');
});

但是,如果您已经在 site-header 模板中找到了 siteLogo 帮助程序,则表明存在其他问题,例如模板或帮助程序名称中的拼写错误。控制台有错误吗?

更新 2

如果你想使用字典式的结构来存储响应式数据,你可以这样做:

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 是否已初始化,这是页面加载时常见的 Meteor 问题。

顺便说一句,如果您发现 Session 变量是一种处理类似字典的反应式数据结构的繁琐方法,那么您可以轻松自己动手。 This 是最好的介绍。

【讨论】:

  • 我添加了一个 Template.site-header.helpers。但它仍然没有呈现部分中的值。所以我不确定我是否理解。您有 Handlebars 块方法的示例吗?欢呼
  • 快速问题——现在它正在工作——我是否必须像这样单独启动每个变量,或者是否可以生成一个 json 列表并获取 key: values 对?
  • 针对您的上一个问题再次更新。
  • 谢谢!这真的很有帮助。我会看看视频链接。我尝试添加一个嵌套值(电话:{human:'01 01 01 01 01',机器:'00441010101})然后通过 {{ myDict 'phone.human'} 访问它当然不起作用。有可能吗?
  • 如果你更新辅助函数当然是可能的。只需将字符串按. 拆分,然后使用结果数组进行递归查找,如果在任何阶段遇到空/未定义则返回空,否则返回最后一个查找值。
猜你喜欢
  • 2015-07-24
  • 2023-03-16
  • 1970-01-01
  • 2023-03-24
  • 1970-01-01
  • 2011-08-10
  • 2014-08-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多