Project

grape-erb

0.0
No commit activity in last 3 years
No release in over 3 years
Use erb in grape
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Runtime

>= 0
>= 0
>= 0
>= 0
 Project Readme

Grape::Erb

Use Erb templates in Grape!

Installation

Add the grape and grape-erb gems to Gemfile.

gem 'grape'
gem 'grape-erb'

And then execute:

$ bundle

Usage

Setup view root directory

# config.ru
use Rack::Config do |env|
  env['api.tilt.root'] = '/path/to/view/root/directory'
end

Tell your API to use Grape::Formatter::Erb

class API < Grape::API
  content_type :json, 'application/json'
  content_type :html, 'text/html'
  formatter :html, Grape::Formatter::Erb
  format :html
end

Use erb templates conditionally

Add the template name to the API options.

get "/user/:id", erb: "users/show" do
  @user = User.find(params[:id])
end

You can use instance variables in the Erb template.

<% unless @user.nil? %>
  <h2><%= @user.name %></h2>
  <p><%= @user.bio %></p>
<% end %>

Use erb layout

Gape-erb first looks for a layout file in #{env['api.tilt.root']}/layouts/application.html.erb.

You can override the default layout conventions:

# config.ru
use Rack::Config do |env|
  env['api.tilt.root'] = '/path/to/view/root/directory'
  env['api.tilt.layout'] = 'layouts/another'
end

You can omit .erb

The following are identical.

get "/home", erb: "view"
get "/home", erb: "view.html.erb"

Usage with rails

Create grape application

# app/api/user.rb
class MyAPI < Grape::API
  content_type :json, 'application/json'
  content_type :js,   'text/javascript'
  content_type :html, 'text/html'
  
  formatter :html, Grape::Formatter::Erb
  formatter :js,   Grape::Formatter::Erb

  format :json # Default format

  get '/user/:id' do
    env['api.format'] = :html
    content_type 'text/html'

    @user = User.find(params[:id])

    render erb: 'users/show'
  end
end
# app/views/api/user.html.erb
<% unless @user.nil? %>
  <h2><%= @user.name %></h2>
  <p><%= @user.bio %></p>
<% end %>

Edit your config/application.rb and add view path

# application.rb
class Application < Rails::Application
  config.middleware.use(Rack::Config) do |env|
    env['api.tilt.root'] = Rails.root.join "app", "views", "api"
  end
end

Specs

See "Writing Tests" in https://github.com/intridea/grape README.

Enjoy :)

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Added some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request