【发布时间】: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 的值传递给>header 的>footer
如果我尝试将Session.set 放入Meteor.isServer 块中,则会出现语法错误Session is not defined
干杯
【问题讨论】:
-
更新后的代码有错别字 -
Handlebars.regesterHelper()是正确的,如下面的richsilv 答案所示。 -
我是个白痴,我重读了不知道多少遍......
标签: meteor