Skip to content

Instantly share code, notes, and snippets.

@kainosnoema
Last active August 29, 2015 13:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kainosnoema/10012517 to your computer and use it in GitHub Desktop.
Save kainosnoema/10012517 to your computer and use it in GitHub Desktop.
Jsonite in Rails using Ruby ActionView Template Handler
# config/initializers/rb_template_template.rb
class RbTemplateHandler
cattr_accessor :default_format
self.default_format = Mime::JSON
def self.call(template)
%{rendered = begin;#{template.source};end
unless rendered.is_a?(String)
rendered = rendered.to_#{template.type.to_sym}
end
rendered}
end
end
ActionView::Template.register_template_handler :rb, RbTemplateHandler
UserPresenter.present @user, root: 'user'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment