cowboyd / handlebars.rb

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

Support for Rails' String#html_safe #10

Open dlee opened 12 years ago

dlee commented 12 years ago

It would be nice if there was support for Rails' String#html_safe to be treated the same way as Handlebars::SafeString.

blairanderson commented 9 years ago

i also wanted this because i'm trying to share data between the frontend and these handlebars templates.

In my rails app i created initializers/handlebars.rb(a dirty monkeypatch, i know)

module Handlebars
  class SafeString
    def self.new(string)
      if context = Context.current
        context.handlebars['SafeString'].new(string)
      else
        string.html_safe
      end
    end
  end
end

then with the data:


  def procs_to_html_safe(data)
    data.each_with_object({}){|(k, v), result|  (v.class.to_s == 'Proc') ? result[k] = v.call : result[k] = v}
  end