railsware / sht_rails

Shared handlebars templates for Rails 3
http://blog.railsware.com/2012/05/21/shared-handlebars-templates-for-rails-3/
MIT License
76 stars 21 forks source link

Ruby helpers #11

Closed justinperkins closed 11 years ago

justinperkins commented 11 years ago

I just sort of jammed this in, but the idea is to expose ruby helpers to handlebars templates when being rendered on the server-side. Seemed like a nice feature to have available.

I also added a bit to the README for the note about custom configs and asset precompiling.

le0pard commented 11 years ago

Thanks for pull request. Does this helpers can used in both version of templates: client-side and server-side?

justinperkins commented 11 years ago

No it's just for the server-side, at least as-is in the example. However, you can code up your handlebars helpers (JS file) to conditionally use it. Given my example in the README, this would work:

Handlebars.registerHelper('formatDateTime', function(date, formatString){
  if (typeof rubyFormatDateTime == 'function'){
    // this is a custom ruby helper mixed into v8
    return rubyFormatDateTime(date, formatString)
  } else {
    // we are in pure javascript land, plan accordingly
  }
});
le0pard commented 11 years ago

Yep, but in this case you can use right now this JS helper in your server-side template:

https://github.com/railsware/sht_rails/blob/master/lib/sht_rails/handlebars.rb#L9-L17

In your solution its impossible :)

justinperkins commented 11 years ago

You use the JS helpers on the server-side in conjunction with the ruby helpers I've added. I'm using it in production right now (my branch) and it works great.

le0pard commented 11 years ago

Ok, thanks. I will review you pull request little later (my day is off) and will notify you, if found some problem :)

le0pard commented 11 years ago

I looked all this stuff (sorry for delay). I think we need add mix this ruby functions into v8 as handlebars helpers, so users can use this without additional work on client-side. What do you think, @justinperkins ?

justinperkins commented 11 years ago

I don't know. I like the idea of using pure-ruby from Handlebars as a replacement for JS lib functionality. I don't think everything has to exist in both environments but that's just me.

In my previous example about using Ruby's strftime, how would you implement that?

le0pard commented 11 years ago

In my previous example about using Ruby's strftime, how would you implement that?

I will use JS helpers with some js libs, because its shared in ruby templates also.

https://github.com/railsware/sht_rails/blob/master/lib/sht_rails/handlebars.rb#L10-L13

What is why I thinking what it solve your problem, but not good solution for library - main point of this library, what this templates should render on client side and server side in the same html.