2012-06-18 02:42:24 -04:00
|
|
|
[![Build Status](https://secure.travis-ci.org/sinatra/sinatra-contrib.png)](http://travis-ci.org/sinatra/sinatra-contrib)
|
|
|
|
|
2011-03-29 04:05:12 -04:00
|
|
|
Collection of common Sinatra extensions, semi-officially supported.
|
|
|
|
|
|
|
|
# Goals
|
|
|
|
|
|
|
|
* For every future Sinatra release, have at least one fully compatible release
|
|
|
|
* High code quality, high test coverage
|
|
|
|
* Include plugins people usually ask for a lot
|
|
|
|
|
|
|
|
# TODO
|
|
|
|
|
|
|
|
* Write documentation, integrate into Sinatra website
|
|
|
|
* Finish imports and rewrites
|
|
|
|
* Wrap up first release
|
|
|
|
* Find contributors (both code and docs)
|
|
|
|
|
|
|
|
# Included extensions
|
|
|
|
|
|
|
|
## Common Extensions
|
|
|
|
|
|
|
|
These are common extension which will not add significant overhead or change any
|
|
|
|
behavior of already existing APIs. They do not add any dependencies not already
|
|
|
|
installed with this gem.
|
|
|
|
|
|
|
|
Currently included:
|
|
|
|
|
2011-05-09 02:36:48 -04:00
|
|
|
* `sinatra/capture`: Let's you capture the content of blocks in templates.
|
|
|
|
|
2011-03-29 04:05:12 -04:00
|
|
|
* `sinatra/config_file`: Allows loading configuration from yaml files.
|
|
|
|
|
|
|
|
* `sinatra/content_for`: Adds Rails-style `content_for` helpers to Haml, Erb,
|
|
|
|
Erubis and Slim.
|
|
|
|
|
2011-09-04 21:13:40 -04:00
|
|
|
* `sinatra/cookies`: A `cookies` helper for reading and writing cookies.
|
|
|
|
|
2011-05-09 02:36:48 -04:00
|
|
|
* `sinatra/engine_tracking`: Adds methods like `haml?` that allow helper
|
|
|
|
methods to check whether they are called from within a template.
|
|
|
|
|
|
|
|
* `sinatra/json`: Adds a `#json` helper method to return JSON documents.
|
2011-03-29 04:05:12 -04:00
|
|
|
|
|
|
|
* `sinatra/link_header`: Helpers for generating `link` HTML tags and
|
|
|
|
corresponding `Link` HTTP headers. Adds `link`, `stylesheet` and `prefetch`
|
|
|
|
helper methods.
|
|
|
|
|
2011-09-04 21:13:27 -04:00
|
|
|
* `sinatra/multi_route`: Adds ability to define one route block for multiple
|
|
|
|
routes and multiple or custom HTTP verbs.
|
|
|
|
|
2011-05-09 02:36:48 -04:00
|
|
|
* `sinatra/namespace`: Adds namespace support to Sinatra.
|
|
|
|
|
2011-03-29 04:05:12 -04:00
|
|
|
* `sinatra/respond_with`: Choose action and/or template depending automatically
|
|
|
|
depending on the incoming request. Adds helpers `respond_to` and
|
|
|
|
`respond_with`.
|
|
|
|
|
|
|
|
|
|
|
|
## Custom Extensions
|
|
|
|
|
|
|
|
These extensions may add additional dependencies and enhance the behavior of the
|
2011-03-30 14:49:11 -04:00
|
|
|
existing APIs.
|
|
|
|
|
|
|
|
Currently included:
|
|
|
|
|
|
|
|
* `sinatra/decompile`: Recreates path patterns from Sinatra's internal data
|
2011-05-09 02:36:48 -04:00
|
|
|
structures (used by other extensions).
|
2011-03-29 04:05:12 -04:00
|
|
|
|
2011-05-09 02:36:48 -04:00
|
|
|
* `sinatra/reloader`: Automatically reloads Ruby files on code changes.
|
2011-03-29 04:05:12 -04:00
|
|
|
|
|
|
|
## Other Tools
|
|
|
|
|
|
|
|
* `sinatra/extension`: Mixin for writing your own Sinatra extensions.
|
|
|
|
|
|
|
|
* `sinatra/test_helpers`: Helper methods to ease testing your Sinatra
|
|
|
|
application. Partly extracted from Sinatra. Testing framework agnostic
|
|
|
|
|
2012-09-21 17:59:35 -04:00
|
|
|
# Installation
|
|
|
|
Add `gem 'sinatra-contrib'` to *Gemfile*, then execute `bundle install`.
|
|
|
|
|
|
|
|
If you don't use Bundler, install the gem manually by executing `gem install sinatra-contrib` in your command line.
|
|
|
|
|
|
|
|
|
2011-03-29 04:05:12 -04:00
|
|
|
# Usage
|
|
|
|
|
|
|
|
## Classic Style
|
|
|
|
|
|
|
|
A single extension (example: sinatra-content-for):
|
|
|
|
|
2011-05-24 13:50:01 -04:00
|
|
|
``` ruby
|
|
|
|
require 'sinatra'
|
|
|
|
require 'sinatra/content_for'
|
|
|
|
```
|
2011-03-29 04:05:12 -04:00
|
|
|
|
|
|
|
Common extensions:
|
|
|
|
|
2011-05-24 13:50:01 -04:00
|
|
|
``` ruby
|
|
|
|
require 'sinatra'
|
|
|
|
require 'sinatra/contrib'
|
|
|
|
```
|
2011-03-29 04:05:12 -04:00
|
|
|
|
|
|
|
All extensions:
|
|
|
|
|
2011-05-24 13:50:01 -04:00
|
|
|
``` ruby
|
|
|
|
require 'sinatra'
|
|
|
|
require 'sinatra/contrib/all'
|
|
|
|
```
|
2011-03-29 04:05:12 -04:00
|
|
|
|
|
|
|
## Modular Style
|
|
|
|
|
|
|
|
A single extension (example: sinatra-content-for):
|
|
|
|
|
2011-05-24 13:50:01 -04:00
|
|
|
``` ruby
|
|
|
|
require 'sinatra/base'
|
|
|
|
require 'sinatra/content_for'
|
2011-09-02 17:01:03 -04:00
|
|
|
require 'sinatra/namespace'
|
2011-05-24 13:50:01 -04:00
|
|
|
|
|
|
|
class MyApp < Sinatra::Base
|
|
|
|
# Note: Some modules are extensions, some helpers, see the specific
|
|
|
|
# documentation or the source
|
|
|
|
helpers Sinatra::ContentFor
|
2011-09-02 17:01:03 -04:00
|
|
|
register Sinatra::Namespace
|
2011-05-24 13:50:01 -04:00
|
|
|
end
|
|
|
|
```
|
2011-03-29 04:05:12 -04:00
|
|
|
|
|
|
|
Common extensions:
|
|
|
|
|
2011-05-24 13:50:01 -04:00
|
|
|
``` ruby
|
|
|
|
require 'sinatra/base'
|
|
|
|
require 'sinatra/contrib'
|
|
|
|
|
|
|
|
class MyApp < Sinatra::Base
|
|
|
|
register Sinatra::Contrib
|
|
|
|
end
|
|
|
|
```
|
2011-03-29 04:05:12 -04:00
|
|
|
|
|
|
|
All extensions:
|
|
|
|
|
2011-05-24 13:50:01 -04:00
|
|
|
``` ruby
|
|
|
|
require 'sinatra/base'
|
2012-02-01 14:02:02 -05:00
|
|
|
require 'sinatra/contrib/all'
|
2011-05-24 13:50:01 -04:00
|
|
|
|
|
|
|
class MyApp < Sinatra::Base
|
|
|
|
register Sinatra::Contrib
|
|
|
|
end
|
|
|
|
```
|