2014-01-13 86 views
0

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

我在main.js一个模板的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> 

我定义如下:

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。

我有一个网站,header.html中部分

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

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

的东西,会去main.js,像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值成>footer

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


干杯

+0

更新的代码有一个错字 - ''Handlebars.regesterHelper()'是正确的,如下面的richsilv答案。 – user728291

+0

我是一个白痴,我重新读它,我不知道有多少次... –

回答

2

你有申报siteLogo一个Template.site-header.helpers功能?如果没有,它将无法工作 - 你不能使用另一个模板的帮手。如果您需要在各种地方使用siteLogo,最好使用Handlebars block helper,因为这些可以通过任何模板访问。

UPDATE

车把帮手只是看起来像这样:

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'}} 。很明显,上述格式在临时助手中也可以正常工作,但只能从该模板中访问。三元运算符仅用于检查myDict是否已在其允许模板尝试查找其中一个键之前进行初始化,这是页面加载时常见的Meteor问题。顺便说一句,如果你发现Session变量是处理被动字典式数据结构的麻烦方式,那么很容易推出自己的变量。 This是最好的介绍。

+0

我已经添加了Template.site-header.helpers。但它仍然没有渲染部分的值。所以我不确定自己明白。你会有一个Handlebars块方法的例子吗?欢呼 –

+0

请参阅上面的更新。 – richsilv

+0

快速问题 - 现在它的工作 - 我必须单独发起这样的每个变量,或者是否有可能生成一个JSON列表并获得键:值对? –