cowboyd / handlebars.rb

Ruby Bindings for Handlebars.js
http://www.handlebarsjs.com
161 stars 67 forks source link

how I can add helper ? #15

Closed rjurado01 closed 11 years ago

rjurado01 commented 11 years ago

Hello,

I want to do this:

Handlebars.registerHelper('method_label', function(method) {
  if( method == "a")
    return "label1"
  else
    return "label2"
});

<span class={{method_label method}}>Hi</span>

how can I add helper like this ?

rjurado01 commented 11 years ago

I found the response in tests:

handlebars = Handlebars::Context.new
handlebars.register_helper('method_label') do |this, context, block|
    if  block.call(context) 
        'label1'
    else
        'label2'
    end
end
<span class="{{#method_label context}}{{method}}{{/method_label}}">  // <span class="label1">

It create span with class = context.method

Please, update documentation with it.

cowboyd commented 11 years ago

please see README section on helpers https://github.com/cowboyd/handlebars.rb#block-helpers thanks!

rjurado01 commented 11 years ago

Please update README, now instead of 'call' it is necesary use 'fn'

...
block.fn(context) 
...
justinperkins commented 11 years ago

The documentation is out-of-date and does not explain how to write block helpers with "else" conditions, which is just what this issue is created for.

justinperkins commented 11 years ago

Here we go, with a common use-case

handlebars = Handlebars::Context.new
handlebars.register_helper(:ifComparison) do |context, arg1, arg2, block|
  if arg1 == arg2
    block.fn(context)
  else
    block.inverse(context)
  end
rjurado01 commented 11 years ago

Thank you.

cowboyd commented 11 years ago

@drinor @justinperkins thanks for keeping after this. Does 31bf410 help?

justinperkins commented 11 years ago

Lookin good, thanks for updating Charles.