Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Rails template handler #2

Open
jamesarosen opened this issue Nov 22, 2010 · 6 comments
Open

Add Rails template handler #2

jamesarosen opened this issue Nov 22, 2010 · 6 comments

Comments

@jamesarosen
Copy link

See the following for a good example:

I just forked the project to give this a go. I'll update with findings as I go.

@GermanDZ
Copy link

On RoR 3.1:

initializerz/template_handlers.rb

require 'action_view/template/handlers/handlebars'

ActionView::Template.register_template_handler(:hbs, ActionView::Template::Handlers::Handlebars.new)

# lib/action_view/template/handlers/handlebars.rb

require 'handlebars'

class ActionView::Template::Handlers::Handlebars
  def call(template)
    <<-RUBY_CODE
      template = Handlebars.compile('#{template.source}');
      vars = {}
      partial_renderer = @view_renderer.send(:_partial_renderer)
      vars.merge!(@_assigns)
      vars.merge!(partial_renderer.instance_variable_get('@locals'))
      vars.merge!(partial_renderer.instance_variable_get('@options')[:context] || {})
      template.call(vars.as_json).html_safe
    RUBY_CODE
  end
end

Just place your .hbs template in the same view directories as other views (just like a regular erb) and enjoy

@mahemoff
Copy link

mahemoff commented Jul 5, 2012

I had some errors with the above and made a couple of modifications:

require 'handlebars'

class ActionView::Template::Handlers::Handlebars
  def call(template)
    <<-RUBY_CODE
      template = Handlebars::Context.new.compile('#{template.source}');
      vars = {}
      partial_renderer = @view_renderer.send(:_partial_renderer)
      vars.merge!(@_assigns)
      vars.merge!(local_assigns)
      # vars.merge!(partial_renderer.instance_variable_get('@options')[:context] || {})
      template.call(vars.as_json).html_safe
    RUBY_CODE
  end 
end

This is using a HandleBars::Context instance rather than Handlebars global; and local_assigns rather than @Locals. I'm not sure how to fix the @options.

@cowboyd
Copy link
Owner

cowboyd commented Jul 6, 2012

I have a handlebars-rails template handler that supports partials and more. https://github.com/cowboyd/handlebars-rails

@jamesarosen any chance you can give me the keys to the rubygem?

@jamesarosen
Copy link
Author

@wycats is the only owner on Rubygems; he'll have to do it.

@wycats
Copy link

wycats commented Jul 9, 2012

@cowboyd you are now an owner!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants