Skip to content

Instantly share code, notes, and snippets.

@thebyrd
Created February 21, 2013 19:43
Show Gist options
  • Save thebyrd/5007531 to your computer and use it in GitHub Desktop.
Save thebyrd/5007531 to your computer and use it in GitHub Desktop.
Allows you to render `.hbs` templates that are in the views directory
require 'handlebars'
class HandlebarsHandler < ActionView::Template
def self.call(template)
# return "Hello World!"
<<-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
ActionView::Template.register_template_handler(:hbs, HandlebarsHandler)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment