README.md

Jbuilder

Jbuilder gives you a simple DSL for declaring JSON structures that beats massaging giant hash structures. This is particularly helpful when the generation process is fraught with conditionals and loops. Here's a simple example:

You can either use Jbuilder stand-alone or directly as an ActionView template language. When required in Rails, you can create views ala show.json.jbuilder (the json is already yielded):

# Any helpers available to views are available to the builder
json.content format_content(@message.content)
json.(@message, :created_at, :updated_at)
json.author do
json.name @message.creator.name.familiar
json.email_address @message.creator.email_address_with_name
json.url url_for(@message.creator, format::json)
endif current_user.admin?
json.visitors calculate_visitors(@message)
end# You can use partials as well. The following line will render the file# RAILS_ROOT/app/views/api/comments/_comments, and set a local variable# 'comments' with all this message's comments, which you can use inside# the partial.
json.partial! "api/comments/comments", comments:@message.comments

Keys can be auto formatted using key_format!, this can be used to convert keynames from the standard ruby_format to CamelCase: