如何使用流星模板中的语句 [英] How work with statements in meteor template
问题描述
我喜欢和流星一起工作,但我遇到了一个问题,我无法找到解决方案。
I like to work with meteor, but I have a problem I can't find a solution for.
在模板文件中,我有以下代码:
In the template file I have this code:
<template name="klussenboard">
<h2>Klussen</h2>
<div class="klussenboard">
{{#each klus}}
{{#if status=1}}
<li>
<a class="startlink" href="#"><img src="/images/starten.png"></a>
</li>
{{/if}}
{{/each}}
</div>
</template>
这是js客户代码
Template.klussenboard.klus = function () {
return Klussen.find({"status": { $gt: 0 }}, {
sort: {datum: -1}
});
};
但这不起作用。如何在模板文件中进行声明?
But this doesn't work. How can I do a statement in a template file?
期待答案。
推荐答案
Spacebars(meteor的模板库),就像 Handlebars (基于它)一样执行任意表达式,例如angular.js的模板。
Spacebars (meteor's templating library), much like Handlebars (that it is based on) doesn't execute arbitrary expressions like, say, angular.js's templates.
如果你改变你试图写入帮助方法的语句,就像这样(随意选择一个更好的名字!):
If you change the statement you're trying to write into a helper method, like so (feel free to pick a better name!):
<template name="klussenboard">
<h2>Klussen</h2>
<div class="klussenboard">
{{#each klus}}
{{#if isEnabled}}
<li>
<a class="startlink" href="#"><img src="/images/starten.png"></a>
</li>
{{/if}}
{{/each}}
</div>
</template>
然后你可以在任何客户端.js文件中定义isEnabled帮助器 - 比如说, client / klussenboard.js
如下:
You could then define the isEnabled helper in any client side .js file - say, client/klussenboard.js
like so:
Template.item.isEnabled = function() {
return this.status == 1;
}
所以,这个
,在辅助函数中是
So, this
, in helper functions is
这假设您处于 status
是一个变量的上下文中(基于在你的问题上,你是)
This assumes that you are in a context where status
is a variable (which, based on your question, you are)
每当 status
变量发生变化时,这将会被动地更新。
This would then reactively update whenever the status
variable changes.
这篇关于如何使用流星模板中的语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!