{"id":19189245,"url":"https://github.com/plexinc/grape-builder","last_synced_at":"2025-07-24T10:09:46.035Z","repository":{"id":56875119,"uuid":"51464028","full_name":"plexinc/grape-builder","owner":"plexinc","description":"Use builder for rendering XML in grape","archived":false,"fork":false,"pushed_at":"2016-02-11T23:17:27.000Z","size":10,"stargazers_count":3,"open_issues_count":0,"forks_count":0,"subscribers_count":14,"default_branch":"master","last_synced_at":"2025-06-24T07:52:24.888Z","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/plexinc.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2016-02-10T18:58:11.000Z","updated_at":"2025-03-14T10:12:54.000Z","dependencies_parsed_at":"2022-08-20T22:00:36.179Z","dependency_job_id":null,"html_url":"https://github.com/plexinc/grape-builder","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/plexinc/grape-builder","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/plexinc%2Fgrape-builder","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/plexinc%2Fgrape-builder/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/plexinc%2Fgrape-builder/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/plexinc%2Fgrape-builder/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/plexinc","download_url":"https://codeload.github.com/plexinc/grape-builder/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/plexinc%2Fgrape-builder/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":266824106,"owners_count":23990158,"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","status":"online","status_checked_at":"2025-07-24T02:00:09.469Z","response_time":99,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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-09T11:28:46.109Z","updated_at":"2025-07-24T10:09:46.019Z","avatar_url":"https://github.com/plexinc.png","language":"Ruby","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Grape::Builder\n\nUse [Builder](https://github.com/jimweirich/builder) templates in [Grape](https://github.com/intridea/grape)!\n\nThis gem is completely based on [grape-jbuilder](https://github.com/milkcocoa/grape-jbuilder) which in turn is completely based on [grape-rabl](https://github.com/LTe/grape-rabl).\n\n## Installation\n\nAdd this line to your application's Gemfile:\n\n    gem 'grape-builder' \n\nAnd then execute:\n\n    $ bundle\n\n\u003c!--Or install it yourself as:--\u003e\n\n\u003c!--    $ gem install grape-builder--\u003e\n\n## Usage\n\n### Require grape-builder\n\n```ruby\n# config.ru\nrequire 'grape/jbuilder'\n```\n\n### Setup view root directory\n```ruby\n# config.ru\nrequire 'grape/builder'\n\nuse Rack::Config do |env|\n  env['api.tilt.root'] = '/path/to/view/root/directory'\nend\n```\n\n### Tell your API to use Grape::Formatter:: Builder\n\n```ruby\nclass API \u003c Grape::API\n  format :xml\n  formatter :xml, Grape::Formatter::Builder\nend\n```\n\n### Use Builder templates conditionally\n\nAdd the template name to the API options.\n\n```ruby\nget '/user/:id', builder: 'user.builder' do\n  @user = User.find(params[:id])\nend\n```\n\nYou can use instance variables in the builder template.\n\n```ruby\nxml.user do\n  xml.name(@user.name)\n  xml.email(@user.email)j\n  xml.project do\n    xml.name(@project.name)\n  end\nend\n```\n\n### Use builder templates dynamically\n\n```ruby\nget ':id' do\n  user = User.find_by(id: params[:id])\n  if article\n    env['api.tilt.template'] = 'users/show'\n    env['api.tilt.locals']   = {user: user}\n  else\n    status 404\n    {'status' =\u003e 'Not Found', 'errors' =\u003e \"User id '#{params[:id]}' is not found.\"}\n  end\nend\n```\n\n## You can omit .builder\n\nThe following are identical.\n\n```ruby\nget '/home', builder: 'view'\nget '/home', builder: 'view.jbuilder'\n```\n\n### Example\n\n```ruby\n# config.ru\nrequire 'grape/builder'\n\nuse Rack::Config do |env|\n  env['api.tilt.root'] = '/path/to/view/root/directory'\nend\n\nclass UserAPI \u003c Grape::API\n  format :xml\n  formatter :xml, Grape::Formatter::Builder\n\n  # use Builder with 'user.builder' template\n  get '/user/:id', builder: 'user' do\n    @user = User.find(params[:id])\n  end\n\n  # do not use builder, fallback to the defalt Grape XML formatter\n  get '/users' do\n    User.all\n  end\nend\n```\n\n```ruby\n# user.builder\nxml.user do\n  xmlname(@user.name)\nend\n```\n\n## Usage with Rails\n\nCreate a grape application.\n\n```ruby\n# app/api/user.rb\nclass MyAPI \u003c Grape::API\n  format :xml\n  formatter :xml, Grape::Formatter::Builder\n  get '/user/:id', builder: 'user' do\n    @user = User.find(params[:id])\n  end\nend\n```\n\n```ruby\n# app/views/api/user.builder\nxml.user do\n  xml.name(@user.name)\nend\n```\n\nEdit your **config/application.rb** and add view path\n\n```ruby\n# application.rb\nclass Application \u003c Rails::Application\n  config.middleware.use(Rack::Config) do |env|\n    env['api.tilt.root'] = Rails.root.join 'app', 'views', 'api'\n  end\nend\n```\n\nMount application to Rails router\n\n```ruby\n# routes.rb\nGrapeExampleRails::Application.routes.draw do\n  mount MyAPI , at: '/api'\nend\n```\n\n## Partials\n\n\nUse the `partial.render` method to render a partial template\n\n```ruby\n# app/views/api/user/index.builder\nxml.users do\n @users.each do |u|\n    xml \u003c\u003c partial.render('user/show', user: u)\n end\nend\n```\n\n```ruby\n# app/views/api/user/show.builder\nxml.user do\n  xml.name(@user.name)\nend\n```\n\n## Rspec\n\nSee \"Writing Tests\" in https://github.com/intridea/grape.\n\nEnjoy :)\n\n\n## Special Thanks\n\nSpecial thanks to [@milkcocoa](https://github.com/milkcocoa) because this gem is completely based on [grape-jbuilder](https://github.com/milkcocoa/grape-jbuilder).\n\nAlso thanks to [@LTe](https://github.com/LTe) since `grape-jbuilder` is based on [grape-rabl](https://github.com/LTe/grape-rabl).\n\n## Contributing\n\n1. Fork it\n2. Create your feature branch (`git checkout -b my-new-feature`)\n3. Commit your changes (`git commit -am 'Add some feature'`)\n4. Push to the branch (`git push origin my-new-feature`)\n5. Create new Pull Request\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fplexinc%2Fgrape-builder","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fplexinc%2Fgrape-builder","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fplexinc%2Fgrape-builder/lists"}