Skip to content

Instantly share code, notes, and snippets.

@alyssais
Created December 28, 2013 17:15
Show Gist options
  • Save alyssais/8161687 to your computer and use it in GitHub Desktop.
Save alyssais/8161687 to your computer and use it in GitHub Desktop.
A Handlebars template handler for Rails.
$handlebars = Handlebars::Context.new
module HandlebarsTemplateHandler
def self.call(template)
if template.locals.include? "raw"
"#{template.source.inspect}.html_safe"
else
%{
locals = #{template.locals.inspect}
vars = Hash[locals.zip(locals.map(&method(:eval)))]
$handlebars.compile(#{template.source.inspect}).call(vars).html_safe
}
end
end
end
ActionView::Template.register_template_handler :handlebars, HandlebarsTemplateHandler
ActionView::Template.register_template_handler :hbs, HandlebarsTemplateHandler
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment