Javascript 在 Handlebars 模板中的 If 块中调用 Helper
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6319231/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Calling Helper Within If Block in Handlebars Template
提问by Kevin
I am working with Handlebars.js template engine and am trying to figure out a way to do something like this (contrived example):
我正在使用 Handlebars.js 模板引擎,并试图找出一种方法来做这样的事情(人为的例子):
{{#if itemSelected "SomeItem"}}
<div>This was selected</div>
{{/if}
where itemSelected
is a registered helper like this:
itemSelected
像这样的注册助手在哪里:
Handlebars.registerHelper("itemSelected", function(item) {
var selected = false;
// Lots of logic that determines if item is selected
return selected;
});
I get errors when trying to use this syntax for the template, and I cannot find any example showing this kind of thing. I do see simple #if blocks like this...
尝试将这种语法用于模板时出现错误,我找不到任何显示此类事情的示例。我确实看到像这样简单的#if 块...
{{#if myValueInContext}}
<div>This will show if myValueInContext results in a truthy value.</div>
{{/if}}
But, I can't figure out how to tackle the first example. Maybe I am approaching this wrong.
但是,我不知道如何处理第一个例子。也许我正在接近这个错误。
By the way, I tagged this Mustache as I could not add a Handlebars tag to the question.
顺便说一句,我标记了这个 Mustache,因为我无法在问题中添加 Handlebars 标记。
回答by gnowoel
You should add parentheses around the embedded helper invocation:
您应该在嵌入式帮助程序调用周围添加括号:
{{#if (itemSelected "SomeItem")}}
<div>This was selected</div>
{{/if}
I did experiments and verified that it just works.
我做了实验并验证它确实有效。
Not sure if it's mentioned in the Handlebars documentation. I learned the trick from the examples of handlebars-layouts.
不确定它是否在 Handlebars 文档中提到。我从handlebars-layouts的例子中学到了诀窍。
回答by jgraglia
With last version (1.0.rc.1) of Handlebars, you have to write sth like:
使用 Handlebars 的最新版本 (1.0.rc.1),你必须这样写:
Handlebars.registerHelper('ifItemSelected', function(item, options) {
var selected = false;
// lots of logic that determines if item is selected
if (selected) {
return options.fn(this);
}
});
ie. block(this)is replaced by options.fn(this)
IE。block(this)被options.fn(this)替换
回答by Steffen
I don't think this is going to work. If I understand the handlebars documentation correct, the #if is a registered block-helper itself and does not take another registered helper as an argument.
我不认为这会奏效。如果我理解把手文档是正确的,#if 本身就是一个已注册的块助手,并且不会将另一个已注册的助手作为参数。
According to the documentation you might implement it like that
根据文档,您可能会像那样实现它
Handlebars.registerHelper('ifItemSelected', function(item, block) {
var selected = false;
// lots of logic that determines if item is selected
if(selected) {
return block(this);
}
});
Afterwards you should be able to call it with
之后你应该可以调用它
{{#ifItemSelected SomeItem}}
This was selected
{{/ifItemSelected}
but you have to make sure SomeItemhas the proper format. I don't see a way to use a registered handler as conditional in an if-statement.
但您必须确保SomeItem具有正确的格式。我没有看到在 if 语句中使用注册处理程序作为条件的方法。
回答by Fiona - myaccessible.website
If you want to have an else option too, you will need this code:
如果您也想拥有其他选项,则需要以下代码:
Handlebars.registerHelper('ifItemSelected', function(item, options) {
var selected = false;
// lots of logic that determines if item is selected
if (selected) {
return options.fn(this);
}
else {
return options.inverse(this);
}
});
Used with:
用于:
{{#ifItemSelected SomeItem}}
This was selected
{{else}}
This was not selected
{{/ifItemSelected}