sinatra 1.3.6 → 1.4.0.a
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of sinatra might be problematic. Click here for more details.
- data/CHANGES +96 -22
- data/Gemfile +11 -3
- data/README.de.md +2590 -0
- data/README.es.rdoc +66 -38
- data/README.fr.md +2630 -0
- data/README.hu.rdoc +3 -2
- data/README.jp.rdoc +16 -3
- data/README.ko.rdoc +11 -5
- data/README.md +2699 -0
- data/README.pt-br.rdoc +152 -21
- data/README.pt-pt.rdoc +3 -2
- data/README.ru.md +2724 -0
- data/README.zh.rdoc +3 -3
- data/Rakefile +3 -4
- data/examples/chat.rb +3 -3
- data/lib/sinatra/base.rb +433 -247
- data/lib/sinatra/main.rb +4 -2
- data/lib/sinatra/showexceptions.rb +6 -1
- data/lib/sinatra/version.rb +1 -1
- data/test/base_test.rb +21 -9
- data/test/builder_test.rb +15 -19
- data/test/coffee_test.rb +4 -6
- data/test/compile_test.rb +154 -0
- data/test/contest.rb +4 -6
- data/test/creole_test.rb +5 -5
- data/test/delegator_test.rb +1 -3
- data/test/erb_test.rb +32 -20
- data/test/extensions_test.rb +1 -3
- data/test/filter_test.rb +65 -56
- data/test/haml_test.rb +34 -26
- data/test/helpers_test.rb +331 -221
- data/test/integration_helper.rb +8 -0
- data/test/integration_test.rb +3 -1
- data/test/less_test.rb +10 -8
- data/test/liquid_test.rb +22 -4
- data/test/mapped_error_test.rb +122 -96
- data/test/markaby_test.rb +5 -5
- data/test/markdown_test.rb +5 -5
- data/test/middleware_test.rb +3 -3
- data/test/nokogiri_test.rb +4 -6
- data/test/rabl_test.rb +89 -0
- data/test/radius_test.rb +4 -4
- data/test/rdoc_test.rb +7 -7
- data/test/readme_test.rb +14 -30
- data/test/request_test.rb +15 -0
- data/test/response_test.rb +3 -4
- data/test/result_test.rb +11 -33
- data/test/route_added_hook_test.rb +10 -10
- data/test/routing_test.rb +123 -1
- data/test/sass_test.rb +26 -26
- data/test/scss_test.rb +16 -16
- data/test/server_test.rb +2 -2
- data/test/settings_test.rb +48 -4
- data/test/sinatra_test.rb +2 -7
- data/test/slim_test.rb +37 -23
- data/test/static_test.rb +56 -15
- data/test/streaming_test.rb +11 -2
- data/test/templates_test.rb +117 -45
- data/test/textile_test.rb +9 -9
- data/test/views/hello.rabl +2 -0
- data/test/views/hello.wlang +1 -0
- data/test/views/hello.yajl +1 -0
- data/test/views/layout2.rabl +3 -0
- data/test/views/layout2.wlang +2 -0
- data/test/wlang_test.rb +87 -0
- data/test/yajl_test.rb +86 -0
- metadata +27 -17
- data/README.de.rdoc +0 -2097
- data/README.fr.rdoc +0 -2036
- data/README.rdoc +0 -2017
- data/README.ru.rdoc +0 -1785
data/README.hu.rdoc
CHANGED
@@ -2,7 +2,8 @@
|
|
2
2
|
<i>Fontos megjegyzés: Ez a dokumentum csak egy fordítása az angol nyelvű
|
3
3
|
változat, és lehet, hogy nem naprakész.</i>
|
4
4
|
|
5
|
-
A Sinatra egy DSL
|
5
|
+
A Sinatra egy {DSL}[http://en.wikipedia.org/wiki/Domain-specific_language]
|
6
|
+
webalkalmazások Ruby nyelven történő fejlesztéséhez, minimális
|
6
7
|
energiabefektetéssel:
|
7
8
|
|
8
9
|
# myapp.rb
|
@@ -14,7 +15,7 @@ energiabefektetéssel:
|
|
14
15
|
Telepítsd a gem-et és indítsd el az alkalmazást a következőképpen:
|
15
16
|
|
16
17
|
sudo gem install sinatra
|
17
|
-
ruby
|
18
|
+
ruby myapp.rb
|
18
19
|
|
19
20
|
Az alkalmazás elérhető lesz itt: http://localhost:4567
|
20
21
|
|
data/README.jp.rdoc
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
= Sinatra
|
2
2
|
<i>注) 本文書は英語から翻訳したものであり、その内容が最新でない場合もあります。最新の情報はオリジナルの英語版を参照して下さい。</i>
|
3
3
|
|
4
|
-
SinatraはRubyで下記のような最小労力で手早くウェブアプリケーションを作成するためのDSLです。
|
4
|
+
SinatraはRubyで下記のような最小労力で手早くウェブアプリケーションを作成するための{DSL}[http://ja.wikipedia.org/wiki/ドメイン固有言語]です。
|
5
5
|
|
6
6
|
# myapp.rb
|
7
7
|
require 'sinatra'
|
@@ -12,7 +12,7 @@ SinatraはRubyで下記のような最小労力で手早くウェブアプリケ
|
|
12
12
|
gemをインストールして動かしてみる。
|
13
13
|
|
14
14
|
gem install sinatra
|
15
|
-
ruby
|
15
|
+
ruby myapp.rb
|
16
16
|
|
17
17
|
http://localhost:4567 を見る。
|
18
18
|
|
@@ -426,6 +426,19 @@ Markabyテンプレートを使うにはmarkabyライブラリが必要です:
|
|
426
426
|
|
427
427
|
<tt>./views/index.mab</tt>を表示します。
|
428
428
|
|
429
|
+
=== RABL テンプレート
|
430
|
+
|
431
|
+
RABLテンプレートを使うにはrablライブラリが必要です:
|
432
|
+
|
433
|
+
# rablを読み込みます
|
434
|
+
require 'rabl'
|
435
|
+
|
436
|
+
get '/' do
|
437
|
+
rabl :index
|
438
|
+
end
|
439
|
+
|
440
|
+
<tt>./views/index.rabl</tt>を表示します。
|
441
|
+
|
429
442
|
=== Slim テンプレート
|
430
443
|
|
431
444
|
Slimテンプレートを使うにはslimライブラリが必要です:
|
@@ -771,7 +784,7 @@ SinatraはRack[http://rack.rubyforge.org/]というRubyのWEBフレームワー
|
|
771
784
|
向けに一番興味深い機能はミドルウェア(サーバとアプリケーション間に介在し、モニタリング、HTTPリクエストとレスポンス
|
772
785
|
の手動操作ができるなど、一般的な機能のいろいろなことを提供するもの)をサポートすることです。
|
773
786
|
|
774
|
-
Sinatraではトップレベルの+
|
787
|
+
Sinatraではトップレベルの+use+ メソッドを使ってRackにパイプラインを構築します。
|
775
788
|
|
776
789
|
require 'sinatra'
|
777
790
|
require 'my_custom_middleware'
|
data/README.ko.rdoc
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
|
3
3
|
<i>주의: 이 문서는 영문판의 번역본이며 최신판 문서와 다를 수 있음.</i>
|
4
4
|
|
5
|
-
Sinatra는 최소한의 노력으로 루비 기반 웹 애플리케이션을 신속하게 만들 수 있게 해 주는 DSL이다:
|
5
|
+
Sinatra는 최소한의 노력으로 루비 기반 웹 애플리케이션을 신속하게 만들 수 있게 해 주는 {DSL}[http://en.wikipedia.org/wiki/Domain-specific_language]이다:
|
6
6
|
|
7
7
|
# myapp.rb
|
8
8
|
require 'sinatra'
|
@@ -14,7 +14,7 @@ Sinatra는 최소한의 노력으로 루비 기반 웹 애플리케이션을 신
|
|
14
14
|
다음과 같이 젬을 설치하고 실행한다:
|
15
15
|
|
16
16
|
gem install sinatra
|
17
|
-
ruby
|
17
|
+
ruby myapp.rb
|
18
18
|
|
19
19
|
확인: http://localhost:4567
|
20
20
|
|
@@ -378,7 +378,7 @@ Liquid 템플릿에서는 루비 메서드(+yield+ 제외)를 호출할 수 없
|
|
378
378
|
=== Markdown 템플릿
|
379
379
|
|
380
380
|
의존:: {rdiscount}[https://github.com/rtomayko/rdiscount],
|
381
|
-
{redcarpet}[https://github.com/
|
381
|
+
{redcarpet}[https://github.com/vmg/redcarpet],
|
382
382
|
{bluecloth}[http://deveiate.org/projects/BlueCloth],
|
383
383
|
{kramdown}[http://kramdown.rubyforge.org/] *또는*
|
384
384
|
{maruku}[http://maruku.rubyforge.org/]
|
@@ -454,6 +454,12 @@ Radius 템플릿에서는 루비 메서드를 호출할 수 없기 때문에,
|
|
454
454
|
|
455
455
|
인라인 템플릿으로 블록을 받을 수도 있음(예제 참조).
|
456
456
|
|
457
|
+
=== RABL 템플릿
|
458
|
+
|
459
|
+
의존:: {rabl}[https://github.com/nesquena/rabl]
|
460
|
+
파일 확장자:: <tt>.rabl</tt>
|
461
|
+
예제:: <tt>rabl :index</tt>
|
462
|
+
|
457
463
|
=== Slim 템플릿
|
458
464
|
|
459
465
|
의존:: {slim}[http://slim-lang.com/]
|
@@ -994,7 +1000,7 @@ Cache-Control 헤더를 다음과 같이 간단하게 설정할 수 있다:
|
|
994
1000
|
|
995
1001
|
이들 헬퍼는 어떠한 캐싱도 하지 않으며, 대신 필요한 정보를 캐시에 제공한다.
|
996
1002
|
여러분이 만약 손쉬운 리버스 프록시(reverse-proxy) 캐싱 솔루션을 찾고 있다면,
|
997
|
-
{rack-cache}[
|
1003
|
+
{rack-cache}[https://github.com/rtomayko/rack-cache]를 써보라:
|
998
1004
|
|
999
1005
|
require "rack/cache"
|
1000
1006
|
require "sinatra"
|
@@ -1923,4 +1929,4 @@ SemVer 및 SemVerTag 둘 다 해당된.
|
|
1923
1929
|
* {Sinatra Recipes}[http://recipes.sinatrarb.com/] 커뮤니티가 만드는 레시피
|
1924
1930
|
* http://rubydoc.info에 있는 {최종 릴리스}[http://rubydoc.info/gems/sinatra]
|
1925
1931
|
또는 {current HEAD}[http://rubydoc.info/github/sinatra/sinatra]에 대한 API 문서
|
1926
|
-
* {CI server}[http://ci.
|
1932
|
+
* {CI server}[http://travis-ci.org/sinatra/sinatra]
|
data/README.md
ADDED
@@ -0,0 +1,2699 @@
|
|
1
|
+
# Sinatra
|
2
|
+
|
3
|
+
Sinatra is a [DSL](http://en.wikipedia.org/wiki/Domain-specific_language) for
|
4
|
+
quickly creating web applications in Ruby with minimal effort:
|
5
|
+
|
6
|
+
```ruby
|
7
|
+
# myapp.rb
|
8
|
+
require 'sinatra'
|
9
|
+
|
10
|
+
get '/' do
|
11
|
+
'Hello world!'
|
12
|
+
end
|
13
|
+
```
|
14
|
+
|
15
|
+
Install the gem and run with:
|
16
|
+
|
17
|
+
```ruby
|
18
|
+
gem install sinatra
|
19
|
+
ruby myapp.rb
|
20
|
+
```
|
21
|
+
|
22
|
+
View at: http://localhost:4567
|
23
|
+
|
24
|
+
It is recommended to also run `gem install thin`, which Sinatra will
|
25
|
+
pick up if available.
|
26
|
+
|
27
|
+
## Routes
|
28
|
+
|
29
|
+
In Sinatra, a route is an HTTP method paired with a URL-matching pattern.
|
30
|
+
Each route is associated with a block:
|
31
|
+
|
32
|
+
```ruby
|
33
|
+
get '/' do
|
34
|
+
.. show something ..
|
35
|
+
end
|
36
|
+
|
37
|
+
post '/' do
|
38
|
+
.. create something ..
|
39
|
+
end
|
40
|
+
|
41
|
+
put '/' do
|
42
|
+
.. replace something ..
|
43
|
+
end
|
44
|
+
|
45
|
+
patch '/' do
|
46
|
+
.. modify something ..
|
47
|
+
end
|
48
|
+
|
49
|
+
delete '/' do
|
50
|
+
.. annihilate something ..
|
51
|
+
end
|
52
|
+
|
53
|
+
options '/' do
|
54
|
+
.. appease something ..
|
55
|
+
end
|
56
|
+
```
|
57
|
+
|
58
|
+
Routes are matched in the order they are defined. The first route that
|
59
|
+
matches the request is invoked.
|
60
|
+
|
61
|
+
Route patterns may include named parameters, accessible via the
|
62
|
+
`params` hash:
|
63
|
+
|
64
|
+
```ruby
|
65
|
+
get '/hello/:name' do
|
66
|
+
# matches "GET /hello/foo" and "GET /hello/bar"
|
67
|
+
# params[:name] is 'foo' or 'bar'
|
68
|
+
"Hello #{params[:name]}!"
|
69
|
+
end
|
70
|
+
```
|
71
|
+
|
72
|
+
You can also access named parameters via block parameters:
|
73
|
+
|
74
|
+
```ruby
|
75
|
+
get '/hello/:name' do |n|
|
76
|
+
"Hello #{n}!"
|
77
|
+
end
|
78
|
+
```
|
79
|
+
|
80
|
+
Route patterns may also include splat (or wildcard) parameters, accessible
|
81
|
+
via the `params[:splat]` array:
|
82
|
+
|
83
|
+
```ruby
|
84
|
+
get '/say/*/to/*' do
|
85
|
+
# matches /say/hello/to/world
|
86
|
+
params[:splat] # => ["hello", "world"]
|
87
|
+
end
|
88
|
+
|
89
|
+
get '/download/*.*' do
|
90
|
+
# matches /download/path/to/file.xml
|
91
|
+
params[:splat] # => ["path/to/file", "xml"]
|
92
|
+
end
|
93
|
+
```
|
94
|
+
|
95
|
+
Or with block parameters:
|
96
|
+
|
97
|
+
```ruby
|
98
|
+
get '/download/*.*' do |path, ext|
|
99
|
+
[path, ext] # => ["path/to/file", "xml"]
|
100
|
+
end
|
101
|
+
```
|
102
|
+
|
103
|
+
Route matching with Regular Expressions:
|
104
|
+
|
105
|
+
```ruby
|
106
|
+
get %r{/hello/([\w]+)} do
|
107
|
+
"Hello, #{params[:captures].first}!"
|
108
|
+
end
|
109
|
+
```
|
110
|
+
|
111
|
+
Or with a block parameter:
|
112
|
+
|
113
|
+
```ruby
|
114
|
+
get %r{/hello/([\w]+)} do |c|
|
115
|
+
"Hello, #{c}!"
|
116
|
+
end
|
117
|
+
```
|
118
|
+
|
119
|
+
Route patterns may have optional parameters:
|
120
|
+
|
121
|
+
```ruby
|
122
|
+
get '/posts.?:format?' do
|
123
|
+
# matches "GET /posts" and any extension "GET /posts.json", "GET /posts.xml" etc.
|
124
|
+
end
|
125
|
+
```
|
126
|
+
|
127
|
+
By the way, unless you disable the path traversal attack protection (see below),
|
128
|
+
the request path might be modified before matching against your routes.
|
129
|
+
|
130
|
+
## Conditions
|
131
|
+
|
132
|
+
Routes may include a variety of matching conditions, such as the user agent:
|
133
|
+
|
134
|
+
```ruby
|
135
|
+
get '/foo', :agent => /Songbird (\d\.\d)[\d\/]*?/ do
|
136
|
+
"You're using Songbird version #{params[:agent][0]}"
|
137
|
+
end
|
138
|
+
|
139
|
+
get '/foo' do
|
140
|
+
# Matches non-songbird browsers
|
141
|
+
end
|
142
|
+
```
|
143
|
+
|
144
|
+
Other available conditions are `host_name` and `provides`:
|
145
|
+
|
146
|
+
```ruby
|
147
|
+
get '/', :host_name => /^admin\./ do
|
148
|
+
"Admin Area, Access denied!"
|
149
|
+
end
|
150
|
+
|
151
|
+
get '/', :provides => 'html' do
|
152
|
+
haml :index
|
153
|
+
end
|
154
|
+
|
155
|
+
get '/', :provides => ['rss', 'atom', 'xml'] do
|
156
|
+
builder :feed
|
157
|
+
end
|
158
|
+
```
|
159
|
+
|
160
|
+
You can easily define your own conditions:
|
161
|
+
|
162
|
+
```ruby
|
163
|
+
set(:probability) { |value| condition { rand <= value } }
|
164
|
+
|
165
|
+
get '/win_a_car', :probability => 0.1 do
|
166
|
+
"You won!"
|
167
|
+
end
|
168
|
+
|
169
|
+
get '/win_a_car' do
|
170
|
+
"Sorry, you lost."
|
171
|
+
end
|
172
|
+
```
|
173
|
+
|
174
|
+
For a condition that takes multiple values use a splat:
|
175
|
+
|
176
|
+
```ruby
|
177
|
+
set(:auth) do |*roles| # <- notice the splat here
|
178
|
+
condition do
|
179
|
+
unless logged_in? && roles.any? {|role| current_user.in_role? role }
|
180
|
+
redirect "/login/", 303
|
181
|
+
end
|
182
|
+
end
|
183
|
+
end
|
184
|
+
|
185
|
+
get "/my/account/", :auth => [:user, :admin] do
|
186
|
+
"Your Account Details"
|
187
|
+
end
|
188
|
+
|
189
|
+
get "/only/admin/", :auth => :admin do
|
190
|
+
"Only admins are allowed here!"
|
191
|
+
end
|
192
|
+
```
|
193
|
+
|
194
|
+
### Return Values
|
195
|
+
|
196
|
+
The return value of a route block determines at least the response body passed
|
197
|
+
on to the HTTP client, or at least the next middleware in the Rack stack.
|
198
|
+
Most commonly, this is a string, as in the above examples. But other values are
|
199
|
+
also accepted.
|
200
|
+
|
201
|
+
You can return any object that would either be a valid Rack response, Rack
|
202
|
+
body object or HTTP status code:
|
203
|
+
|
204
|
+
* An Array with three elements: `[status (Fixnum), headers (Hash), response
|
205
|
+
body (responds to #each)]`
|
206
|
+
* An Array with two elements: `[status (Fixnum), response body (responds to
|
207
|
+
#each)]`
|
208
|
+
* An object that responds to `#each` and passes nothing but strings to
|
209
|
+
the given block
|
210
|
+
* A Fixnum representing the status code
|
211
|
+
|
212
|
+
That way we can, for instance, easily implement a streaming example:
|
213
|
+
|
214
|
+
```ruby
|
215
|
+
class Stream
|
216
|
+
def each
|
217
|
+
100.times { |i| yield "#{i}\n" }
|
218
|
+
end
|
219
|
+
end
|
220
|
+
|
221
|
+
get('/') { Stream.new }
|
222
|
+
```
|
223
|
+
|
224
|
+
You can also use the `stream` helper method (described below) to reduce boiler
|
225
|
+
plate and embed the streaming logic in the route.
|
226
|
+
|
227
|
+
### Custom Route Matchers
|
228
|
+
|
229
|
+
As shown above, Sinatra ships with built-in support for using String patterns
|
230
|
+
and regular expressions as route matches. However, it does not stop there. You
|
231
|
+
can easily define your own matchers:
|
232
|
+
|
233
|
+
```ruby
|
234
|
+
class AllButPattern
|
235
|
+
Match = Struct.new(:captures)
|
236
|
+
|
237
|
+
def initialize(except)
|
238
|
+
@except = except
|
239
|
+
@captures = Match.new([])
|
240
|
+
end
|
241
|
+
|
242
|
+
def match(str)
|
243
|
+
@captures unless @except === str
|
244
|
+
end
|
245
|
+
end
|
246
|
+
|
247
|
+
def all_but(pattern)
|
248
|
+
AllButPattern.new(pattern)
|
249
|
+
end
|
250
|
+
|
251
|
+
get all_but("/index") do
|
252
|
+
# ...
|
253
|
+
end
|
254
|
+
```
|
255
|
+
|
256
|
+
Note that the above example might be over-engineered, as it can also be
|
257
|
+
expressed as:
|
258
|
+
|
259
|
+
```ruby
|
260
|
+
get // do
|
261
|
+
pass if request.path_info == "/index"
|
262
|
+
# ...
|
263
|
+
end
|
264
|
+
```
|
265
|
+
|
266
|
+
Or, using negative look ahead:
|
267
|
+
|
268
|
+
```ruby
|
269
|
+
get %r{^(?!/index$)} do
|
270
|
+
# ...
|
271
|
+
end
|
272
|
+
```
|
273
|
+
|
274
|
+
### Static Files
|
275
|
+
|
276
|
+
Static files are served from the `./public` directory. You can specify
|
277
|
+
a different location by setting the `:public_folder` option:
|
278
|
+
|
279
|
+
```ruby
|
280
|
+
set :public_folder, File.dirname(__FILE__) + '/static'
|
281
|
+
```
|
282
|
+
|
283
|
+
Note that the public directory name is not included in the URL. A file
|
284
|
+
`./public/css/style.css` is made available as
|
285
|
+
`http://example.com/css/style.css`.
|
286
|
+
|
287
|
+
Use the `:static_cache_control` setting (see below) to add
|
288
|
+
`Cache-Control` header info.
|
289
|
+
|
290
|
+
### Views / Templates
|
291
|
+
|
292
|
+
Each template language is exposed via its own rendering method. These
|
293
|
+
methods simply return a string:
|
294
|
+
|
295
|
+
```ruby
|
296
|
+
get '/' do
|
297
|
+
erb :index
|
298
|
+
end
|
299
|
+
```
|
300
|
+
|
301
|
+
This renders `views/index.erb`.
|
302
|
+
|
303
|
+
Instead of a template name, you can also just pass in the template content
|
304
|
+
directly:
|
305
|
+
|
306
|
+
```ruby
|
307
|
+
get '/' do
|
308
|
+
code = "<%= Time.now %>"
|
309
|
+
erb code
|
310
|
+
end
|
311
|
+
```
|
312
|
+
|
313
|
+
Templates take a second argument, the options hash:
|
314
|
+
|
315
|
+
```ruby
|
316
|
+
get '/' do
|
317
|
+
erb :index, :layout => :post
|
318
|
+
end
|
319
|
+
```
|
320
|
+
|
321
|
+
This will render `views/index.erb` embedded in the
|
322
|
+
`views/post.erb` (default is `views/layout.erb`, if it exists).
|
323
|
+
|
324
|
+
Any options not understood by Sinatra will be passed on to the template
|
325
|
+
engine:
|
326
|
+
|
327
|
+
```ruby
|
328
|
+
get '/' do
|
329
|
+
haml :index, :format => :html5
|
330
|
+
end
|
331
|
+
```
|
332
|
+
|
333
|
+
You can also set options per template language in general:
|
334
|
+
|
335
|
+
```ruby
|
336
|
+
set :haml, :format => :html5
|
337
|
+
|
338
|
+
get '/' do
|
339
|
+
haml :index
|
340
|
+
end
|
341
|
+
```
|
342
|
+
|
343
|
+
Options passed to the render method override options set via `set`.
|
344
|
+
|
345
|
+
Available Options:
|
346
|
+
|
347
|
+
<dl>
|
348
|
+
<dt>locals</dt>
|
349
|
+
<dd>
|
350
|
+
List of locals passed to the document. Handy with partials.
|
351
|
+
Example: <tt>erb "<%= foo %>", :locals => {:foo => "bar"}</tt>
|
352
|
+
</dd>
|
353
|
+
|
354
|
+
<dt>default_encoding</dt>
|
355
|
+
<dd>
|
356
|
+
String encoding to use if uncertain. Defaults to
|
357
|
+
<tt>settings.default_encoding</tt>.
|
358
|
+
</dd>
|
359
|
+
|
360
|
+
<dt>views</dt>
|
361
|
+
<dd>
|
362
|
+
Views folder to load templates from. Defaults to <tt>settings.views</tt>.
|
363
|
+
</dd>
|
364
|
+
|
365
|
+
<dt>layout</dt>
|
366
|
+
<dd>
|
367
|
+
Whether to use a layout (<tt>true</tt> or <tt>false</tt>), if it's a Symbol, specifies
|
368
|
+
what template to use. Example: <tt>erb :index, :layout => !request.xhr?</tt>
|
369
|
+
</dd>
|
370
|
+
|
371
|
+
<dt>content_type</dt>
|
372
|
+
<dd>
|
373
|
+
Content-Type the template produces, default depends on template language.
|
374
|
+
</dd>
|
375
|
+
|
376
|
+
<dt>scope</dt>
|
377
|
+
<dd>
|
378
|
+
Scope to render template under. Defaults to the application instance. If you
|
379
|
+
change this, instance variables and helper methods will not be available.
|
380
|
+
</dd>
|
381
|
+
|
382
|
+
<dt>layout_engine</dt>
|
383
|
+
<dd>
|
384
|
+
Template engine to use for rendering the layout. Useful for languages that
|
385
|
+
do not support layouts otherwise. Defaults to the engine used for the
|
386
|
+
template. Example: <tt>set :rdoc, :layout_engine => :erb</tt>
|
387
|
+
</dd>
|
388
|
+
|
389
|
+
<dt>layout_options</dt>
|
390
|
+
<dd>
|
391
|
+
Special options only used for rendering the layout. Example:
|
392
|
+
<tt>set :rdoc, :layout_options => { :views => 'views/layouts' }</tt>
|
393
|
+
</dd>
|
394
|
+
|
395
|
+
<dd>
|
396
|
+
Templates are assumed to be located directly under the `./views`
|
397
|
+
directory. To use a different views directory:
|
398
|
+
<tt>set :views, settings.root + '/templates'</tt>
|
399
|
+
</dd>
|
400
|
+
|
401
|
+
<dd>
|
402
|
+
One important thing to remember is that you always have to reference
|
403
|
+
templates with symbols, even if they're in a subdirectory (in this
|
404
|
+
case, use: <tt>'subdir/template'</tt>). You must use a symbol because
|
405
|
+
otherwise rendering methods will render any strings passed to them
|
406
|
+
directly.
|
407
|
+
</dd>
|
408
|
+
</dl>
|
409
|
+
|
410
|
+
#### Literal Templates
|
411
|
+
|
412
|
+
```ruby
|
413
|
+
get '/' do
|
414
|
+
haml '%div.title Hello World'
|
415
|
+
end
|
416
|
+
```
|
417
|
+
|
418
|
+
Renders the template string.
|
419
|
+
|
420
|
+
### Available Template Languages
|
421
|
+
|
422
|
+
Some languages have multiple implementations. To specify what implementation
|
423
|
+
to use (and to be thread-safe), you should simply require it first:
|
424
|
+
|
425
|
+
```ruby
|
426
|
+
require 'rdiscount' # or require 'bluecloth'
|
427
|
+
get('/') { markdown :index }
|
428
|
+
```
|
429
|
+
|
430
|
+
#### Haml Templates
|
431
|
+
|
432
|
+
<table>
|
433
|
+
<tr>
|
434
|
+
<td>Dependency</td>
|
435
|
+
<td><a href="http://haml.info/" title="haml">haml</a></td>
|
436
|
+
</tr>
|
437
|
+
<tr>
|
438
|
+
<td>File Extension</td>
|
439
|
+
<td><tt>.haml</tt></td>
|
440
|
+
</tr>
|
441
|
+
<tr>
|
442
|
+
<td>Example</td>
|
443
|
+
<td><tt>haml :index, :format => :html5</tt></td>
|
444
|
+
</tr>
|
445
|
+
</table>
|
446
|
+
|
447
|
+
#### Erb Templates
|
448
|
+
|
449
|
+
<table>
|
450
|
+
<tr>
|
451
|
+
<td>Dependency</td>
|
452
|
+
<td>
|
453
|
+
<a href="http://www.kuwata-lab.com/erubis/" title="erubis">erubis</a>
|
454
|
+
or erb (included in Ruby)
|
455
|
+
</td>
|
456
|
+
</tr>
|
457
|
+
<tr>
|
458
|
+
<td>File Extensions</td>
|
459
|
+
<td><tt>.erb</tt>, <tt>.rhtml</tt> or <tt>.erubis</tt> (Erubis only)</td>
|
460
|
+
</tr>
|
461
|
+
<tr>
|
462
|
+
<td>Example</td>
|
463
|
+
<td><tt>erb :index</tt></td>
|
464
|
+
</tr>
|
465
|
+
</table>
|
466
|
+
|
467
|
+
#### Builder Templates
|
468
|
+
|
469
|
+
<table>
|
470
|
+
<tr>
|
471
|
+
<td>Dependency</td>
|
472
|
+
<td>
|
473
|
+
<a href="http://builder.rubyforge.org/" title="builder">builder</a>
|
474
|
+
</td>
|
475
|
+
</tr>
|
476
|
+
<tr>
|
477
|
+
<td>File Extension</td>
|
478
|
+
<td><tt>.builder</tt></td>
|
479
|
+
</tr>
|
480
|
+
<tr>
|
481
|
+
<td>Example</td>
|
482
|
+
<td><tt>builder { |xml| xml.em "hi" }</tt></td>
|
483
|
+
</tr>
|
484
|
+
</table>
|
485
|
+
|
486
|
+
It also takes a block for inline templates (see example).
|
487
|
+
|
488
|
+
#### Nokogiri Templates
|
489
|
+
|
490
|
+
<table>
|
491
|
+
<tr>
|
492
|
+
<td>Dependency</td>
|
493
|
+
<td><a href="http://nokogiri.org/" title="nokogiri">nokogiri</a></td>
|
494
|
+
</tr>
|
495
|
+
<tr>
|
496
|
+
<td>File Extension</td>
|
497
|
+
<td><tt>.nokogiri</tt></td>
|
498
|
+
</tr>
|
499
|
+
<tr>
|
500
|
+
<td>Example</td>
|
501
|
+
<td><tt>nokogiri { |xml| xml.em "hi" }</tt></td>
|
502
|
+
</tr>
|
503
|
+
</table>
|
504
|
+
|
505
|
+
It also takes a block for inline templates (see example).
|
506
|
+
|
507
|
+
#### Sass Templates
|
508
|
+
|
509
|
+
<table>
|
510
|
+
<tr>
|
511
|
+
<td>Dependency</td>
|
512
|
+
<td><a href="http://sass-lang.com/" title="sass">sass</a></td>
|
513
|
+
</tr>
|
514
|
+
<tr>
|
515
|
+
<td>File Extension</td>
|
516
|
+
<td><tt>.sass</tt></td>
|
517
|
+
</tr>
|
518
|
+
<tr>
|
519
|
+
<td>Example</td>
|
520
|
+
<td><tt>sass :stylesheet, :style => :expanded</tt></td>
|
521
|
+
</tr>
|
522
|
+
</table>
|
523
|
+
|
524
|
+
#### SCSS Templates
|
525
|
+
|
526
|
+
<table>
|
527
|
+
<tr>
|
528
|
+
<td>Dependency</td>
|
529
|
+
<td><a href="http://sass-lang.com/" title="sass">sass</a></td>
|
530
|
+
</tr>
|
531
|
+
<tr>
|
532
|
+
<td>File Extension</td>
|
533
|
+
<td><tt>.scss</tt></td>
|
534
|
+
</tr>
|
535
|
+
<tr>
|
536
|
+
<td>Example</td>
|
537
|
+
<td><tt>scss :stylesheet, :style => :expanded</tt></td>
|
538
|
+
</tr>
|
539
|
+
</table>
|
540
|
+
|
541
|
+
#### Less Templates
|
542
|
+
|
543
|
+
<table>
|
544
|
+
<tr>
|
545
|
+
<td>Dependency</td>
|
546
|
+
<td><a href="http://www.lesscss.org/" title="less">less</a></td>
|
547
|
+
</tr>
|
548
|
+
<tr>
|
549
|
+
<td>File Extension</td>
|
550
|
+
<td><tt>.less</tt></td>
|
551
|
+
</tr>
|
552
|
+
<tr>
|
553
|
+
<td>Example</td>
|
554
|
+
<td><tt>less :stylesheet</tt></td>
|
555
|
+
</tr>
|
556
|
+
</table>
|
557
|
+
|
558
|
+
#### Liquid Templates
|
559
|
+
|
560
|
+
<table>
|
561
|
+
<tr>
|
562
|
+
<td>Dependency</td>
|
563
|
+
<td><a href="http://www.liquidmarkup.org/" title="liquid">liquid</a></td>
|
564
|
+
</tr>
|
565
|
+
<tr>
|
566
|
+
<td>File Extension</td>
|
567
|
+
<td><tt>.liquid</tt></td>
|
568
|
+
</tr>
|
569
|
+
<tr>
|
570
|
+
<td>Example</td>
|
571
|
+
<td><tt>liquid :index, :locals => { :key => 'value' }</tt></td>
|
572
|
+
</tr>
|
573
|
+
</table>
|
574
|
+
|
575
|
+
Since you cannot call Ruby methods (except for `yield`) from a Liquid
|
576
|
+
template, you almost always want to pass locals to it.
|
577
|
+
|
578
|
+
#### Markdown Templates
|
579
|
+
|
580
|
+
<table>
|
581
|
+
<tr>
|
582
|
+
<td>Dependency</td>
|
583
|
+
<td>
|
584
|
+
Anyone of:
|
585
|
+
<a href="https://github.com/rtomayko/rdiscount" title="RDiscount">RDiscount</a>,
|
586
|
+
<a href="https://github.com/vmg/redcarpet" title="RedCarpet">RedCarpet</a>,
|
587
|
+
<a href="http://deveiate.org/projects/BlueCloth" title="BlueCloth">BlueCloth</a>,
|
588
|
+
<a href="http://kramdown.rubyforge.org/" title="kramdown">kramdown</a>,
|
589
|
+
<a href="http://maruku.rubyforge.org/" title="maruku">maruku</a>
|
590
|
+
</td>
|
591
|
+
</tr>
|
592
|
+
<tr>
|
593
|
+
<td>File Extensions</td>
|
594
|
+
<td><tt>.markdown</tt>, <tt>.mkd</tt> and <tt>.md</tt></td>
|
595
|
+
</tr>
|
596
|
+
<tr>
|
597
|
+
<td>Example</td>
|
598
|
+
<td><tt>markdown :index, :layout_engine => :erb</tt></td>
|
599
|
+
</tr>
|
600
|
+
</table>
|
601
|
+
|
602
|
+
It is not possible to call methods from markdown, nor to pass locals to it.
|
603
|
+
You therefore will usually use it in combination with another rendering
|
604
|
+
engine:
|
605
|
+
|
606
|
+
```ruby
|
607
|
+
erb :overview, :locals => { :text => markdown(:introduction) }
|
608
|
+
```
|
609
|
+
|
610
|
+
Note that you may also call the `markdown` method from within other templates:
|
611
|
+
|
612
|
+
```ruby
|
613
|
+
%h1 Hello From Haml!
|
614
|
+
%p= markdown(:greetings)
|
615
|
+
```
|
616
|
+
|
617
|
+
Since you cannot call Ruby from Markdown, you cannot use layouts written in
|
618
|
+
Markdown. However, it is possible to use another rendering engine for the
|
619
|
+
template than for the layout by passing the `:layout_engine` option.
|
620
|
+
|
621
|
+
#### Textile Templates
|
622
|
+
|
623
|
+
<table>
|
624
|
+
<tr>
|
625
|
+
<td>Dependency</td>
|
626
|
+
<td><a href="http://redcloth.org/" title="RedCloth">RedCloth</a></td>
|
627
|
+
</tr>
|
628
|
+
<tr>
|
629
|
+
<td>File Extension</td>
|
630
|
+
<td><tt>.textile</tt></td>
|
631
|
+
</tr>
|
632
|
+
<tr>
|
633
|
+
<td>Example</td>
|
634
|
+
<td><tt>textile :index, :layout_engine => :erb</tt></td>
|
635
|
+
</tr>
|
636
|
+
</table>
|
637
|
+
|
638
|
+
|
639
|
+
It is not possible to call methods from textile, nor to pass locals to it. You
|
640
|
+
therefore will usually use it in combination with another rendering engine:
|
641
|
+
|
642
|
+
```ruby
|
643
|
+
erb :overview, :locals => { :text => textile(:introduction) }
|
644
|
+
```
|
645
|
+
|
646
|
+
Note that you may also call the `textile` method from within other templates:
|
647
|
+
|
648
|
+
```ruby
|
649
|
+
%h1 Hello From Haml!
|
650
|
+
%p= textile(:greetings)
|
651
|
+
```
|
652
|
+
|
653
|
+
Since you cannot call Ruby from Textile, you cannot use layouts written in
|
654
|
+
Textile. However, it is possible to use another rendering engine for the
|
655
|
+
template than for the layout by passing the `:layout_engine` option.
|
656
|
+
|
657
|
+
#### RDoc Templates
|
658
|
+
|
659
|
+
<table>
|
660
|
+
<tr>
|
661
|
+
<td>Dependency</td>
|
662
|
+
<td><a href="http://rdoc.rubyforge.org/" title="RDoc">RDoc</a></td>
|
663
|
+
</tr>
|
664
|
+
<tr>
|
665
|
+
<td>File Extension</td>
|
666
|
+
<td><tt>.rdoc</tt></td>
|
667
|
+
</tr>
|
668
|
+
<tr>
|
669
|
+
<td>Example</td>
|
670
|
+
<td><tt>rdoc :README, :layout_engine => :erb</tt></td>
|
671
|
+
</tr>
|
672
|
+
</table>
|
673
|
+
|
674
|
+
It is not possible to call methods from rdoc, nor to pass locals to it. You
|
675
|
+
therefore will usually use it in combination with another rendering engine:
|
676
|
+
|
677
|
+
```ruby
|
678
|
+
erb :overview, :locals => { :text => rdoc(:introduction) }
|
679
|
+
```
|
680
|
+
|
681
|
+
Note that you may also call the `rdoc` method from within other templates:
|
682
|
+
|
683
|
+
```ruby
|
684
|
+
%h1 Hello From Haml!
|
685
|
+
%p= rdoc(:greetings)
|
686
|
+
```
|
687
|
+
|
688
|
+
Since you cannot call Ruby from RDoc, you cannot use layouts written in
|
689
|
+
RDoc. However, it is possible to use another rendering engine for the
|
690
|
+
template than for the layout by passing the `:layout_engine` option.
|
691
|
+
|
692
|
+
#### Radius Templates
|
693
|
+
|
694
|
+
<table>
|
695
|
+
<tr>
|
696
|
+
<td>Dependency</td>
|
697
|
+
<td><a href="http://radius.rubyforge.org/" title="Radius">Radius</a></td>
|
698
|
+
</tr>
|
699
|
+
<tr>
|
700
|
+
<td>File Extension</td>
|
701
|
+
<td><tt>.radius</tt></td>
|
702
|
+
</tr>
|
703
|
+
<tr>
|
704
|
+
<td>Example</td>
|
705
|
+
<td><tt>radius :index, :locals => { :key => 'value' }</tt></td>
|
706
|
+
</tr>
|
707
|
+
</table>
|
708
|
+
|
709
|
+
Since you cannot call Ruby methods directly from a Radius template, you almost
|
710
|
+
always want to pass locals to it.
|
711
|
+
|
712
|
+
#### Markaby Templates
|
713
|
+
|
714
|
+
<table>
|
715
|
+
<tr>
|
716
|
+
<td>Dependency</td>
|
717
|
+
<td><a href="http://markaby.github.com/" title="Markaby">Markaby</a></td>
|
718
|
+
</tr>
|
719
|
+
<tr>
|
720
|
+
<td>File Extension</td>
|
721
|
+
<td><tt>.mab</tt></td>
|
722
|
+
</tr>
|
723
|
+
<tr>
|
724
|
+
<td>Example</td>
|
725
|
+
<td><tt>markaby { h1 "Welcome!" }</tt></td>
|
726
|
+
</tr>
|
727
|
+
</table>
|
728
|
+
|
729
|
+
It also takes a block for inline templates (see example).
|
730
|
+
|
731
|
+
#### RABL Templates
|
732
|
+
|
733
|
+
<table>
|
734
|
+
<tr>
|
735
|
+
<td>Dependency</td>
|
736
|
+
<td><a href="https://github.com/nesquena/rabl" title="Rabl">Rabl</a></td>
|
737
|
+
</tr>
|
738
|
+
<tr>
|
739
|
+
<td>File Extension</td>
|
740
|
+
<td><tt>.rabl</tt></td>
|
741
|
+
</tr>
|
742
|
+
<tr>
|
743
|
+
<td>Example</td>
|
744
|
+
<td><tt>rabl :index</tt></td>
|
745
|
+
</tr>
|
746
|
+
</table>
|
747
|
+
|
748
|
+
#### Slim Templates
|
749
|
+
|
750
|
+
<table>
|
751
|
+
<tr>
|
752
|
+
<td>Dependency</td>
|
753
|
+
<td><a href="http://slim-lang.com/" title="Slim Lang">Slim Lang</a></td>
|
754
|
+
</tr>
|
755
|
+
<tr>
|
756
|
+
<td>File Extension</td>
|
757
|
+
<td><tt>.slim</tt></td>
|
758
|
+
</tr>
|
759
|
+
<tr>
|
760
|
+
<td>Example</td>
|
761
|
+
<td><tt>slim :index</tt></td>
|
762
|
+
</tr>
|
763
|
+
</table>
|
764
|
+
|
765
|
+
#### Creole Templates
|
766
|
+
|
767
|
+
<table>
|
768
|
+
<tr>
|
769
|
+
<td>Dependency</td>
|
770
|
+
<td><a href="https://github.com/minad/creole" title="Creole">Creole</a></td>
|
771
|
+
</tr>
|
772
|
+
<tr>
|
773
|
+
<td>File Extension</td>
|
774
|
+
<td><tt>.creole</tt></td>
|
775
|
+
</tr>
|
776
|
+
<tr>
|
777
|
+
<td>Example</td>
|
778
|
+
<td><tt>creole :wiki, :layout_engine => :erb</tt></td>
|
779
|
+
</tr>
|
780
|
+
</table>
|
781
|
+
|
782
|
+
It is not possible to call methods from creole, nor to pass locals to it. You
|
783
|
+
therefore will usually use it in combination with another rendering engine:
|
784
|
+
|
785
|
+
```ruby
|
786
|
+
erb :overview, :locals => { :text => creole(:introduction) }
|
787
|
+
```
|
788
|
+
|
789
|
+
Note that you may also call the `creole` method from within other templates:
|
790
|
+
|
791
|
+
```ruby
|
792
|
+
%h1 Hello From Haml!
|
793
|
+
%p= creole(:greetings)
|
794
|
+
```
|
795
|
+
|
796
|
+
Since you cannot call Ruby from Creole, you cannot use layouts written in
|
797
|
+
Creole. However, it is possible to use another rendering engine for the
|
798
|
+
template than for the layout by passing the `:layout_engine` option.
|
799
|
+
|
800
|
+
#### CoffeeScript Templates
|
801
|
+
|
802
|
+
<table>
|
803
|
+
<tr>
|
804
|
+
<td>Dependency</td>
|
805
|
+
<td>
|
806
|
+
<a href="https://github.com/josh/ruby-coffee-script" title="Ruby CoffeeScript">
|
807
|
+
CoffeeScript
|
808
|
+
</a> and a
|
809
|
+
<a href="https://github.com/sstephenson/execjs/blob/master/README.md#readme" title="ExecJS">
|
810
|
+
way to execute javascript
|
811
|
+
</a>
|
812
|
+
</td>
|
813
|
+
</tr>
|
814
|
+
<tr>
|
815
|
+
<td>File Extension</td>
|
816
|
+
<td><tt>.coffee</tt></td>
|
817
|
+
</tr>
|
818
|
+
<tr>
|
819
|
+
<td>Example</td>
|
820
|
+
<td><tt>coffee :index</tt></td>
|
821
|
+
</tr>
|
822
|
+
</table>
|
823
|
+
|
824
|
+
#### Yajl Templates
|
825
|
+
|
826
|
+
<table>
|
827
|
+
<tr>
|
828
|
+
<td>Dependency</td>
|
829
|
+
<td><a href="https://github.com/brianmario/yajl-ruby" title="yajl-ruby">yajl-ruby</a></td>
|
830
|
+
</tr>
|
831
|
+
<tr>
|
832
|
+
<td>File Extension</td>
|
833
|
+
<td><tt>.yajl</tt></td>
|
834
|
+
</tr>
|
835
|
+
<tr>
|
836
|
+
<td>Example</td>
|
837
|
+
<td>
|
838
|
+
<tt>
|
839
|
+
yajl :index,
|
840
|
+
:locals => { :key => 'qux' },
|
841
|
+
:callback => 'present',
|
842
|
+
:variable => 'resource'
|
843
|
+
</tt>
|
844
|
+
</td>
|
845
|
+
</tr>
|
846
|
+
</table>
|
847
|
+
|
848
|
+
|
849
|
+
The template source is evaluated as a Ruby string, and the
|
850
|
+
resulting json variable is converted using `#to_json`.
|
851
|
+
|
852
|
+
```ruby
|
853
|
+
json = { :foo => 'bar' }
|
854
|
+
json[:baz] = key
|
855
|
+
```
|
856
|
+
|
857
|
+
The `:callback` and `:variable` options can be used to decorate the rendered object.
|
858
|
+
|
859
|
+
```ruby
|
860
|
+
var resource = {"foo":"bar","baz":"qux"}; present(resource);
|
861
|
+
```
|
862
|
+
|
863
|
+
#### WLang Templates
|
864
|
+
|
865
|
+
<table>
|
866
|
+
<tr>
|
867
|
+
<td>Dependency</td>
|
868
|
+
<td><a href="https://github.com/blambeau/wlang/" title="wlang">wlang</a></td>
|
869
|
+
</tr>
|
870
|
+
<tr>
|
871
|
+
<td>File Extension</td>
|
872
|
+
<td><tt>.wlang</tt></td>
|
873
|
+
</tr>
|
874
|
+
<tr>
|
875
|
+
<td>Example</td>
|
876
|
+
<td><tt>wlang :index, :locals => { :key => 'value' }</tt></td>
|
877
|
+
</tr>
|
878
|
+
</table>
|
879
|
+
|
880
|
+
Since calling ruby methods is not idiomatic in wlang, you almost always want to pass locals
|
881
|
+
to it. Layouts written in wlang and `yield` are supported, though.
|
882
|
+
|
883
|
+
### Accessing Variables in Templates
|
884
|
+
|
885
|
+
Templates are evaluated within the same context as route handlers. Instance
|
886
|
+
variables set in route handlers are directly accessible by templates:
|
887
|
+
|
888
|
+
```ruby
|
889
|
+
get '/:id' do
|
890
|
+
@foo = Foo.find(params[:id])
|
891
|
+
haml '%h1= @foo.name'
|
892
|
+
end
|
893
|
+
```
|
894
|
+
|
895
|
+
Or, specify an explicit Hash of local variables:
|
896
|
+
|
897
|
+
```ruby
|
898
|
+
get '/:id' do
|
899
|
+
foo = Foo.find(params[:id])
|
900
|
+
haml '%h1= bar.name', :locals => { :bar => foo }
|
901
|
+
end
|
902
|
+
```
|
903
|
+
|
904
|
+
This is typically used when rendering templates as partials from within
|
905
|
+
other templates.
|
906
|
+
|
907
|
+
### Templates with `yield` and nested layouts
|
908
|
+
|
909
|
+
A layout is usually just a template that calls `yield`.
|
910
|
+
Such a template can by used either through the `:template` option as
|
911
|
+
described above, or it can be rendered with a block as follows:
|
912
|
+
|
913
|
+
```ruby
|
914
|
+
erb :post, :layout => false do
|
915
|
+
erb :index
|
916
|
+
end
|
917
|
+
```
|
918
|
+
|
919
|
+
This code is mostly equivalent to `erb :index, :layout => :post`.
|
920
|
+
|
921
|
+
Passing blocks to rendering methods is most useful for creating nested
|
922
|
+
layouts:
|
923
|
+
|
924
|
+
```ruby
|
925
|
+
erb :main_layout, :layout => false do
|
926
|
+
erb :admin_layout do
|
927
|
+
erb :user
|
928
|
+
end
|
929
|
+
end
|
930
|
+
```
|
931
|
+
|
932
|
+
This can also be done in fewer lines of code with:
|
933
|
+
|
934
|
+
```ruby
|
935
|
+
erb :admin_layout, :layout => :main_layout do
|
936
|
+
erb :user
|
937
|
+
end
|
938
|
+
```
|
939
|
+
|
940
|
+
Currently the following rendering method accept a block: `erb`, `haml`,
|
941
|
+
`liquid`, `slim `, `wlang`.
|
942
|
+
Also the general `render` method accepts a block.
|
943
|
+
|
944
|
+
### Inline Templates
|
945
|
+
|
946
|
+
Templates may be defined at the end of the source file:
|
947
|
+
|
948
|
+
```ruby
|
949
|
+
require 'sinatra'
|
950
|
+
|
951
|
+
get '/' do
|
952
|
+
haml :index
|
953
|
+
end
|
954
|
+
|
955
|
+
__END__
|
956
|
+
|
957
|
+
@@ layout
|
958
|
+
%html
|
959
|
+
= yield
|
960
|
+
|
961
|
+
@@ index
|
962
|
+
%div.title Hello world.
|
963
|
+
```
|
964
|
+
|
965
|
+
NOTE: Inline templates defined in the source file that requires sinatra are
|
966
|
+
automatically loaded. Call `enable :inline_templates` explicitly if you
|
967
|
+
have inline templates in other source files.
|
968
|
+
|
969
|
+
### Named Templates
|
970
|
+
|
971
|
+
Templates may also be defined using the top-level `template` method:
|
972
|
+
|
973
|
+
```ruby
|
974
|
+
template :layout do
|
975
|
+
"%html\n =yield\n"
|
976
|
+
end
|
977
|
+
|
978
|
+
template :index do
|
979
|
+
'%div.title Hello World!'
|
980
|
+
end
|
981
|
+
|
982
|
+
get '/' do
|
983
|
+
haml :index
|
984
|
+
end
|
985
|
+
```
|
986
|
+
|
987
|
+
If a template named "layout" exists, it will be used each time a template
|
988
|
+
is rendered. You can individually disable layouts by passing
|
989
|
+
`:layout => false` or disable them by default via
|
990
|
+
`set :haml, :layout => false`:
|
991
|
+
|
992
|
+
```ruby
|
993
|
+
get '/' do
|
994
|
+
haml :index, :layout => !request.xhr?
|
995
|
+
end
|
996
|
+
```
|
997
|
+
|
998
|
+
### Associating File Extensions
|
999
|
+
|
1000
|
+
To associate a file extension with a template engine, use
|
1001
|
+
`Tilt.register`. For instance, if you like to use the file extension
|
1002
|
+
`tt` for Textile templates, you can do the following:
|
1003
|
+
|
1004
|
+
```ruby
|
1005
|
+
Tilt.register :tt, Tilt[:textile]
|
1006
|
+
```
|
1007
|
+
|
1008
|
+
### Adding Your Own Template Engine
|
1009
|
+
|
1010
|
+
First, register your engine with Tilt, then create a rendering method:
|
1011
|
+
|
1012
|
+
```ruby
|
1013
|
+
Tilt.register :myat, MyAwesomeTemplateEngine
|
1014
|
+
|
1015
|
+
helpers do
|
1016
|
+
def myat(*args) render(:myat, *args) end
|
1017
|
+
end
|
1018
|
+
|
1019
|
+
get '/' do
|
1020
|
+
myat :index
|
1021
|
+
end
|
1022
|
+
```
|
1023
|
+
|
1024
|
+
Renders `./views/index.myat`. See https://github.com/rtomayko/tilt to
|
1025
|
+
learn more about Tilt.
|
1026
|
+
|
1027
|
+
## Filters
|
1028
|
+
|
1029
|
+
Before filters are evaluated before each request within the same
|
1030
|
+
context as the routes will be and can modify the request and response. Instance
|
1031
|
+
variables set in filters are accessible by routes and templates:
|
1032
|
+
|
1033
|
+
```ruby
|
1034
|
+
before do
|
1035
|
+
@note = 'Hi!'
|
1036
|
+
request.path_info = '/foo/bar/baz'
|
1037
|
+
end
|
1038
|
+
|
1039
|
+
get '/foo/*' do
|
1040
|
+
@note #=> 'Hi!'
|
1041
|
+
params[:splat] #=> 'bar/baz'
|
1042
|
+
end
|
1043
|
+
```
|
1044
|
+
|
1045
|
+
After filters are evaluated after each request within the same context and can
|
1046
|
+
also modify the request and response. Instance variables set in before filters
|
1047
|
+
and routes are accessible by after filters:
|
1048
|
+
|
1049
|
+
```ruby
|
1050
|
+
after do
|
1051
|
+
puts response.status
|
1052
|
+
end
|
1053
|
+
```
|
1054
|
+
|
1055
|
+
Note: Unless you use the `body` method rather than just returning a String from
|
1056
|
+
the routes, the body will not yet be available in the after filter, since it is
|
1057
|
+
generated later on.
|
1058
|
+
|
1059
|
+
Filters optionally take a pattern, causing them to be evaluated only if the
|
1060
|
+
request path matches that pattern:
|
1061
|
+
|
1062
|
+
```ruby
|
1063
|
+
before '/protected/*' do
|
1064
|
+
authenticate!
|
1065
|
+
end
|
1066
|
+
|
1067
|
+
after '/create/:slug' do |slug|
|
1068
|
+
session[:last_slug] = slug
|
1069
|
+
end
|
1070
|
+
```
|
1071
|
+
|
1072
|
+
Like routes, filters also take conditions:
|
1073
|
+
|
1074
|
+
```ruby
|
1075
|
+
before :agent => /Songbird/ do
|
1076
|
+
# ...
|
1077
|
+
end
|
1078
|
+
|
1079
|
+
after '/blog/*', :host_name => 'example.com' do
|
1080
|
+
# ...
|
1081
|
+
end
|
1082
|
+
```
|
1083
|
+
|
1084
|
+
## Helpers
|
1085
|
+
|
1086
|
+
Use the top-level `helpers` method to define helper methods for use in
|
1087
|
+
route handlers and templates:
|
1088
|
+
|
1089
|
+
```ruby
|
1090
|
+
helpers do
|
1091
|
+
def bar(name)
|
1092
|
+
"#{name}bar"
|
1093
|
+
end
|
1094
|
+
end
|
1095
|
+
|
1096
|
+
get '/:name' do
|
1097
|
+
bar(params[:name])
|
1098
|
+
end
|
1099
|
+
```
|
1100
|
+
|
1101
|
+
Alternatively, helper methods can be separately defined in a module:
|
1102
|
+
|
1103
|
+
```ruby
|
1104
|
+
module FooUtils
|
1105
|
+
def foo(name) "#{name}foo" end
|
1106
|
+
end
|
1107
|
+
|
1108
|
+
module BarUtils
|
1109
|
+
def bar(name) "#{name}bar" end
|
1110
|
+
end
|
1111
|
+
|
1112
|
+
helpers FooUtils, BarUtils
|
1113
|
+
```
|
1114
|
+
|
1115
|
+
The effect is the same as including the modules in the application class.
|
1116
|
+
|
1117
|
+
### Using Sessions
|
1118
|
+
|
1119
|
+
A session is used to keep state during requests. If activated, you have one
|
1120
|
+
session hash per user session:
|
1121
|
+
|
1122
|
+
```ruby
|
1123
|
+
enable :sessions
|
1124
|
+
|
1125
|
+
get '/' do
|
1126
|
+
"value = " << session[:value].inspect
|
1127
|
+
end
|
1128
|
+
|
1129
|
+
get '/:value' do
|
1130
|
+
session[:value] = params[:value]
|
1131
|
+
end
|
1132
|
+
```
|
1133
|
+
|
1134
|
+
Note that `enable :sessions` actually stores all data in a cookie. This
|
1135
|
+
might not always be what you want (storing lots of data will increase your
|
1136
|
+
traffic, for instance). You can use any Rack session middleware: in order to
|
1137
|
+
do so, do **not** call `enable :sessions`, but instead pull in your
|
1138
|
+
middleware of choice as you would any other middleware:
|
1139
|
+
|
1140
|
+
```ruby
|
1141
|
+
use Rack::Session::Pool, :expire_after => 2592000
|
1142
|
+
|
1143
|
+
get '/' do
|
1144
|
+
"value = " << session[:value].inspect
|
1145
|
+
end
|
1146
|
+
|
1147
|
+
get '/:value' do
|
1148
|
+
session[:value] = params[:value]
|
1149
|
+
end
|
1150
|
+
```
|
1151
|
+
|
1152
|
+
To improve security, the session data in the cookie is signed with a session
|
1153
|
+
secret. A random secret is generated for you by Sinatra. However, since this
|
1154
|
+
secret will change with every start of your application, you might want to
|
1155
|
+
set the secret yourself, so all your application instances share it:
|
1156
|
+
|
1157
|
+
```ruby
|
1158
|
+
set :session_secret, 'super secret'
|
1159
|
+
```
|
1160
|
+
|
1161
|
+
If you want to configure it further, you may also store a hash with options in
|
1162
|
+
the `sessions` setting:
|
1163
|
+
|
1164
|
+
```ruby
|
1165
|
+
set :sessions, :domain => 'foo.com'
|
1166
|
+
```
|
1167
|
+
|
1168
|
+
### Halting
|
1169
|
+
|
1170
|
+
To immediately stop a request within a filter or route use:
|
1171
|
+
|
1172
|
+
```ruby
|
1173
|
+
halt
|
1174
|
+
```
|
1175
|
+
|
1176
|
+
You can also specify the status when halting:
|
1177
|
+
|
1178
|
+
```ruby
|
1179
|
+
halt 410
|
1180
|
+
```
|
1181
|
+
|
1182
|
+
Or the body:
|
1183
|
+
|
1184
|
+
```ruby
|
1185
|
+
halt 'this will be the body'
|
1186
|
+
```
|
1187
|
+
|
1188
|
+
Or both:
|
1189
|
+
|
1190
|
+
```ruby
|
1191
|
+
halt 401, 'go away!'
|
1192
|
+
```
|
1193
|
+
|
1194
|
+
With headers:
|
1195
|
+
|
1196
|
+
```ruby
|
1197
|
+
halt 402, {'Content-Type' => 'text/plain'}, 'revenge'
|
1198
|
+
```
|
1199
|
+
|
1200
|
+
It is of course possible to combine a template with `halt`:
|
1201
|
+
|
1202
|
+
```ruby
|
1203
|
+
halt erb(:error)
|
1204
|
+
```
|
1205
|
+
|
1206
|
+
### Passing
|
1207
|
+
|
1208
|
+
A route can punt processing to the next matching route using `pass`:
|
1209
|
+
|
1210
|
+
```ruby
|
1211
|
+
get '/guess/:who' do
|
1212
|
+
pass unless params[:who] == 'Frank'
|
1213
|
+
'You got me!'
|
1214
|
+
end
|
1215
|
+
|
1216
|
+
get '/guess/*' do
|
1217
|
+
'You missed!'
|
1218
|
+
end
|
1219
|
+
```
|
1220
|
+
|
1221
|
+
The route block is immediately exited and control continues with the next
|
1222
|
+
matching route. If no matching route is found, a 404 is returned.
|
1223
|
+
|
1224
|
+
### Triggering Another Route
|
1225
|
+
|
1226
|
+
Sometimes `pass` is not what you want, instead you would like to get the result
|
1227
|
+
of calling another route. Simply use `call` to achieve this:
|
1228
|
+
|
1229
|
+
```ruby
|
1230
|
+
get '/foo' do
|
1231
|
+
status, headers, body = call env.merge("PATH_INFO" => '/bar')
|
1232
|
+
[status, headers, body.map(&:upcase)]
|
1233
|
+
end
|
1234
|
+
|
1235
|
+
get '/bar' do
|
1236
|
+
"bar"
|
1237
|
+
end
|
1238
|
+
```
|
1239
|
+
|
1240
|
+
Note that in the example above, you would ease testing and increase performance
|
1241
|
+
by simply moving `"bar"` into a helper used by both `/foo`
|
1242
|
+
and `/bar`.
|
1243
|
+
|
1244
|
+
If you want the request to be sent to the same application instance rather than
|
1245
|
+
a duplicate, use `call!` instead of `call`.
|
1246
|
+
|
1247
|
+
Check out the Rack specification if you want to learn more about `call`.
|
1248
|
+
|
1249
|
+
### Setting Body, Status Code and Headers
|
1250
|
+
|
1251
|
+
It is possible and recommended to set the status code and response body with the
|
1252
|
+
return value of the route block. However, in some scenarios you might want to
|
1253
|
+
set the body at an arbitrary point in the execution flow. You can do so with the
|
1254
|
+
`body` helper method. If you do so, you can use that method from there on to
|
1255
|
+
access the body:
|
1256
|
+
|
1257
|
+
```ruby
|
1258
|
+
get '/foo' do
|
1259
|
+
body "bar"
|
1260
|
+
end
|
1261
|
+
|
1262
|
+
after do
|
1263
|
+
puts body
|
1264
|
+
end
|
1265
|
+
```
|
1266
|
+
|
1267
|
+
It is also possible to pass a block to `body`, which will be executed by the
|
1268
|
+
Rack handler (this can be used to implement streaming, see "Return Values").
|
1269
|
+
|
1270
|
+
Similar to the body, you can also set the status code and headers:
|
1271
|
+
|
1272
|
+
```ruby
|
1273
|
+
get '/foo' do
|
1274
|
+
status 418
|
1275
|
+
headers \
|
1276
|
+
"Allow" => "BREW, POST, GET, PROPFIND, WHEN",
|
1277
|
+
"Refresh" => "Refresh: 20; http://www.ietf.org/rfc/rfc2324.txt"
|
1278
|
+
body "I'm a tea pot!"
|
1279
|
+
end
|
1280
|
+
```
|
1281
|
+
|
1282
|
+
Like `body`, `headers` and `status` with no arguments can be used to access
|
1283
|
+
their current values.
|
1284
|
+
|
1285
|
+
### Streaming Responses
|
1286
|
+
|
1287
|
+
Sometimes you want to start sending out data while still generating parts of
|
1288
|
+
the response body. In extreme examples, you want to keep sending data until
|
1289
|
+
the client closes the connection. You can use the `stream` helper to avoid
|
1290
|
+
creating your own wrapper:
|
1291
|
+
|
1292
|
+
```ruby
|
1293
|
+
get '/' do
|
1294
|
+
stream do |out|
|
1295
|
+
out << "It's gonna be legen -\n"
|
1296
|
+
sleep 0.5
|
1297
|
+
out << " (wait for it) \n"
|
1298
|
+
sleep 1
|
1299
|
+
out << "- dary!\n"
|
1300
|
+
end
|
1301
|
+
end
|
1302
|
+
```
|
1303
|
+
|
1304
|
+
This allows you to implement streaming APIs,
|
1305
|
+
[Server Sent Events](http://dev.w3.org/html5/eventsource/) and can be used as
|
1306
|
+
the basis for [WebSockets](http://en.wikipedia.org/wiki/WebSocket). It can also be
|
1307
|
+
used to increase throughput if some but not all content depends on a slow
|
1308
|
+
resource.
|
1309
|
+
|
1310
|
+
Note that the streaming behavior, especially the number of concurrent requests,
|
1311
|
+
highly depends on the web server used to serve the application. Some servers,
|
1312
|
+
like WEBRick, might not even support streaming at all. If the server does not
|
1313
|
+
support streaming, the body will be sent all at once after the block passed to
|
1314
|
+
`stream` finishes executing. Streaming does not work at all with Shotgun.
|
1315
|
+
|
1316
|
+
If the optional parameter is set to `keep_open`, it will not call `close` on
|
1317
|
+
the stream object, allowing you to close it at any later point in the
|
1318
|
+
execution flow. This only works on evented servers, like Thin and Rainbows.
|
1319
|
+
Other servers will still close the stream:
|
1320
|
+
|
1321
|
+
```ruby
|
1322
|
+
# long polling
|
1323
|
+
|
1324
|
+
set :server, :thin
|
1325
|
+
connections = []
|
1326
|
+
|
1327
|
+
get '/subscribe' do
|
1328
|
+
# register a client's interest in server events
|
1329
|
+
stream(:keep_open) { |out| connections << out }
|
1330
|
+
|
1331
|
+
# purge dead connections
|
1332
|
+
connections.reject!(&:closed?)
|
1333
|
+
|
1334
|
+
# acknowledge
|
1335
|
+
"subscribed"
|
1336
|
+
end
|
1337
|
+
|
1338
|
+
post '/message' do
|
1339
|
+
connections.each do |out|
|
1340
|
+
# notify client that a new message has arrived
|
1341
|
+
out << params[:message] << "\n"
|
1342
|
+
|
1343
|
+
# indicate client to connect again
|
1344
|
+
out.close
|
1345
|
+
end
|
1346
|
+
|
1347
|
+
# acknowledge
|
1348
|
+
"message received"
|
1349
|
+
end
|
1350
|
+
```
|
1351
|
+
|
1352
|
+
### Logging
|
1353
|
+
|
1354
|
+
In the request scope, the `logger` helper exposes a `Logger` instance:
|
1355
|
+
|
1356
|
+
```ruby
|
1357
|
+
get '/' do
|
1358
|
+
logger.info "loading data"
|
1359
|
+
# ...
|
1360
|
+
end
|
1361
|
+
```
|
1362
|
+
|
1363
|
+
This logger will automatically take your Rack handler's logging settings into
|
1364
|
+
account. If logging is disabled, this method will return a dummy object, so
|
1365
|
+
you do not have to worry in your routes and filters about it.
|
1366
|
+
|
1367
|
+
Note that logging is only enabled for `Sinatra::Application` by
|
1368
|
+
default, so if you inherit from `Sinatra::Base`, you probably want to
|
1369
|
+
enable it yourself:
|
1370
|
+
|
1371
|
+
```ruby
|
1372
|
+
class MyApp < Sinatra::Base
|
1373
|
+
configure :production, :development do
|
1374
|
+
enable :logging
|
1375
|
+
end
|
1376
|
+
end
|
1377
|
+
```
|
1378
|
+
|
1379
|
+
To avoid any logging middleware to be set up, set the `logging` setting to
|
1380
|
+
`nil`. However, keep in mind that `logger` will in that case return `nil`. A
|
1381
|
+
common use case is when you want to set your own logger. Sinatra will use
|
1382
|
+
whatever it will find in `env['rack.logger']`.
|
1383
|
+
|
1384
|
+
### Mime Types
|
1385
|
+
|
1386
|
+
When using `send_file` or static files you may have mime types Sinatra
|
1387
|
+
doesn't understand. Use `mime_type` to register them by file extension:
|
1388
|
+
|
1389
|
+
```ruby
|
1390
|
+
configure do
|
1391
|
+
mime_type :foo, 'text/foo'
|
1392
|
+
end
|
1393
|
+
```
|
1394
|
+
|
1395
|
+
You can also use it with the `content_type` helper:
|
1396
|
+
|
1397
|
+
```ruby
|
1398
|
+
get '/' do
|
1399
|
+
content_type :foo
|
1400
|
+
"foo foo foo"
|
1401
|
+
end
|
1402
|
+
```
|
1403
|
+
|
1404
|
+
### Generating URLs
|
1405
|
+
|
1406
|
+
For generating URLs you should use the `url` helper method, for instance, in
|
1407
|
+
Haml:
|
1408
|
+
|
1409
|
+
```ruby
|
1410
|
+
%a{:href => url('/foo')} foo
|
1411
|
+
```
|
1412
|
+
|
1413
|
+
It takes reverse proxies and Rack routers into account, if present.
|
1414
|
+
|
1415
|
+
This method is also aliased to `to` (see below for an example).
|
1416
|
+
|
1417
|
+
### Browser Redirect
|
1418
|
+
|
1419
|
+
You can trigger a browser redirect with the `redirect` helper method:
|
1420
|
+
|
1421
|
+
```ruby
|
1422
|
+
get '/foo' do
|
1423
|
+
redirect to('/bar')
|
1424
|
+
end
|
1425
|
+
```
|
1426
|
+
|
1427
|
+
Any additional parameters are handled like arguments passed to `halt`:
|
1428
|
+
|
1429
|
+
```ruby
|
1430
|
+
redirect to('/bar'), 303
|
1431
|
+
redirect 'http://google.com', 'wrong place, buddy'
|
1432
|
+
```
|
1433
|
+
|
1434
|
+
You can also easily redirect back to the page the user came from with
|
1435
|
+
`redirect back`:
|
1436
|
+
|
1437
|
+
```ruby
|
1438
|
+
get '/foo' do
|
1439
|
+
"<a href='/bar'>do something</a>"
|
1440
|
+
end
|
1441
|
+
|
1442
|
+
get '/bar' do
|
1443
|
+
do_something
|
1444
|
+
redirect back
|
1445
|
+
end
|
1446
|
+
```
|
1447
|
+
|
1448
|
+
To pass arguments with a redirect, either add them to the query:
|
1449
|
+
|
1450
|
+
```ruby
|
1451
|
+
redirect to('/bar?sum=42')
|
1452
|
+
```
|
1453
|
+
|
1454
|
+
Or use a session:
|
1455
|
+
|
1456
|
+
```ruby
|
1457
|
+
enable :sessions
|
1458
|
+
|
1459
|
+
get '/foo' do
|
1460
|
+
session[:secret] = 'foo'
|
1461
|
+
redirect to('/bar')
|
1462
|
+
end
|
1463
|
+
|
1464
|
+
get '/bar' do
|
1465
|
+
session[:secret]
|
1466
|
+
end
|
1467
|
+
```
|
1468
|
+
|
1469
|
+
### Cache Control
|
1470
|
+
|
1471
|
+
Setting your headers correctly is the foundation for proper HTTP caching.
|
1472
|
+
|
1473
|
+
You can easily set the Cache-Control header like this:
|
1474
|
+
|
1475
|
+
```ruby
|
1476
|
+
get '/' do
|
1477
|
+
cache_control :public
|
1478
|
+
"cache it!"
|
1479
|
+
end
|
1480
|
+
```
|
1481
|
+
|
1482
|
+
Pro tip: Set up caching in a before filter:
|
1483
|
+
|
1484
|
+
```ruby
|
1485
|
+
before do
|
1486
|
+
cache_control :public, :must_revalidate, :max_age => 60
|
1487
|
+
end
|
1488
|
+
```
|
1489
|
+
|
1490
|
+
If you are using the `expires` helper to set the corresponding header,
|
1491
|
+
`Cache-Control` will be set automatically for you:
|
1492
|
+
|
1493
|
+
```ruby
|
1494
|
+
before do
|
1495
|
+
expires 500, :public, :must_revalidate
|
1496
|
+
end
|
1497
|
+
```
|
1498
|
+
|
1499
|
+
To properly use caches, you should consider using `etag` or `last_modified`.
|
1500
|
+
It is recommended to call those helpers *before* doing any heavy lifting, as they
|
1501
|
+
will immediately flush a response if the client already has the current
|
1502
|
+
version in its cache:
|
1503
|
+
|
1504
|
+
```ruby
|
1505
|
+
get '/article/:id' do
|
1506
|
+
@article = Article.find params[:id]
|
1507
|
+
last_modified @article.updated_at
|
1508
|
+
etag @article.sha1
|
1509
|
+
erb :article
|
1510
|
+
end
|
1511
|
+
```
|
1512
|
+
|
1513
|
+
It is also possible to use a
|
1514
|
+
[weak ETag](http://en.wikipedia.org/wiki/HTTP_ETag#Strong_and_weak_validation):
|
1515
|
+
|
1516
|
+
```ruby
|
1517
|
+
etag @article.sha1, :weak
|
1518
|
+
```
|
1519
|
+
|
1520
|
+
These helpers will not do any caching for you, but rather feed the necessary
|
1521
|
+
information to your cache. If you are looking for a quick reverse-proxy caching
|
1522
|
+
solution, try [rack-cache](https://github.com/rtomayko/rack-cache):
|
1523
|
+
|
1524
|
+
```ruby
|
1525
|
+
require "rack/cache"
|
1526
|
+
require "sinatra"
|
1527
|
+
|
1528
|
+
use Rack::Cache
|
1529
|
+
|
1530
|
+
get '/' do
|
1531
|
+
cache_control :public, :max_age => 36000
|
1532
|
+
sleep 5
|
1533
|
+
"hello"
|
1534
|
+
end
|
1535
|
+
```
|
1536
|
+
|
1537
|
+
Use the `:static_cache_control` setting (see below) to add
|
1538
|
+
`Cache-Control` header info to static files.
|
1539
|
+
|
1540
|
+
According to RFC 2616 your application should behave differently if the If-Match
|
1541
|
+
or If-None-Match header is set to `*` depending on whether the resource
|
1542
|
+
requested is already in existence. Sinatra assumes resources for safe (like get)
|
1543
|
+
and idempotent (like put) requests are already in existence, whereas other
|
1544
|
+
resources (for instance for post requests), are treated as new resources. You
|
1545
|
+
can change this behavior by passing in a `:new_resource` option:
|
1546
|
+
|
1547
|
+
```ruby
|
1548
|
+
get '/create' do
|
1549
|
+
etag '', :new_resource => true
|
1550
|
+
Article.create
|
1551
|
+
erb :new_article
|
1552
|
+
end
|
1553
|
+
```
|
1554
|
+
|
1555
|
+
If you still want to use a weak ETag, pass in a `:kind` option:
|
1556
|
+
|
1557
|
+
```ruby
|
1558
|
+
etag '', :new_resource => true, :kind => :weak
|
1559
|
+
```
|
1560
|
+
|
1561
|
+
### Sending Files
|
1562
|
+
|
1563
|
+
For sending files, you can use the `send_file` helper method:
|
1564
|
+
|
1565
|
+
```ruby
|
1566
|
+
get '/' do
|
1567
|
+
send_file 'foo.png'
|
1568
|
+
end
|
1569
|
+
```
|
1570
|
+
|
1571
|
+
It also takes options:
|
1572
|
+
|
1573
|
+
```ruby
|
1574
|
+
send_file 'foo.png', :type => :jpg
|
1575
|
+
```
|
1576
|
+
|
1577
|
+
The options are:
|
1578
|
+
|
1579
|
+
<dl>
|
1580
|
+
<dt>filename</dt>
|
1581
|
+
<dd>file name, in response, defaults to the real file name.</dd>
|
1582
|
+
|
1583
|
+
<dt>last_modified</dt>
|
1584
|
+
<dd>value for Last-Modified header, defaults to the file's mtime.</dd>
|
1585
|
+
|
1586
|
+
<dt>type</dt>
|
1587
|
+
<dd>content type to use, guessed from the file extension if missing.</dd>
|
1588
|
+
|
1589
|
+
</dt>disposition</dt>
|
1590
|
+
<dd>
|
1591
|
+
used for Content-Disposition, possible value: <tt>nil</tt> (default),
|
1592
|
+
<tt>:attachment</tt> and <tt>:inline</tt>
|
1593
|
+
</dd>
|
1594
|
+
|
1595
|
+
<dt>length</dt>
|
1596
|
+
<dd>Content-Length header, defaults to file size.</dd>
|
1597
|
+
|
1598
|
+
<dt>status</dt>
|
1599
|
+
<dd>
|
1600
|
+
Status code to be send. Useful when sending a static file as an error page.
|
1601
|
+
|
1602
|
+
If supported by the Rack handler, other means than streaming from the Ruby
|
1603
|
+
process will be used. If you use this helper method, Sinatra will automatically
|
1604
|
+
handle range requests.
|
1605
|
+
</dd>
|
1606
|
+
</dl>
|
1607
|
+
|
1608
|
+
### Accessing the Request Object
|
1609
|
+
|
1610
|
+
The incoming request object can be accessed from request level (filter, routes,
|
1611
|
+
error handlers) through the `request` method:
|
1612
|
+
|
1613
|
+
```ruby
|
1614
|
+
# app running on http://example.com/example
|
1615
|
+
get '/foo' do
|
1616
|
+
t = %w[text/css text/html application/javascript]
|
1617
|
+
request.accept # ['text/html', '*/*']
|
1618
|
+
request.accept? 'text/xml' # true
|
1619
|
+
request.preferred_type(t) # 'text/html'
|
1620
|
+
request.body # request body sent by the client (see below)
|
1621
|
+
request.scheme # "http"
|
1622
|
+
request.script_name # "/example"
|
1623
|
+
request.path_info # "/foo"
|
1624
|
+
request.port # 80
|
1625
|
+
request.request_method # "GET"
|
1626
|
+
request.query_string # ""
|
1627
|
+
request.content_length # length of request.body
|
1628
|
+
request.media_type # media type of request.body
|
1629
|
+
request.host # "example.com"
|
1630
|
+
request.get? # true (similar methods for other verbs)
|
1631
|
+
request.form_data? # false
|
1632
|
+
request["some_param"] # value of some_param parameter. [] is a shortcut to the params hash.
|
1633
|
+
request.referrer # the referrer of the client or '/'
|
1634
|
+
request.user_agent # user agent (used by :agent condition)
|
1635
|
+
request.cookies # hash of browser cookies
|
1636
|
+
request.xhr? # is this an ajax request?
|
1637
|
+
request.url # "http://example.com/example/foo"
|
1638
|
+
request.path # "/example/foo"
|
1639
|
+
request.ip # client IP address
|
1640
|
+
request.secure? # false (would be true over ssl)
|
1641
|
+
request.forwarded? # true (if running behind a reverse proxy)
|
1642
|
+
request.env # raw env hash handed in by Rack
|
1643
|
+
end
|
1644
|
+
```
|
1645
|
+
|
1646
|
+
Some options, like `script_name` or `path_info`, can also be
|
1647
|
+
written:
|
1648
|
+
|
1649
|
+
```ruby
|
1650
|
+
before { request.path_info = "/" }
|
1651
|
+
|
1652
|
+
get "/" do
|
1653
|
+
"all requests end up here"
|
1654
|
+
end
|
1655
|
+
```
|
1656
|
+
|
1657
|
+
The `request.body` is an IO or StringIO object:
|
1658
|
+
|
1659
|
+
```ruby
|
1660
|
+
post "/api" do
|
1661
|
+
request.body.rewind # in case someone already read it
|
1662
|
+
data = JSON.parse request.body.read
|
1663
|
+
"Hello #{data['name']}!"
|
1664
|
+
end
|
1665
|
+
```
|
1666
|
+
|
1667
|
+
### Attachments
|
1668
|
+
|
1669
|
+
You can use the `attachment` helper to tell the browser the response should be
|
1670
|
+
stored on disk rather than displayed in the browser:
|
1671
|
+
|
1672
|
+
```ruby
|
1673
|
+
get '/' do
|
1674
|
+
attachment
|
1675
|
+
"store it!"
|
1676
|
+
end
|
1677
|
+
```
|
1678
|
+
|
1679
|
+
You can also pass it a file name:
|
1680
|
+
|
1681
|
+
```ruby
|
1682
|
+
get '/' do
|
1683
|
+
attachment "info.txt"
|
1684
|
+
"store it!"
|
1685
|
+
end
|
1686
|
+
```
|
1687
|
+
|
1688
|
+
### Dealing with Date and Time
|
1689
|
+
|
1690
|
+
Sinatra offers a `time_for` helper method that generates a Time object
|
1691
|
+
from the given value. It is also able to convert `DateTime`, `Date` and
|
1692
|
+
similar classes:
|
1693
|
+
|
1694
|
+
```ruby
|
1695
|
+
get '/' do
|
1696
|
+
pass if Time.now > time_for('Dec 23, 2012')
|
1697
|
+
"still time"
|
1698
|
+
end
|
1699
|
+
```
|
1700
|
+
|
1701
|
+
This method is used internally by `expires`, `last_modified` and akin. You can
|
1702
|
+
therefore easily extend the behavior of those methods by overriding `time_for`
|
1703
|
+
in your application:
|
1704
|
+
|
1705
|
+
```ruby
|
1706
|
+
helpers do
|
1707
|
+
def time_for(value)
|
1708
|
+
case value
|
1709
|
+
when :yesterday then Time.now - 24*60*60
|
1710
|
+
when :tomorrow then Time.now + 24*60*60
|
1711
|
+
else super
|
1712
|
+
end
|
1713
|
+
end
|
1714
|
+
end
|
1715
|
+
|
1716
|
+
get '/' do
|
1717
|
+
last_modified :yesterday
|
1718
|
+
expires :tomorrow
|
1719
|
+
"hello"
|
1720
|
+
end
|
1721
|
+
```
|
1722
|
+
|
1723
|
+
### Looking Up Template Files
|
1724
|
+
|
1725
|
+
The `find_template` helper is used to find template files for rendering:
|
1726
|
+
|
1727
|
+
```ruby
|
1728
|
+
find_template settings.views, 'foo', Tilt[:haml] do |file|
|
1729
|
+
puts "could be #{file}"
|
1730
|
+
end
|
1731
|
+
```
|
1732
|
+
|
1733
|
+
This is not really useful. But it is useful that you can actually override this
|
1734
|
+
method to hook in your own lookup mechanism. For instance, if you want to be
|
1735
|
+
able to use more than one view directory:
|
1736
|
+
|
1737
|
+
```ruby
|
1738
|
+
set :views, ['views', 'templates']
|
1739
|
+
|
1740
|
+
helpers do
|
1741
|
+
def find_template(views, name, engine, &block)
|
1742
|
+
Array(views).each { |v| super(v, name, engine, &block) }
|
1743
|
+
end
|
1744
|
+
end
|
1745
|
+
```
|
1746
|
+
|
1747
|
+
Another example would be using different directories for different engines:
|
1748
|
+
|
1749
|
+
```ruby
|
1750
|
+
set :views, :sass => 'views/sass', :haml => 'templates', :default => 'views'
|
1751
|
+
|
1752
|
+
helpers do
|
1753
|
+
def find_template(views, name, engine, &block)
|
1754
|
+
_, folder = views.detect { |k,v| engine == Tilt[k] }
|
1755
|
+
folder ||= views[:default]
|
1756
|
+
super(folder, name, engine, &block)
|
1757
|
+
end
|
1758
|
+
end
|
1759
|
+
```
|
1760
|
+
|
1761
|
+
You can also easily wrap this up in an extension and share with others!
|
1762
|
+
|
1763
|
+
Note that `find_template` does not check if the file really exists but
|
1764
|
+
rather calls the given block for all possible paths. This is not a performance
|
1765
|
+
issue, since `render` will use `break` as soon as a file is found. Also,
|
1766
|
+
template locations (and content) will be cached if you are not running in
|
1767
|
+
development mode. You should keep that in mind if you write a really crazy
|
1768
|
+
method.
|
1769
|
+
|
1770
|
+
## Configuration
|
1771
|
+
|
1772
|
+
Run once, at startup, in any environment:
|
1773
|
+
|
1774
|
+
```ruby
|
1775
|
+
configure do
|
1776
|
+
# setting one option
|
1777
|
+
set :option, 'value'
|
1778
|
+
|
1779
|
+
# setting multiple options
|
1780
|
+
set :a => 1, :b => 2
|
1781
|
+
|
1782
|
+
# same as `set :option, true`
|
1783
|
+
enable :option
|
1784
|
+
|
1785
|
+
# same as `set :option, false`
|
1786
|
+
disable :option
|
1787
|
+
|
1788
|
+
# you can also have dynamic settings with blocks
|
1789
|
+
set(:css_dir) { File.join(views, 'css') }
|
1790
|
+
end
|
1791
|
+
```
|
1792
|
+
|
1793
|
+
Run only when the environment (`RACK_ENV` environment variable) is set to
|
1794
|
+
`:production`:
|
1795
|
+
|
1796
|
+
```ruby
|
1797
|
+
configure :production do
|
1798
|
+
...
|
1799
|
+
end
|
1800
|
+
```
|
1801
|
+
|
1802
|
+
Run when the environment is set to either `:production` or
|
1803
|
+
`:test`:
|
1804
|
+
|
1805
|
+
```ruby
|
1806
|
+
configure :production, :test do
|
1807
|
+
...
|
1808
|
+
end
|
1809
|
+
```
|
1810
|
+
|
1811
|
+
You can access those options via `settings`:
|
1812
|
+
|
1813
|
+
```ruby
|
1814
|
+
configure do
|
1815
|
+
set :foo, 'bar'
|
1816
|
+
end
|
1817
|
+
|
1818
|
+
get '/' do
|
1819
|
+
settings.foo? # => true
|
1820
|
+
settings.foo # => 'bar'
|
1821
|
+
...
|
1822
|
+
end
|
1823
|
+
```
|
1824
|
+
|
1825
|
+
### Configuring attack protection
|
1826
|
+
|
1827
|
+
Sinatra is using
|
1828
|
+
[Rack::Protection](https://github.com/rkh/rack-protection#readme) to defend
|
1829
|
+
your application against common, opportunistic attacks. You can easily disable
|
1830
|
+
this behavior (which will open up your application to tons of common
|
1831
|
+
vulnerabilities):
|
1832
|
+
|
1833
|
+
```ruby
|
1834
|
+
disable :protection
|
1835
|
+
```
|
1836
|
+
|
1837
|
+
To skip a single defense layer, set `protection` to an options hash:
|
1838
|
+
|
1839
|
+
```ruby
|
1840
|
+
set :protection, :except => :path_traversal
|
1841
|
+
```
|
1842
|
+
You can also hand in an array in order to disable a list of protections:
|
1843
|
+
|
1844
|
+
```ruby
|
1845
|
+
set :protection, :except => [:path_traversal, :session_hijacking]
|
1846
|
+
```
|
1847
|
+
|
1848
|
+
By default, Sinatra will only set up session based protection if `:sessions`
|
1849
|
+
has been enabled. Sometimes you want to set up sessions on your own, though. In
|
1850
|
+
that case you can get it to set up session based protections by passing the `:session` option:
|
1851
|
+
|
1852
|
+
```ruby
|
1853
|
+
use Rack::Session::Pool
|
1854
|
+
set :protection, :session => true
|
1855
|
+
```
|
1856
|
+
|
1857
|
+
### Available Settings
|
1858
|
+
|
1859
|
+
<dl>
|
1860
|
+
<dt>absolute_redirects</dt>
|
1861
|
+
<dd>
|
1862
|
+
If disabled, Sinatra will allow relative redirects, however, Sinatra will no
|
1863
|
+
longer conform with RFC 2616 (HTTP 1.1), which only allows absolute redirects.
|
1864
|
+
</dd>
|
1865
|
+
<dd>
|
1866
|
+
Enable if your app is running behind a reverse proxy that has not been set up
|
1867
|
+
properly. Note that the <tt>url</tt> helper will still produce absolute URLs, unless you
|
1868
|
+
pass in <tt>false</tt> as the second parameter.
|
1869
|
+
</dd>
|
1870
|
+
<dd>Disabled per default.</dd>
|
1871
|
+
|
1872
|
+
<dt>add_charsets</dt>
|
1873
|
+
<dd>
|
1874
|
+
mime types the <tt>content_type</tt> helper will automatically add the charset info to.
|
1875
|
+
You should add to it rather than overriding this option:
|
1876
|
+
<tt>settings.add_charsets << "application/foobar"</tt>
|
1877
|
+
</dd>
|
1878
|
+
|
1879
|
+
<dt>app_file</dt>
|
1880
|
+
<dd>
|
1881
|
+
Path to the main application file, used to detect project root, views and public
|
1882
|
+
folder and inline templates.
|
1883
|
+
</dd>
|
1884
|
+
|
1885
|
+
<dt>bind</dt>
|
1886
|
+
<dd>IP address to bind to (default: 0.0.0.0). Only used for built-in server.</dd>
|
1887
|
+
|
1888
|
+
<dt>default_encoding</dt>
|
1889
|
+
<dd>encoding to assume if unknown (defaults to <tt>"utf-8"</tt>).</dd>
|
1890
|
+
|
1891
|
+
<dt>dump_errors</dt>
|
1892
|
+
<dd>display errors in the log.</dd>
|
1893
|
+
|
1894
|
+
<dt>environment</dt>
|
1895
|
+
<dd>
|
1896
|
+
current environment, defaults to <tt>ENV['RACK_ENV']</tt>, or <tt>"development"</tt> if
|
1897
|
+
not available.
|
1898
|
+
</dd>
|
1899
|
+
|
1900
|
+
<dt>logging</dt>
|
1901
|
+
<dd>use the logger.</dd>
|
1902
|
+
|
1903
|
+
<dt>lock</dt>
|
1904
|
+
<dd>
|
1905
|
+
Places a lock around every request, only running processing on request
|
1906
|
+
per Ruby process concurrently.
|
1907
|
+
</dd>
|
1908
|
+
<dd>Enabled if your app is not thread-safe. Disabled per default.</dd>
|
1909
|
+
|
1910
|
+
<dt>method_override</dt>
|
1911
|
+
<dd>
|
1912
|
+
use <tt>_method</tt> magic to allow put/delete forms in browsers that
|
1913
|
+
don't support it.
|
1914
|
+
</dd>
|
1915
|
+
|
1916
|
+
<dt>port</dt>
|
1917
|
+
<dd>Port to listen on. Only used for built-in server.</dd>
|
1918
|
+
|
1919
|
+
<dt>prefixed_redirects</dt>
|
1920
|
+
<dd>
|
1921
|
+
Whether or not to insert <tt>request.script_name</tt> into redirects if no
|
1922
|
+
absolute path is given. That way <tt>redirect '/foo'</tt> would behave like
|
1923
|
+
<tt>redirect to('/foo')</tt>. Disabled per default.
|
1924
|
+
</dd>
|
1925
|
+
|
1926
|
+
<dt>protection</dt>
|
1927
|
+
<dd>Whether or not to enable web attack protections. See protection section above.</dd>
|
1928
|
+
|
1929
|
+
<dt>public_dir</dt>
|
1930
|
+
<dd>Alias for <tt>public_folder</tt>. See below.</dd>
|
1931
|
+
|
1932
|
+
<dt>public_folder</dt>
|
1933
|
+
<dd>
|
1934
|
+
Path to the folder public files are served from. Only used if static
|
1935
|
+
file serving is enabled (see <tt>static</tt> setting below). Inferred from
|
1936
|
+
<tt>app_file</tt> setting if not set.
|
1937
|
+
</dd>
|
1938
|
+
|
1939
|
+
<dt>reload_templates</dt>
|
1940
|
+
<dd>
|
1941
|
+
Whether or not to reload templates between requests. Enabled in development mode.
|
1942
|
+
</dd>
|
1943
|
+
|
1944
|
+
<dt>root</dt>
|
1945
|
+
<dd>
|
1946
|
+
Path to project root folder. Inferred from <tt>app_file</tt> setting if not set.
|
1947
|
+
</dd>
|
1948
|
+
|
1949
|
+
<dt>raise_errors</dt>
|
1950
|
+
<dd>
|
1951
|
+
raise exceptions (will stop application). Enabled by default when
|
1952
|
+
<tt>environment</tt> is set to <tt>"test"</tt>, disabled otherwise.
|
1953
|
+
</dd>
|
1954
|
+
|
1955
|
+
<dt>run</dt>
|
1956
|
+
<dd>
|
1957
|
+
if enabled, Sinatra will handle starting the web server, do not
|
1958
|
+
enable if using rackup or other means.
|
1959
|
+
</dd>
|
1960
|
+
|
1961
|
+
<dt>running</dt>
|
1962
|
+
<dd>is the built-in server running now? do not change this setting!</dd>
|
1963
|
+
|
1964
|
+
<dt>server</dt>
|
1965
|
+
<dd>
|
1966
|
+
server or list of servers to use for built-in server. defaults to
|
1967
|
+
['thin', 'mongrel', 'webrick'], order indicates priority.
|
1968
|
+
</dd>
|
1969
|
+
|
1970
|
+
<dt>sessions</dt>
|
1971
|
+
<dd>
|
1972
|
+
Enable cookie-based sessions support using <tt>Rack::Session::Cookie</tt>.
|
1973
|
+
See 'Using Sessions' section for more information.
|
1974
|
+
</dd>
|
1975
|
+
|
1976
|
+
<dt>show_exceptions</dt>
|
1977
|
+
<dd>
|
1978
|
+
Show a stack trace in the browser when an exception
|
1979
|
+
happens. Enabled by default when <tt>environment</tt>
|
1980
|
+
is set to <tt>"development"</tt>, disabled otherwise.
|
1981
|
+
</dd>
|
1982
|
+
<dd>
|
1983
|
+
Can also be set to <tt>:after_handler</tt> to trigger
|
1984
|
+
app-specified error handling before showing a stack
|
1985
|
+
trace in the browser.
|
1986
|
+
</dd>
|
1987
|
+
|
1988
|
+
<dt>static</dt>
|
1989
|
+
<dd>Whether Sinatra should handle serving static files.</dd>
|
1990
|
+
<dd>Disable when using a server able to do this on its own.</dd>
|
1991
|
+
<dd>Disabling will boost performance.</dd>
|
1992
|
+
<dd>
|
1993
|
+
Enabled per default in classic style, disabled for
|
1994
|
+
modular apps.
|
1995
|
+
</dd>
|
1996
|
+
|
1997
|
+
<dt>static_cache_control</dt>
|
1998
|
+
<dd>
|
1999
|
+
When Sinatra is serving static files, set this to add
|
2000
|
+
<tt>Cache-Control</tt> headers to the responses. Uses the
|
2001
|
+
<tt>cache_control</tt> helper. Disabled by default.
|
2002
|
+
</dd>
|
2003
|
+
<dd>
|
2004
|
+
Use an explicit array when setting multiple values:
|
2005
|
+
<tt>set :static_cache_control, [:public, :max_age => 300]</tt>
|
2006
|
+
</dd>
|
2007
|
+
|
2008
|
+
<dt>threaded</dt>
|
2009
|
+
<dd>
|
2010
|
+
If set to <tt>true</tt>, will tell Thin to use <tt>EventMachine.defer</tt>
|
2011
|
+
for processing the request.
|
2012
|
+
</dd>
|
2013
|
+
|
2014
|
+
<dt>views</dt>
|
2015
|
+
<dd>
|
2016
|
+
Path to the views folder. Inferred from <tt>app_file</tt> setting if
|
2017
|
+
not set.
|
2018
|
+
</dd>
|
2019
|
+
|
2020
|
+
<dt>x_cascade</dt>
|
2021
|
+
<dd>
|
2022
|
+
Whether or not to set the X-Cascade header if no route matches.
|
2023
|
+
Defaults to `true`.
|
2024
|
+
</dd>
|
2025
|
+
</dl>
|
2026
|
+
|
2027
|
+
## Environments
|
2028
|
+
|
2029
|
+
There are three predefined `environments`: `"development"`,
|
2030
|
+
`"production"` and `"test"`. Environments can be set
|
2031
|
+
through the `RACK_ENV` environment variable. The default value is
|
2032
|
+
`"development"`. In the `"development"` environment all templates are reloaded between
|
2033
|
+
requests, and special `not_found` and `error` handlers
|
2034
|
+
display stack traces in your browser.
|
2035
|
+
In the `"production"` and `"test"` environments, templates are cached by default.
|
2036
|
+
|
2037
|
+
To run different environments use the `-e` option:
|
2038
|
+
|
2039
|
+
```ruby
|
2040
|
+
ruby my_app.rb -e [ENVIRONMENT]
|
2041
|
+
```
|
2042
|
+
|
2043
|
+
You can use predefined methods: `development?`, `test?` and `production?` to
|
2044
|
+
check the current environment setting.
|
2045
|
+
|
2046
|
+
## Error Handling
|
2047
|
+
|
2048
|
+
Error handlers run within the same context as routes and before filters, which
|
2049
|
+
means you get all the goodies it has to offer, like `haml`,
|
2050
|
+
`erb`, `halt`, etc.
|
2051
|
+
|
2052
|
+
### Not Found
|
2053
|
+
|
2054
|
+
When a `Sinatra::NotFound` exception is raised, or the response's status
|
2055
|
+
code is 404, the `not_found` handler is invoked:
|
2056
|
+
|
2057
|
+
```ruby
|
2058
|
+
not_found do
|
2059
|
+
'This is nowhere to be found.'
|
2060
|
+
end
|
2061
|
+
```
|
2062
|
+
|
2063
|
+
### Error
|
2064
|
+
|
2065
|
+
The `error` handler is invoked any time an exception is raised from a route
|
2066
|
+
block or a filter. The exception object can be obtained from the
|
2067
|
+
`sinatra.error` Rack variable:
|
2068
|
+
|
2069
|
+
```ruby
|
2070
|
+
error do
|
2071
|
+
'Sorry there was a nasty error - ' + env['sinatra.error'].name
|
2072
|
+
end
|
2073
|
+
```
|
2074
|
+
|
2075
|
+
Custom errors:
|
2076
|
+
|
2077
|
+
```ruby
|
2078
|
+
error MyCustomError do
|
2079
|
+
'So what happened was...' + env['sinatra.error'].message
|
2080
|
+
end
|
2081
|
+
```
|
2082
|
+
|
2083
|
+
Then, if this happens:
|
2084
|
+
|
2085
|
+
```ruby
|
2086
|
+
get '/' do
|
2087
|
+
raise MyCustomError, 'something bad'
|
2088
|
+
end
|
2089
|
+
```
|
2090
|
+
|
2091
|
+
You get this:
|
2092
|
+
|
2093
|
+
```ruby
|
2094
|
+
So what happened was... something bad
|
2095
|
+
```
|
2096
|
+
|
2097
|
+
Alternatively, you can install an error handler for a status code:
|
2098
|
+
|
2099
|
+
```ruby
|
2100
|
+
error 403 do
|
2101
|
+
'Access forbidden'
|
2102
|
+
end
|
2103
|
+
|
2104
|
+
get '/secret' do
|
2105
|
+
403
|
2106
|
+
end
|
2107
|
+
```
|
2108
|
+
|
2109
|
+
Or a range:
|
2110
|
+
|
2111
|
+
```ruby
|
2112
|
+
error 400..510 do
|
2113
|
+
'Boom'
|
2114
|
+
end
|
2115
|
+
```
|
2116
|
+
|
2117
|
+
Sinatra installs special `not_found` and `error` handlers when
|
2118
|
+
running under the development environment to display nice stack traces
|
2119
|
+
and additional debugging information in your browser.
|
2120
|
+
|
2121
|
+
## Rack Middleware
|
2122
|
+
|
2123
|
+
Sinatra rides on [Rack](http://rack.rubyforge.org/), a minimal standard
|
2124
|
+
interface for Ruby web frameworks. One of Rack's most interesting capabilities
|
2125
|
+
for application developers is support for "middleware" -- components that sit
|
2126
|
+
between the server and your application monitoring and/or manipulating the
|
2127
|
+
HTTP request/response to provide various types of common functionality.
|
2128
|
+
|
2129
|
+
Sinatra makes building Rack middleware pipelines a cinch via a top-level
|
2130
|
+
`use` method:
|
2131
|
+
|
2132
|
+
```ruby
|
2133
|
+
require 'sinatra'
|
2134
|
+
require 'my_custom_middleware'
|
2135
|
+
|
2136
|
+
use Rack::Lint
|
2137
|
+
use MyCustomMiddleware
|
2138
|
+
|
2139
|
+
get '/hello' do
|
2140
|
+
'Hello World'
|
2141
|
+
end
|
2142
|
+
```
|
2143
|
+
|
2144
|
+
The semantics of `use` are identical to those defined for the
|
2145
|
+
[Rack::Builder](http://rack.rubyforge.org/doc/classes/Rack/Builder.html) DSL
|
2146
|
+
(most frequently used from rackup files). For example, the `use` method
|
2147
|
+
accepts multiple/variable args as well as blocks:
|
2148
|
+
|
2149
|
+
```ruby
|
2150
|
+
use Rack::Auth::Basic do |username, password|
|
2151
|
+
username == 'admin' && password == 'secret'
|
2152
|
+
end
|
2153
|
+
```
|
2154
|
+
|
2155
|
+
Rack is distributed with a variety of standard middleware for logging,
|
2156
|
+
debugging, URL routing, authentication, and session handling. Sinatra uses
|
2157
|
+
many of these components automatically based on configuration so you
|
2158
|
+
typically don't have to `use` them explicitly.
|
2159
|
+
|
2160
|
+
You can find useful middleware in
|
2161
|
+
[rack](https://github.com/rack/rack/tree/master/lib/rack),
|
2162
|
+
[rack-contrib](https://github.com/rack/rack-contrib#readm),
|
2163
|
+
with [CodeRack](http://coderack.org/) or in the
|
2164
|
+
[Rack wiki](https://github.com/rack/rack/wiki/List-of-Middleware).
|
2165
|
+
|
2166
|
+
## Testing
|
2167
|
+
|
2168
|
+
Sinatra tests can be written using any Rack-based testing library or framework.
|
2169
|
+
[Rack::Test](http://rdoc.info/github/brynary/rack-test/master/frames)
|
2170
|
+
is recommended:
|
2171
|
+
|
2172
|
+
```ruby
|
2173
|
+
require 'my_sinatra_app'
|
2174
|
+
require 'test/unit'
|
2175
|
+
require 'rack/test'
|
2176
|
+
|
2177
|
+
class MyAppTest < Test::Unit::TestCase
|
2178
|
+
include Rack::Test::Methods
|
2179
|
+
|
2180
|
+
def app
|
2181
|
+
Sinatra::Application
|
2182
|
+
end
|
2183
|
+
|
2184
|
+
def test_my_default
|
2185
|
+
get '/'
|
2186
|
+
assert_equal 'Hello World!', last_response.body
|
2187
|
+
end
|
2188
|
+
|
2189
|
+
def test_with_params
|
2190
|
+
get '/meet', :name => 'Frank'
|
2191
|
+
assert_equal 'Hello Frank!', last_response.body
|
2192
|
+
end
|
2193
|
+
|
2194
|
+
def test_with_rack_env
|
2195
|
+
get '/', {}, 'HTTP_USER_AGENT' => 'Songbird'
|
2196
|
+
assert_equal "You're using Songbird!", last_response.body
|
2197
|
+
end
|
2198
|
+
end
|
2199
|
+
```
|
2200
|
+
|
2201
|
+
Note: If you are using Sinatra in the modular style, replace `Sinatra::Application`
|
2202
|
+
above with the class name of your app.
|
2203
|
+
|
2204
|
+
## Sinatra::Base - Middleware, Libraries, and Modular Apps
|
2205
|
+
|
2206
|
+
Defining your app at the top-level works well for micro-apps but has
|
2207
|
+
considerable drawbacks when building reusable components such as Rack
|
2208
|
+
middleware, Rails metal, simple libraries with a server component, or even
|
2209
|
+
Sinatra extensions. The top-level assumes a micro-app style configuration
|
2210
|
+
(e.g., a single application file, `./public` and `./views`
|
2211
|
+
directories, logging, exception detail page, etc.). That's where
|
2212
|
+
`Sinatra::Base` comes into play:
|
2213
|
+
|
2214
|
+
```ruby
|
2215
|
+
require 'sinatra/base'
|
2216
|
+
|
2217
|
+
class MyApp < Sinatra::Base
|
2218
|
+
set :sessions, true
|
2219
|
+
set :foo, 'bar'
|
2220
|
+
|
2221
|
+
get '/' do
|
2222
|
+
'Hello world!'
|
2223
|
+
end
|
2224
|
+
end
|
2225
|
+
```
|
2226
|
+
|
2227
|
+
The methods available to `Sinatra::Base` subclasses are exactly the same as those
|
2228
|
+
available via the top-level DSL. Most top-level apps can be converted to
|
2229
|
+
`Sinatra::Base` components with two modifications:
|
2230
|
+
|
2231
|
+
* Your file should require `sinatra/base` instead of `sinatra`;
|
2232
|
+
otherwise, all of Sinatra's DSL methods are imported into the main
|
2233
|
+
namespace.
|
2234
|
+
* Put your app's routes, error handlers, filters, and options in a subclass
|
2235
|
+
of `Sinatra::Base`.
|
2236
|
+
|
2237
|
+
`Sinatra::Base` is a blank slate. Most options are disabled by default,
|
2238
|
+
including the built-in server. See
|
2239
|
+
[Options and Configuration](http://sinatra.github.com/configuration.html)
|
2240
|
+
for details on available options and their behavior.
|
2241
|
+
|
2242
|
+
### Modular vs. Classic Style
|
2243
|
+
|
2244
|
+
Contrary to common belief, there is nothing wrong with the classic style. If it
|
2245
|
+
suits your application, you do not have to switch to a modular application.
|
2246
|
+
|
2247
|
+
The main disadvantage of using the classic style rather than the modular style is that
|
2248
|
+
you will only have one Sinatra application per Ruby process. If you plan to use
|
2249
|
+
more than one, switch to the modular style. There is no reason you cannot mix
|
2250
|
+
the modular and the classic styles.
|
2251
|
+
|
2252
|
+
If switching from one style to the other, you should be aware of slightly
|
2253
|
+
different default settings:
|
2254
|
+
|
2255
|
+
```
|
2256
|
+
Setting Classic Modular
|
2257
|
+
|
2258
|
+
app_file file loading sinatra file subclassing Sinatra::Base
|
2259
|
+
run $0 == app_file false
|
2260
|
+
logging true false
|
2261
|
+
method_override true false
|
2262
|
+
inline_templates true false
|
2263
|
+
static true false
|
2264
|
+
```
|
2265
|
+
|
2266
|
+
### Serving a Modular Application
|
2267
|
+
|
2268
|
+
There are two common options for starting a modular app, actively starting with
|
2269
|
+
`run!`:
|
2270
|
+
|
2271
|
+
```ruby
|
2272
|
+
# my_app.rb
|
2273
|
+
require 'sinatra/base'
|
2274
|
+
|
2275
|
+
class MyApp < Sinatra::Base
|
2276
|
+
# ... app code here ...
|
2277
|
+
|
2278
|
+
# start the server if ruby file executed directly
|
2279
|
+
run! if app_file == $0
|
2280
|
+
end
|
2281
|
+
```
|
2282
|
+
|
2283
|
+
Start with:
|
2284
|
+
|
2285
|
+
```ruby
|
2286
|
+
ruby my_app.rb
|
2287
|
+
```
|
2288
|
+
|
2289
|
+
Or with a `config.ru` file, which allows using any Rack handler:
|
2290
|
+
|
2291
|
+
```ruby
|
2292
|
+
# config.ru (run with rackup)
|
2293
|
+
require './my_app'
|
2294
|
+
run MyApp
|
2295
|
+
```
|
2296
|
+
|
2297
|
+
Run:
|
2298
|
+
|
2299
|
+
```ruby
|
2300
|
+
rackup -p 4567
|
2301
|
+
```
|
2302
|
+
|
2303
|
+
### Using a Classic Style Application with a config.ru
|
2304
|
+
|
2305
|
+
Write your app file:
|
2306
|
+
|
2307
|
+
```ruby
|
2308
|
+
# app.rb
|
2309
|
+
require 'sinatra'
|
2310
|
+
|
2311
|
+
get '/' do
|
2312
|
+
'Hello world!'
|
2313
|
+
end
|
2314
|
+
```
|
2315
|
+
|
2316
|
+
And a corresponding `config.ru`:
|
2317
|
+
|
2318
|
+
```ruby
|
2319
|
+
require './app'
|
2320
|
+
run Sinatra::Application
|
2321
|
+
```
|
2322
|
+
|
2323
|
+
### When to use a config.ru?
|
2324
|
+
|
2325
|
+
A `config.ru` file is recommended if:
|
2326
|
+
|
2327
|
+
* You want to deploy with a different Rack handler (Passenger, Unicorn,
|
2328
|
+
Heroku, ...).
|
2329
|
+
* You want to use more than one subclass of `Sinatra::Base`.
|
2330
|
+
* You want to use Sinatra only for middleware, and not as an endpoint.
|
2331
|
+
|
2332
|
+
**There is no need to switch to a `config.ru` simply because you
|
2333
|
+
switched to the modular style, and you don't have to use the modular style for running
|
2334
|
+
with a `config.ru`.**
|
2335
|
+
|
2336
|
+
### Using Sinatra as Middleware
|
2337
|
+
|
2338
|
+
Not only is Sinatra able to use other Rack middleware, any Sinatra application
|
2339
|
+
can in turn be added in front of any Rack endpoint as middleware itself. This
|
2340
|
+
endpoint could be another Sinatra application, or any other Rack-based
|
2341
|
+
application (Rails/Ramaze/Camping/...):
|
2342
|
+
|
2343
|
+
```ruby
|
2344
|
+
require 'sinatra/base'
|
2345
|
+
|
2346
|
+
class LoginScreen < Sinatra::Base
|
2347
|
+
enable :sessions
|
2348
|
+
|
2349
|
+
get('/login') { haml :login }
|
2350
|
+
|
2351
|
+
post('/login') do
|
2352
|
+
if params[:name] == 'admin' && params[:password] == 'admin'
|
2353
|
+
session['user_name'] = params[:name]
|
2354
|
+
else
|
2355
|
+
redirect '/login'
|
2356
|
+
end
|
2357
|
+
end
|
2358
|
+
end
|
2359
|
+
|
2360
|
+
class MyApp < Sinatra::Base
|
2361
|
+
# middleware will run before filters
|
2362
|
+
use LoginScreen
|
2363
|
+
|
2364
|
+
before do
|
2365
|
+
unless session['user_name']
|
2366
|
+
halt "Access denied, please <a href='/login'>login</a>."
|
2367
|
+
end
|
2368
|
+
end
|
2369
|
+
|
2370
|
+
get('/') { "Hello #{session['user_name']}." }
|
2371
|
+
end
|
2372
|
+
```
|
2373
|
+
|
2374
|
+
### Dynamic Application Creation
|
2375
|
+
|
2376
|
+
Sometimes you want to create new applications at runtime without having to
|
2377
|
+
assign them to a constant, you can do this with `Sinatra.new`:
|
2378
|
+
|
2379
|
+
```ruby
|
2380
|
+
require 'sinatra/base'
|
2381
|
+
my_app = Sinatra.new { get('/') { "hi" } }
|
2382
|
+
my_app.run!
|
2383
|
+
```
|
2384
|
+
|
2385
|
+
It takes the application to inherit from as an optional argument:
|
2386
|
+
|
2387
|
+
```ruby
|
2388
|
+
# config.ru (run with rackup)
|
2389
|
+
require 'sinatra/base'
|
2390
|
+
|
2391
|
+
controller = Sinatra.new do
|
2392
|
+
enable :logging
|
2393
|
+
helpers MyHelpers
|
2394
|
+
end
|
2395
|
+
|
2396
|
+
map('/a') do
|
2397
|
+
run Sinatra.new(controller) { get('/') { 'a' } }
|
2398
|
+
end
|
2399
|
+
|
2400
|
+
map('/b') do
|
2401
|
+
run Sinatra.new(controller) { get('/') { 'b' } }
|
2402
|
+
end
|
2403
|
+
```
|
2404
|
+
|
2405
|
+
This is especially useful for testing Sinatra extensions or using Sinatra in
|
2406
|
+
your own library.
|
2407
|
+
|
2408
|
+
This also makes using Sinatra as middleware extremely easy:
|
2409
|
+
|
2410
|
+
```ruby
|
2411
|
+
require 'sinatra/base'
|
2412
|
+
|
2413
|
+
use Sinatra do
|
2414
|
+
get('/') { ... }
|
2415
|
+
end
|
2416
|
+
|
2417
|
+
run RailsProject::Application
|
2418
|
+
```
|
2419
|
+
|
2420
|
+
## Scopes and Binding
|
2421
|
+
|
2422
|
+
The scope you are currently in determines what methods and variables are
|
2423
|
+
available.
|
2424
|
+
|
2425
|
+
### Application/Class Scope
|
2426
|
+
|
2427
|
+
Every Sinatra application corresponds to a subclass of `Sinatra::Base`.
|
2428
|
+
If you are using the top-level DSL (`require 'sinatra'`), then this
|
2429
|
+
class is `Sinatra::Application`, otherwise it is the subclass you
|
2430
|
+
created explicitly. At class level you have methods like `get` or `before`, but
|
2431
|
+
you cannot access the `request` or `session` objects, as there is only a
|
2432
|
+
single application class for all requests.
|
2433
|
+
|
2434
|
+
Options created via `set` are methods at class level:
|
2435
|
+
|
2436
|
+
```ruby
|
2437
|
+
class MyApp < Sinatra::Base
|
2438
|
+
# Hey, I'm in the application scope!
|
2439
|
+
set :foo, 42
|
2440
|
+
foo # => 42
|
2441
|
+
|
2442
|
+
get '/foo' do
|
2443
|
+
# Hey, I'm no longer in the application scope!
|
2444
|
+
end
|
2445
|
+
end
|
2446
|
+
```
|
2447
|
+
|
2448
|
+
You have the application scope binding inside:
|
2449
|
+
|
2450
|
+
* Your application class body
|
2451
|
+
* Methods defined by extensions
|
2452
|
+
* The block passed to `helpers`
|
2453
|
+
* Procs/blocks used as value for `set`
|
2454
|
+
* The block passed to `Sinatra.new`
|
2455
|
+
|
2456
|
+
You can reach the scope object (the class) like this:
|
2457
|
+
|
2458
|
+
* Via the object passed to configure blocks (`configure { |c| ... }`)
|
2459
|
+
* `settings` from within the request scope
|
2460
|
+
|
2461
|
+
### Request/Instance Scope
|
2462
|
+
|
2463
|
+
For every incoming request, a new instance of your application class is
|
2464
|
+
created and all handler blocks run in that scope. From within this scope you
|
2465
|
+
can access the `request` and `session` objects or call rendering methods like
|
2466
|
+
`erb` or `haml`. You can access the application scope from within the request
|
2467
|
+
scope via the `settings` helper:
|
2468
|
+
|
2469
|
+
```ruby
|
2470
|
+
class MyApp < Sinatra::Base
|
2471
|
+
# Hey, I'm in the application scope!
|
2472
|
+
get '/define_route/:name' do
|
2473
|
+
# Request scope for '/define_route/:name'
|
2474
|
+
@value = 42
|
2475
|
+
|
2476
|
+
settings.get("/#{params[:name]}") do
|
2477
|
+
# Request scope for "/#{params[:name]}"
|
2478
|
+
@value # => nil (not the same request)
|
2479
|
+
end
|
2480
|
+
|
2481
|
+
"Route defined!"
|
2482
|
+
end
|
2483
|
+
end
|
2484
|
+
```
|
2485
|
+
|
2486
|
+
You have the request scope binding inside:
|
2487
|
+
|
2488
|
+
* get/head/post/put/delete/options blocks
|
2489
|
+
* before/after filters
|
2490
|
+
* helper methods
|
2491
|
+
* templates/views
|
2492
|
+
|
2493
|
+
### Delegation Scope
|
2494
|
+
|
2495
|
+
The delegation scope just forwards methods to the class scope. However, it
|
2496
|
+
does not behave exactly like the class scope, as you do not have the class
|
2497
|
+
binding. Only methods explicitly marked for delegation are available, and you
|
2498
|
+
do not share variables/state with the class scope (read: you have a different
|
2499
|
+
`self`). You can explicitly add method delegations by calling
|
2500
|
+
`Sinatra::Delegator.delegate :method_name`.
|
2501
|
+
|
2502
|
+
You have the delegate scope binding inside:
|
2503
|
+
|
2504
|
+
* The top level binding, if you did `require "sinatra"`
|
2505
|
+
* An object extended with the `Sinatra::Delegator` mixin
|
2506
|
+
|
2507
|
+
Have a look at the code for yourself: here's the
|
2508
|
+
[Sinatra::Delegator mixin](https://github.com/sinatra/sinatra/blob/ca06364/lib/sinatra/base.rb#L1609-1633)
|
2509
|
+
being [extending the main object](https://github.com/sinatra/sinatra/blob/ca06364/lib/sinatra/main.rb#L28-30).
|
2510
|
+
|
2511
|
+
## Command Line
|
2512
|
+
|
2513
|
+
Sinatra applications can be run directly:
|
2514
|
+
|
2515
|
+
```ruby
|
2516
|
+
ruby myapp.rb [-h] [-x] [-e ENVIRONMENT] [-p PORT] [-o HOST] [-s HANDLER]
|
2517
|
+
```
|
2518
|
+
|
2519
|
+
Options are:
|
2520
|
+
|
2521
|
+
```
|
2522
|
+
-h # help
|
2523
|
+
-p # set the port (default is 4567)
|
2524
|
+
-o # set the host (default is 0.0.0.0)
|
2525
|
+
-e # set the environment (default is development)
|
2526
|
+
-s # specify rack server/handler (default is thin)
|
2527
|
+
-x # turn on the mutex lock (default is off)
|
2528
|
+
```
|
2529
|
+
|
2530
|
+
## Requirement
|
2531
|
+
|
2532
|
+
The following Ruby versions are officially supported:
|
2533
|
+
<dl>
|
2534
|
+
<dt>Ruby 1.8.7</dt>
|
2535
|
+
<dd>
|
2536
|
+
1.8.7 is fully supported, however, if nothing is keeping you from it, we
|
2537
|
+
recommend upgrading to 1.9.2 or switching to JRuby or Rubinius. Support for
|
2538
|
+
1.8.7 will not be dropped before Sinatra 2.0 and Ruby 2.0 except maybe in
|
2539
|
+
the unlikely event of 1.8.8 being released. Even then, we might continue
|
2540
|
+
supporting it. <b>Ruby 1.8.6 is no longer supported.</b> If you want to run
|
2541
|
+
with 1.8.6, downgrade to Sinatra 1.2, which will receive bug fixes until
|
2542
|
+
Sinatra 1.4.0 is released.
|
2543
|
+
</dd>
|
2544
|
+
|
2545
|
+
<dt>Ruby 1.9.2</dt>
|
2546
|
+
<dd>
|
2547
|
+
1.9.2 is fully supported and recommended. Do not use 1.9.2p0, as it is known to
|
2548
|
+
cause segmentation faults when running Sinatra. Support will continue at least
|
2549
|
+
until the release of Ruby 1.9.4/2.0 and support for the latest 1.9 release
|
2550
|
+
will continue as long as it is still supported by the Ruby core team.
|
2551
|
+
</dd>
|
2552
|
+
|
2553
|
+
<dt>Ruby 1.9.3</dt>
|
2554
|
+
<dd>
|
2555
|
+
1.9.3 is fully supported and recommended. Please note that switching to 1.9.3
|
2556
|
+
from an earlier version will invalidate all sessions.
|
2557
|
+
</dd>
|
2558
|
+
|
2559
|
+
<dt>Rubinius</dt>
|
2560
|
+
<dd>
|
2561
|
+
Rubinius is officially supported (Rubinius >= 1.2.4), everything works, including
|
2562
|
+
all template languages. The upcoming 2.0 release is supported as
|
2563
|
+
well, including 1.9 mode.
|
2564
|
+
</dd>
|
2565
|
+
|
2566
|
+
<dt>JRuby</dt>
|
2567
|
+
<dd>
|
2568
|
+
JRuby is officially supported (JRuby >= 1.6.7). No issues with third party
|
2569
|
+
template libraries are known, however, if you choose to use JRuby, please
|
2570
|
+
look into JRuby rack handlers, as the Thin web server is not fully supported
|
2571
|
+
on JRuby. JRuby's support for C extensions is still experimental, which only
|
2572
|
+
affects RDiscount, Redcarpet, RedCloth and Yajl templates as well as Thin
|
2573
|
+
and Mongrel at the moment.
|
2574
|
+
</dd>
|
2575
|
+
</dl>
|
2576
|
+
We also keep an eye on upcoming Ruby versions.
|
2577
|
+
|
2578
|
+
The following Ruby implementations are not officially supported but still are
|
2579
|
+
known to run Sinatra:
|
2580
|
+
|
2581
|
+
* Older versions of JRuby and Rubinius
|
2582
|
+
* Ruby Enterprise Edition
|
2583
|
+
* MacRuby, Maglev, IronRuby
|
2584
|
+
* Ruby 1.9.0 and 1.9.1 (but we do recommend against using those)
|
2585
|
+
|
2586
|
+
Not being officially supported means if things only break there and not on a
|
2587
|
+
supported platform, we assume it's not our issue but theirs.
|
2588
|
+
|
2589
|
+
We also run our CI against ruby-head (the upcoming 2.0.0) and the 1.9.4
|
2590
|
+
branch, but we can't guarantee anything, since it is constantly moving. Expect
|
2591
|
+
both 1.9.4p0 and 2.0.0p0 to be supported.
|
2592
|
+
|
2593
|
+
Sinatra should work on any operating system supported by the chosen Ruby
|
2594
|
+
implementation.
|
2595
|
+
|
2596
|
+
Sinatra currently doesn't run on Cardinal, SmallRuby, BlueRuby or any
|
2597
|
+
Ruby version prior to 1.8.7.
|
2598
|
+
|
2599
|
+
## The Bleeding Edge
|
2600
|
+
|
2601
|
+
If you would like to use Sinatra's latest bleeding-edge code, feel free to run your
|
2602
|
+
application against the master branch, it should be rather stable.
|
2603
|
+
|
2604
|
+
We also push out prerelease gems from time to time, so you can do a
|
2605
|
+
|
2606
|
+
```ruby
|
2607
|
+
gem install sinatra --pre
|
2608
|
+
```
|
2609
|
+
|
2610
|
+
To get some of the latest features.
|
2611
|
+
|
2612
|
+
### With Bundler
|
2613
|
+
|
2614
|
+
If you want to run your application with the latest Sinatra, using
|
2615
|
+
[Bundler](http://gembundler.com/) is the recommended way.
|
2616
|
+
|
2617
|
+
First, install bundler, if you haven't:
|
2618
|
+
|
2619
|
+
```ruby
|
2620
|
+
gem install bundler
|
2621
|
+
```
|
2622
|
+
|
2623
|
+
Then, in your project directory, create a `Gemfile`:
|
2624
|
+
|
2625
|
+
```ruby
|
2626
|
+
source :rubygems
|
2627
|
+
gem 'sinatra', :git => "git://github.com/sinatra/sinatra.git"
|
2628
|
+
|
2629
|
+
# other dependencies
|
2630
|
+
gem 'haml' # for instance, if you use haml
|
2631
|
+
gem 'activerecord', '~> 3.0' # maybe you also need ActiveRecord 3.x
|
2632
|
+
```
|
2633
|
+
|
2634
|
+
Note that you will have to list all your application's dependencies in the `Gemfile`.
|
2635
|
+
Sinatra's direct dependencies (Rack and Tilt) will, however, be automatically
|
2636
|
+
fetched and added by Bundler.
|
2637
|
+
|
2638
|
+
Now you can run your app like this:
|
2639
|
+
|
2640
|
+
```ruby
|
2641
|
+
bundle exec ruby myapp.rb
|
2642
|
+
```
|
2643
|
+
|
2644
|
+
### Roll Your Own
|
2645
|
+
|
2646
|
+
Create a local clone and run your app with the `sinatra/lib` directory
|
2647
|
+
on the `$LOAD_PATH`:
|
2648
|
+
|
2649
|
+
```ruby
|
2650
|
+
cd myapp
|
2651
|
+
git clone git://github.com/sinatra/sinatra.git
|
2652
|
+
ruby -I sinatra/lib myapp.rb
|
2653
|
+
```
|
2654
|
+
|
2655
|
+
To update the Sinatra sources in the future:
|
2656
|
+
|
2657
|
+
```ruby
|
2658
|
+
cd myapp/sinatra
|
2659
|
+
git pull
|
2660
|
+
```
|
2661
|
+
### Install Globally
|
2662
|
+
|
2663
|
+
You can build the gem on your own:
|
2664
|
+
|
2665
|
+
```ruby
|
2666
|
+
git clone git://github.com/sinatra/sinatra.git
|
2667
|
+
cd sinatra
|
2668
|
+
rake sinatra.gemspec
|
2669
|
+
rake install
|
2670
|
+
```
|
2671
|
+
|
2672
|
+
If you install gems as root, the last step should be
|
2673
|
+
|
2674
|
+
```ruby
|
2675
|
+
sudo rake install
|
2676
|
+
```
|
2677
|
+
|
2678
|
+
## Versioning
|
2679
|
+
|
2680
|
+
Sinatra follows [Semantic Versioning](http://semver.org/), both SemVer and
|
2681
|
+
SemVerTag.
|
2682
|
+
|
2683
|
+
## Further Reading
|
2684
|
+
|
2685
|
+
* [Project Website](http://www.sinatrarb.com/) - Additional documentation,
|
2686
|
+
news, and links to other resources.
|
2687
|
+
* [Contributing](http://www.sinatrarb.com/contributing) - Find a bug? Need
|
2688
|
+
help? Have a patch?
|
2689
|
+
* [Issue tracker](http://github.com/sinatra/sinatra/issues)
|
2690
|
+
* [Twitter](http://twitter.com/sinatra)
|
2691
|
+
* [Mailing List](http://groups.google.com/group/sinatrarb/topics)
|
2692
|
+
* IRC: [#sinatra](irc://chat.freenode.net/#sinatra) on http://freenode.net
|
2693
|
+
* [Sinatra Book](http://sinatra-book.gittr.com) Cookbook Tutorial
|
2694
|
+
* [Sinatra Recipes](http://recipes.sinatrarb.com/) Community
|
2695
|
+
contributed recipes
|
2696
|
+
* API documentation for the [latest release](http://rubydoc.info/gems/sinatra)
|
2697
|
+
or the [current HEAD](http://rubydoc.info/github/sinatra/sinatra) on
|
2698
|
+
http://rubydoc.info
|
2699
|
+
* [CI server](http://travis-ci.org/sinatra/sinatra)
|