如果在把手模板中阻塞呼叫助手 [英] Calling Helper Within If Block in Handlebars Template

查看:100
本文介绍了如果在把手模板中阻塞呼叫助手的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



 <$ 

我正在使用Handlebars.js模板引擎,并试图找出一种方法来做这样的事情c $ c> {{#if itemSelectedSomeItem}}
< div>选择了< / div>
{{/ if}

其中 itemSelected
$ p $ Handlebars.registerHelper(itemSelected,function(item){
var selected = false;
//确定是否选择项目的许多逻辑
返回选定;
});

尝试为模板使用此语法时出现错误,并且找不到任何示例之类的事情。我看到简单的#if块就像这样...

  {{#if myValueInContext} 
< div> ;这将显示myValueInContext是否生成真值。< / div>
{{/ if}}

但是,我无法弄清楚如何解决第一个例子。也许我正在接近这个错误。顺便说一下,我标记了这个胡须,因为我无法在问题中添加Handlebars标记。

解决方案

我认为这不会起作用。如果我理解handlebars文档是正确的,#if是一个已注册的block-helper,并且不会将另一个注册的helper作为参数。

根据您可能的文档像这样执行它

  
Handlebars.registerHelper('ifItemSelected',function(item,block){
var selected = false;
//确定是否选择项目的逻辑很多

if(selected){
return block(this);
}
});

之后您应该可以使用

  
{{#ifItemSelected SomeItem}}
这被选中
{{/ ifItemSelected}

但您必须确保 SomeItem 具有适当的格式。我没有看到在if语句中使用注册处理程序作为条件的方法。


I am working with Handlebars.js template engine and am trying to figure out a way to do something like this (contrived example):

{{#if itemSelected "SomeItem"}}
    <div>This was selected</div>
{{/if}

where itemSelected is a registered helper like this:

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 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.

解决方案

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.

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 SomeItem has the proper format. I don't see a way to use a registered handler as conditional in an if-statement.

这篇关于如果在把手模板中阻塞呼叫助手的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆