{"id":19552784,"url":"https://github.com/ruby-grape/grape-rabl","last_synced_at":"2025-04-04T20:15:47.571Z","repository":{"id":2198953,"uuid":"3147317","full_name":"ruby-grape/grape-rabl","owner":"ruby-grape","description":"Use rabl with grape","archived":false,"fork":false,"pushed_at":"2019-11-19T17:00:52.000Z","size":99,"stargazers_count":138,"open_issues_count":4,"forks_count":29,"subscribers_count":10,"default_branch":"master","last_synced_at":"2024-03-25T21:19:04.634Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"Ruby","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ruby-grape.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2012-01-10T18:05:08.000Z","updated_at":"2023-06-24T00:46:41.000Z","dependencies_parsed_at":"2022-09-08T07:42:48.581Z","dependency_job_id":null,"html_url":"https://github.com/ruby-grape/grape-rabl","commit_stats":null,"previous_names":[],"tags_count":17,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ruby-grape%2Fgrape-rabl","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ruby-grape%2Fgrape-rabl/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ruby-grape%2Fgrape-rabl/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ruby-grape%2Fgrape-rabl/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ruby-grape","download_url":"https://codeload.github.com/ruby-grape/grape-rabl/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247242681,"owners_count":20907134,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-11T04:19:42.531Z","updated_at":"2025-04-04T20:15:47.553Z","avatar_url":"https://github.com/ruby-grape.png","language":"Ruby","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Grape::Rabl\r\n\r\nUse [Rabl](https://github.com/nesquena/rabl) templates in [Grape](https://github.com/ruby-grape/grape)!\r\n\r\n[![Gem Version](http://img.shields.io/gem/v/grape-rabl.svg)](http://badge.fury.io/rb/grape-rabl)\r\n[![Build Status](http://img.shields.io/travis/ruby-grape/grape-rabl.svg)](https://travis-ci.org/ruby-grape/grape-rabl)\r\n[![Code Climate](https://codeclimate.com/github/ruby-grape/grape-rabl.svg)](https://codeclimate.com/github/ruby-grape/grape-rabl)\r\n[![Coverage Status](https://img.shields.io/coveralls/ruby-grape/grape-rabl.svg)](https://coveralls.io/r/ruby-grape/grape-rabl?branch=master)\r\n\r\n## Installation\r\n\r\nAdd the `grape` and `grape-rabl` gems to Gemfile.\r\n\r\n```ruby\r\ngem 'grape'\r\ngem 'grape-rabl'\r\n```\r\n\r\nAnd then execute:\r\n\r\n    $ bundle\r\n\r\n## Upgrading\r\n\r\nSee [UPGRADING](UPGRADING.md).\r\n\r\n## Usage\r\n\r\n### Setup view root directory\r\n\r\n```ruby\r\n# config.ru\r\nuse Rack::Config do |env|\r\n  env['api.tilt.root'] = '/path/to/view/root/directory'\r\nend\r\n```\r\n\r\n### Tell your API to use Grape::Formatter::Rabl\r\n\r\n```ruby\r\nclass API \u003c Grape::API\r\n  format :json\r\n  formatter :json, Grape::Formatter::Rabl\r\nend\r\n```\r\n\r\n### Use rabl templates conditionally\r\n\r\nAdd the template name to the API options.\r\n\r\n```ruby\r\nget \"/user/:id\", :rabl =\u003e \"user.rabl\" do\r\n  @user = User.find(params[:id])\r\nend\r\n```\r\n\r\nYou can use instance variables in the Rabl template.\r\n\r\n```ruby\r\nobject @user =\u003e :user\r\nattributes :name, :email\r\n\r\nchild @project =\u003e :project do\r\n  attributes :name\r\nend\r\n```\r\n\r\n### Use rabl layout\r\n\r\nGape-rabl first looks for a layout file in `#{env['api.tilt.root']}/layouts/application.rabl`.\r\n\r\nYou can override the default layout conventions:\r\n\r\n```ruby\r\n# config.ru\r\nuse Rack::Config do |env|\r\n  env['api.tilt.root'] = '/path/to/view/root/directory'\r\n  env['api.tilt.layout'] = 'layouts/another'\r\nend\r\n```\r\n\r\n### Enable template caching\r\n\r\nGrape-rabl allows for template caching after templates are loaded initially.\r\n\r\nYou can enable template caching:\r\n\r\n```ruby\r\n# config.ru\r\nGrape::Rabl.configure do |config|\r\n  config.cache_template_loading = true # default: false\r\nend\r\n```\r\n\r\n## You can omit .rabl\r\n\r\nThe following are identical.\r\n\r\n```ruby\r\nget \"/home\", :rabl =\u003e \"view\"\r\nget \"/home\", :rabl =\u003e \"view.rabl\"\r\n```\r\n\r\n### Example\r\n\r\n```ruby\r\n# config.ru\r\nuse Rack::Config do |env|\r\n  env['api.tilt.root'] = '/path/to/view/root/directory'\r\nend\r\n\r\nclass UserAPI \u003c Grape::API\r\n  format :json\r\n  formatter :json, Grape::Formatter::Rabl\r\n\r\n  get '/user/:id' do\r\n    @user = User.find(params[:id])\r\n\r\n    # use rabl with 'user.rabl' or 'admin.rabl' template\r\n    if @user.admin?\r\n      # pass locals with the #render method\r\n      render rabl: 'admin', locals: { details: 'this user is an admin' }\r\n    else\r\n      render rabl: 'user'\r\n    end\r\n  end\r\n\r\n  get '/admin/:id', :rabl =\u003e 'admin' do\r\n    @user = User.find(params[:id])\r\n\r\n    # use rabl with 'super_admin.rabl'\r\n    render rabl: 'super_admin' if @user.super_admin?\r\n    # when render method has not been used use template from endpoint definition\r\n  end\r\n\r\n  # use rabl with 'user_history.rabl' template\r\n  get '/user/:id/history', :rabl =\u003e 'user_history' do\r\n    @history = User.find(params[:id]).history\r\n  end\r\n\r\n  # do not use rabl, fallback to the default Grape response formatter\r\n  get '/users' do\r\n    User.all\r\n  end\r\nend\r\n```\r\n\r\n```ruby\r\n# user.rabl\r\nobject @user =\u003e :user\r\n\r\nattributes :name\r\n```\r\n\r\n## Usage with rails\r\n\r\nCreate grape application\r\n\r\n```ruby\r\n# app/api/user.rb\r\nclass MyAPI \u003c Grape::API\r\n  format :json\r\n  formatter :json, Grape::Formatter::Rabl\r\n  get '/user/:id', :rabl =\u003e \"user\" do\r\n    @user = User.find(params[:id])\r\n  end\r\nend\r\n```\r\n\r\n```ruby\r\n# app/views/api/user.rabl\r\nobject @user =\u003e :user\r\n```\r\n\r\nEdit your **config/application.rb** and add view path\r\n\r\n```ruby\r\n# application.rb\r\nclass Application \u003c Rails::Application\r\n  config.middleware.use(Rack::Config) do |env|\r\n    env['api.tilt.root'] = Rails.root.join \"app\", \"views\", \"api\"\r\n  end\r\nend\r\n```\r\n\r\nMount application to rails router\r\n\r\n```ruby\r\n# routes.rb\r\nGrapeExampleRails::Application.routes.draw do\r\n  mount MyAPI , :at =\u003e \"/api\"\r\nend\r\n```\r\n\r\n## Specs\r\n\r\nSee [\"Writing Tests\"](https://github.com/ruby-grape/grape#writing-tests) in [grape](https://github.com/ruby-grape/grape) README.\r\n\r\nEnjoy :)\r\n\r\n## Contributing\r\n\r\nSee [CONTRIBUTING](CONTRIBUTING.md).\r\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fruby-grape%2Fgrape-rabl","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fruby-grape%2Fgrape-rabl","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fruby-grape%2Fgrape-rabl/lists"}