English 中文(简体)
Calling Helper Within If Block in Handlebars Template
原标题:

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.

问题回答

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.

With last version (1.0.rc.1) of Handlebars, you have to write sth like:

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)

http://handlebarsjs.com/block_helpers.html#conditionals

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}




相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签