sinatra 1.2.0.a → 1.2.0.c
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of sinatra might be problematic. Click here for more details.
- data/AUTHORS +2 -0
- data/CHANGES +69 -1
- data/Gemfile +35 -0
- data/Gemfile.lock +74 -0
- data/README.de.rdoc +16 -16
- data/README.es.rdoc +360 -80
- data/README.fr.rdoc +16 -16
- data/README.hu.rdoc +4 -4
- data/README.jp.rdoc +16 -16
- data/README.pt-br.rdoc +6 -6
- data/README.rdoc +629 -95
- data/README.ru.rdoc +317 -103
- data/README.zh.rdoc +16 -16
- data/Rakefile +6 -14
- data/lib/sinatra/base.rb +70 -30
- data/lib/sinatra/showexceptions.rb +25 -0
- data/sinatra.gemspec +8 -18
- data/test/builder_test.rb +6 -0
- data/test/coffee_test.rb +1 -1
- data/test/erubis_test.rb +6 -0
- data/test/haml_test.rb +2 -1
- data/test/helpers_test.rb +53 -0
- data/test/less_test.rb +6 -0
- data/test/liquid_test.rb +2 -1
- data/test/markaby_test.rb +2 -1
- data/test/markdown_test.rb +3 -2
- data/test/nokogiri_test.rb +2 -1
- data/test/radius_test.rb +3 -2
- data/test/rdoc_test.rb +2 -1
- data/test/routing_test.rb +49 -5
- data/test/sass_test.rb +1 -1
- data/test/scss_test.rb +1 -1
- data/test/slim_test.rb +2 -1
- data/test/templates_test.rb +43 -0
- data/test/textile_test.rb +2 -1
- data/test/views/a/in_a.str +1 -0
- data/test/views/b/in_b.str +1 -0
- data/test/views/calc.html.erb +1 -0
- metadata +22 -223
data/README.fr.rdoc
CHANGED
@@ -171,7 +171,7 @@ au lieu de les considérer comme un chemin vers un fichier.
|
|
171
171
|
|
172
172
|
Le gem haml est nécessaire pour utiliser la fonction de rendu Haml:
|
173
173
|
|
174
|
-
|
174
|
+
# Chargez la bibliothèque haml dans votre application
|
175
175
|
require 'haml'
|
176
176
|
|
177
177
|
get '/' do
|
@@ -194,7 +194,7 @@ et supportent aussi la réécriture (surcharge) comme dans cet exemple.
|
|
194
194
|
|
195
195
|
=== Templates Erb
|
196
196
|
|
197
|
-
|
197
|
+
# Chargez la bibliothèque erb dans votre application
|
198
198
|
require 'erb'
|
199
199
|
|
200
200
|
get '/' do
|
@@ -207,7 +207,7 @@ Utilisera le template: <tt>./views/index.erb</tt>
|
|
207
207
|
|
208
208
|
Le gem erubis est nécessaire pour utiliser la fonction de rendu erubis:
|
209
209
|
|
210
|
-
|
210
|
+
# Chargez la bibliothèque erubis dans votre application
|
211
211
|
require 'erubis'
|
212
212
|
|
213
213
|
get '/' do
|
@@ -220,7 +220,7 @@ Utilisera le template: <tt>./views/index.erubis</tt>
|
|
220
220
|
|
221
221
|
Le gem builder est nécessaire pour utiliser la fonction de rendu builder:
|
222
222
|
|
223
|
-
|
223
|
+
# Chargez la bibliothèque builder dans votre application
|
224
224
|
require 'builder'
|
225
225
|
|
226
226
|
get '/' do
|
@@ -233,7 +233,7 @@ Utilisera le template: <tt>./views/index.builder</tt>.
|
|
233
233
|
|
234
234
|
Le gem nokogiri est nécessaire pour utiliser la fonction de rendu nokogiri:
|
235
235
|
|
236
|
-
|
236
|
+
# Chargez la bibliothèque nokogiri dans votre application
|
237
237
|
require 'nokogiri'
|
238
238
|
|
239
239
|
get '/' do
|
@@ -246,7 +246,7 @@ Utilisera le template: <tt>./views/index.nokogiri</tt>.
|
|
246
246
|
|
247
247
|
Le gem haml est nécessaire pour utiliser la fonction de rendu Sass:
|
248
248
|
|
249
|
-
|
249
|
+
# Chargez la bibliothèque haml ou sass dans votre application
|
250
250
|
require 'sass'
|
251
251
|
|
252
252
|
get '/stylesheet.css' do
|
@@ -270,7 +270,7 @@ et supportent aussi la réécriture (surcharge) comme dans cet exemple.
|
|
270
270
|
|
271
271
|
Le gem haml est nécessaire pour utiliser la fonction de rendu Scss:
|
272
272
|
|
273
|
-
|
273
|
+
# Chargez la bibliothèque haml ou sass dans votre application
|
274
274
|
require 'sass'
|
275
275
|
|
276
276
|
get '/stylesheet.css' do
|
@@ -294,7 +294,7 @@ et supportent aussi la réécriture (surcharge) comme dans cet exemple.
|
|
294
294
|
|
295
295
|
Le gem less est nécessaire pour utiliser la fonction de rendu Less:
|
296
296
|
|
297
|
-
|
297
|
+
# Chargez la bibliothèque less dans votre application
|
298
298
|
require 'less'
|
299
299
|
|
300
300
|
get '/stylesheet.css' do
|
@@ -307,7 +307,7 @@ Utilisera le template: <tt>./views/stylesheet.less</tt>.
|
|
307
307
|
|
308
308
|
Le gem liquid est nécessaire pour utiliser la fonction de rendu Liquid:
|
309
309
|
|
310
|
-
|
310
|
+
# Chargez la bibliothèque liquid dans votre application
|
311
311
|
require 'liquid'
|
312
312
|
|
313
313
|
get '/' do
|
@@ -325,7 +325,7 @@ template Liquid, il sera toujours nécessaire de lui passer des variables locale
|
|
325
325
|
|
326
326
|
Le gem rdiscount est nécessaire pour utiliser la fonction de rendu Markdown:
|
327
327
|
|
328
|
-
|
328
|
+
# Chargez la bibliothèque rdiscount dans votre application
|
329
329
|
require "rdiscount"
|
330
330
|
|
331
331
|
get '/' do
|
@@ -350,7 +350,7 @@ Notez que vous pouvez également appeler la méthode markdown au sein d'autres t
|
|
350
350
|
|
351
351
|
Le gem RedCloth est nécessaire pour utiliser la fonction de rendu Textile:
|
352
352
|
|
353
|
-
|
353
|
+
# Chargez la bibliothèqye redcloth dans votre application
|
354
354
|
require "redcloth"
|
355
355
|
|
356
356
|
get '/' do
|
@@ -374,7 +374,7 @@ Notez que vous pouvez également appeler la méthode textile au sein d'autres te
|
|
374
374
|
|
375
375
|
Le gem RDoc est nécessaire pour utiliser la fonction de rendu RDoc:
|
376
376
|
|
377
|
-
|
377
|
+
# Chargez la bibliothèque rdoc dans votre application
|
378
378
|
require "rdoc"
|
379
379
|
|
380
380
|
get '/' do
|
@@ -398,7 +398,7 @@ Notez que vous pouvez également appeler la méthode rdoc au sein d'autres templ
|
|
398
398
|
|
399
399
|
Le gem radius est nécessaire pour utiliser la fonction de rendu Radius:
|
400
400
|
|
401
|
-
|
401
|
+
# Chargez la bibliotèque radius dans votre application
|
402
402
|
require 'radius'
|
403
403
|
|
404
404
|
get '/' do
|
@@ -416,7 +416,7 @@ template Radius, il sera toujours nécessaire de lui passer des variables locale
|
|
416
416
|
|
417
417
|
Le gem markaby est nécessaire pour utiliser la fonction de rendu Markaby:
|
418
418
|
|
419
|
-
|
419
|
+
# Chargez la bibliothèque markaby dans votre application
|
420
420
|
require 'markaby'
|
421
421
|
|
422
422
|
get '/' do
|
@@ -429,7 +429,7 @@ Utilisera <tt>./views/index.mab</tt>.
|
|
429
429
|
|
430
430
|
Le gem slim est nécessaire pour utiliser la fonction de rendu Slim:
|
431
431
|
|
432
|
-
|
432
|
+
# Chargez la bibliothèque slim dans votre application
|
433
433
|
require 'slim'
|
434
434
|
|
435
435
|
get '/' do
|
@@ -443,7 +443,7 @@ Utilisera <tt>./views/index.slim</tt>.
|
|
443
443
|
Le gem coffee-script et l'exécutable `coffee` sont nécessaires pour utiliser la
|
444
444
|
fonction de rendu CoffeeScript:
|
445
445
|
|
446
|
-
|
446
|
+
# Chargez la bibliothèque coffee-script dans votre application
|
447
447
|
require 'coffee-script'
|
448
448
|
|
449
449
|
get '/application.js' do
|
data/README.hu.rdoc
CHANGED
@@ -123,7 +123,7 @@ metódusok minden, nekik közvetlenül átadott karakterláncot megjelenítenek.
|
|
123
123
|
|
124
124
|
HAML sablonok rendereléséhez szükségünk lesz a haml gem-re vagy könyvtárra:
|
125
125
|
|
126
|
-
|
126
|
+
# Importáljuk be a haml-t az alkalmazásba
|
127
127
|
require 'haml'
|
128
128
|
|
129
129
|
get '/' do
|
@@ -146,7 +146,7 @@ A globális beállításokat lehetőségünk van felülírni metódus szinten is
|
|
146
146
|
|
147
147
|
=== Erb sablonok
|
148
148
|
|
149
|
-
|
149
|
+
# Importáljuk be az erb-t az alkalmazásba
|
150
150
|
require 'erb'
|
151
151
|
|
152
152
|
get '/' do
|
@@ -160,7 +160,7 @@ Ez a <tt>./views/index.erb</tt> sablont fogja lerenderelni.
|
|
160
160
|
Szükségünk lesz a builder gem-re vagy könyvtárra a builder sablonok
|
161
161
|
rendereléséhez:
|
162
162
|
|
163
|
-
|
163
|
+
# Importáljuk be a builder-t az alkalmazásba
|
164
164
|
require 'builder'
|
165
165
|
|
166
166
|
get '/' do
|
@@ -173,7 +173,7 @@ Ez pedig a <tt>./views/index.builder</tt> állományt fogja renderelni.
|
|
173
173
|
|
174
174
|
Sass sablonok használatához szükség lesz a haml gem-re vagy könyvtárra:
|
175
175
|
|
176
|
-
|
176
|
+
# Be kell importálni a haml, vagy a sass könyvtárat
|
177
177
|
require 'sass'
|
178
178
|
|
179
179
|
get '/stylesheet.css' do
|
data/README.jp.rdoc
CHANGED
@@ -169,7 +169,7 @@ Rackレスポンス、Rackボディオブジェクト、HTTPステータスコ
|
|
169
169
|
|
170
170
|
hamlを使うにはhamlライブラリが必要です:
|
171
171
|
|
172
|
-
|
172
|
+
# hamlを読み込みます
|
173
173
|
require 'haml'
|
174
174
|
|
175
175
|
get '/' do
|
@@ -192,7 +192,7 @@ hamlを使うにはhamlライブラリが必要です:
|
|
192
192
|
|
193
193
|
=== Erb テンプレート
|
194
194
|
|
195
|
-
|
195
|
+
# erbを読み込みます
|
196
196
|
require 'erb'
|
197
197
|
|
198
198
|
get '/' do
|
@@ -205,7 +205,7 @@ hamlを使うにはhamlライブラリが必要です:
|
|
205
205
|
|
206
206
|
erubisテンプレートを表示するには、erubisライブラリが必要です:
|
207
207
|
|
208
|
-
|
208
|
+
# erubisを読み込みます
|
209
209
|
require 'erubis'
|
210
210
|
|
211
211
|
get '/' do
|
@@ -218,7 +218,7 @@ erubisテンプレートを表示するには、erubisライブラリが必要
|
|
218
218
|
|
219
219
|
builderを使うにはbuilderライブラリが必要です:
|
220
220
|
|
221
|
-
|
221
|
+
# builderを読み込みます
|
222
222
|
require 'builder'
|
223
223
|
|
224
224
|
get '/' do
|
@@ -231,7 +231,7 @@ builderを使うにはbuilderライブラリが必要です:
|
|
231
231
|
|
232
232
|
鋸を使うには鋸ライブラリが必要です:
|
233
233
|
|
234
|
-
|
234
|
+
# 鋸を読み込みます
|
235
235
|
require 'nokogiri'
|
236
236
|
|
237
237
|
get '/' do
|
@@ -244,7 +244,7 @@ builderを使うにはbuilderライブラリが必要です:
|
|
244
244
|
|
245
245
|
Sassテンプレートを使うにはsassライブラリが必要です:
|
246
246
|
|
247
|
-
|
247
|
+
# hamlかsassを読み込みます
|
248
248
|
require 'sass'
|
249
249
|
|
250
250
|
get '/stylesheet.css' do
|
@@ -268,7 +268,7 @@ see {Options and Configurations}[http://www.sinatrarb.com/configuration.html],
|
|
268
268
|
|
269
269
|
Scssテンプレートを使うにはsassライブラリが必要です:
|
270
270
|
|
271
|
-
|
271
|
+
# hamlかsassを読み込みます
|
272
272
|
require 'sass'
|
273
273
|
|
274
274
|
get '/stylesheet.css' do
|
@@ -292,7 +292,7 @@ see {Options and Configurations}[http://www.sinatrarb.com/configuration.html],
|
|
292
292
|
|
293
293
|
Lessテンプレートを使うにはlessライブラリが必要です:
|
294
294
|
|
295
|
-
|
295
|
+
# lessを読み込みます
|
296
296
|
require 'less'
|
297
297
|
|
298
298
|
get '/stylesheet.css' do
|
@@ -305,7 +305,7 @@ Lessテンプレートを使うにはlessライブラリが必要です:
|
|
305
305
|
|
306
306
|
Liquidテンプレートを使うにはliquidライブラリが必要です:
|
307
307
|
|
308
|
-
|
308
|
+
# liquidを読み込みます
|
309
309
|
require 'liquid'
|
310
310
|
|
311
311
|
get '/' do
|
@@ -323,7 +323,7 @@ LiquidテンプレートからRubyのメソッド(+yield+を除く)を呼び出
|
|
323
323
|
|
324
324
|
Markdownテンプレートを使うにはrdiscountライブラリが必要です:
|
325
325
|
|
326
|
-
|
326
|
+
# rdiscountを読み込みます
|
327
327
|
require "rdiscount"
|
328
328
|
|
329
329
|
get '/' do
|
@@ -346,7 +346,7 @@ markdownからメソッドを呼び出すことも、localsに変数を渡すこ
|
|
346
346
|
|
347
347
|
Textileテンプレートを使うにはRedClothライブラリが必要です:
|
348
348
|
|
349
|
-
|
349
|
+
# redclothを読み込みます
|
350
350
|
require "redcloth"
|
351
351
|
|
352
352
|
get '/' do
|
@@ -369,7 +369,7 @@ textileからメソッドを呼び出すことも、localsに変数を渡すこ
|
|
369
369
|
|
370
370
|
RDocテンプレートを使うにはRDocライブラリが必要です:
|
371
371
|
|
372
|
-
|
372
|
+
# rdocを読み込みます
|
373
373
|
require "rdoc"
|
374
374
|
|
375
375
|
get '/' do
|
@@ -392,7 +392,7 @@ rdocからメソッドを呼び出すことも、localsに変数を渡すこと
|
|
392
392
|
|
393
393
|
Radiusテンプレートを使うにはradiusライブラリが必要です:
|
394
394
|
|
395
|
-
|
395
|
+
# radiusを読み込みます
|
396
396
|
require 'radius'
|
397
397
|
|
398
398
|
get '/' do
|
@@ -410,7 +410,7 @@ RadiusテンプレートからRubyのメソッド(+yield+を除く)を呼び出
|
|
410
410
|
|
411
411
|
Markabyテンプレートを使うにはmarkabyライブラリが必要です:
|
412
412
|
|
413
|
-
|
413
|
+
# markabyを読み込みます
|
414
414
|
require 'markaby'
|
415
415
|
|
416
416
|
get '/' do
|
@@ -423,7 +423,7 @@ Markabyテンプレートを使うにはmarkabyライブラリが必要です:
|
|
423
423
|
|
424
424
|
Slimテンプレートを使うにはslimライブラリが必要です:
|
425
425
|
|
426
|
-
|
426
|
+
# slimを読み込みます
|
427
427
|
require 'slim'
|
428
428
|
|
429
429
|
get '/' do
|
@@ -436,7 +436,7 @@ Slimテンプレートを使うにはslimライブラリが必要です:
|
|
436
436
|
|
437
437
|
CoffeeScriptテンプレートを表示するにはcoffee-scriptライブラリと`coffee`バイナリが必要です:
|
438
438
|
|
439
|
-
|
439
|
+
# coffee-scriptを読み込みます
|
440
440
|
require 'coffee-script'
|
441
441
|
|
442
442
|
get '/application.js' do
|
data/README.pt-br.rdoc
CHANGED
@@ -119,7 +119,7 @@ qualquer string passada diretamente para elas.
|
|
119
119
|
|
120
120
|
A gem/biblioteca haml é necessária para renderizar templates HAML:
|
121
121
|
|
122
|
-
|
122
|
+
# Você precisa do 'require haml' em sua aplicação.
|
123
123
|
require 'haml'
|
124
124
|
|
125
125
|
get '/' do
|
@@ -142,7 +142,7 @@ e substitua em uma requisição individual.
|
|
142
142
|
|
143
143
|
=== Erb Templates
|
144
144
|
|
145
|
-
|
145
|
+
# Você precisa do 'require erb' em sua aplicação
|
146
146
|
require 'erb'
|
147
147
|
|
148
148
|
get '/' do
|
@@ -155,7 +155,7 @@ Renderiza <tt>./views/index.erb</tt>
|
|
155
155
|
|
156
156
|
A gem/biblioteca erubis é necessária para renderizar templates erubis:
|
157
157
|
|
158
|
-
|
158
|
+
# Você precisa do 'require erubis' em sua aplicação.
|
159
159
|
require 'erubis'
|
160
160
|
|
161
161
|
get '/' do
|
@@ -168,7 +168,7 @@ Renderiza <tt>./views/index.erubis</tt>
|
|
168
168
|
|
169
169
|
A gem/biblioteca builder é necessária para renderizar templates builder:
|
170
170
|
|
171
|
-
|
171
|
+
# Você precisa do 'require builder' em sua aplicação.
|
172
172
|
require 'builder'
|
173
173
|
|
174
174
|
get '/' do
|
@@ -182,7 +182,7 @@ Renderiza <tt>./views/index.builder</tt>.
|
|
182
182
|
|
183
183
|
A gem/biblioteca sass é necessária para renderizar templates sass:
|
184
184
|
|
185
|
-
|
185
|
+
# Você precisa do 'require haml' ou 'require sass' em sua aplicação.
|
186
186
|
require 'sass'
|
187
187
|
|
188
188
|
get '/stylesheet.css' do
|
@@ -208,7 +208,7 @@ e substitua em uma requisição individual.
|
|
208
208
|
|
209
209
|
A gem/biblioteca less é necessária para renderizar templates Less:
|
210
210
|
|
211
|
-
|
211
|
+
# Você precisa do 'require less' em sua aplicação.
|
212
212
|
require 'less'
|
213
213
|
|
214
214
|
get '/stylesheet.css' do
|
data/README.rdoc
CHANGED
@@ -123,7 +123,7 @@ You can easily define your own conditions:
|
|
123
123
|
"Sorry, you lost."
|
124
124
|
end
|
125
125
|
|
126
|
-
=== Return
|
126
|
+
=== Return Values
|
127
127
|
|
128
128
|
The return value of a route block determines at least the response body passed
|
129
129
|
on to the HTTP client, or at least the next middleware in the Rack stack.
|
@@ -174,9 +174,9 @@ directly.
|
|
174
174
|
|
175
175
|
=== Haml Templates
|
176
176
|
|
177
|
-
The haml gem/library is required to render HAML templates:
|
177
|
+
The <tt>haml</tt> gem/library is required to render HAML templates:
|
178
178
|
|
179
|
-
|
179
|
+
# You'll need to require haml in your app
|
180
180
|
require 'haml'
|
181
181
|
|
182
182
|
get '/' do
|
@@ -199,33 +199,44 @@ and overridden on an individual basis.
|
|
199
199
|
|
200
200
|
=== Erb Templates
|
201
201
|
|
202
|
-
|
202
|
+
# You'll need to require erb in your app
|
203
203
|
require 'erb'
|
204
204
|
|
205
205
|
get '/' do
|
206
206
|
erb :index
|
207
207
|
end
|
208
208
|
|
209
|
-
Renders <tt>./views/index.erb</tt
|
209
|
+
Renders <tt>./views/index.erb</tt>.
|
210
210
|
|
211
|
-
=== Erubis
|
211
|
+
=== Erubis Templates
|
212
212
|
|
213
|
-
The erubis gem/library is required to render
|
213
|
+
The <tt>erubis</tt> gem/library is required to render Erubis templates:
|
214
214
|
|
215
|
-
|
215
|
+
# You'll need to require erubis in your app
|
216
216
|
require 'erubis'
|
217
217
|
|
218
218
|
get '/' do
|
219
219
|
erubis :index
|
220
220
|
end
|
221
221
|
|
222
|
-
Renders <tt>./views/index.erubis</tt
|
222
|
+
Renders <tt>./views/index.erubis</tt>.
|
223
|
+
|
224
|
+
It is also possible to replace Erb with Erubis:
|
225
|
+
|
226
|
+
require 'erubis'
|
227
|
+
Tilt.register :erb, Tilt[:erubis]
|
228
|
+
|
229
|
+
get '/' do
|
230
|
+
erb :index
|
231
|
+
end
|
232
|
+
|
233
|
+
Renders <tt>./views/index.erb</tt> with Erubis.
|
223
234
|
|
224
235
|
=== Builder Templates
|
225
236
|
|
226
|
-
The builder gem/library is required to render builder templates:
|
237
|
+
The <tt>builder</tt> gem/library is required to render builder templates:
|
227
238
|
|
228
|
-
|
239
|
+
# You'll need to require builder in your app
|
229
240
|
require 'builder'
|
230
241
|
|
231
242
|
get '/' do
|
@@ -236,9 +247,9 @@ Renders <tt>./views/index.builder</tt>.
|
|
236
247
|
|
237
248
|
=== Nokogiri Templates
|
238
249
|
|
239
|
-
The nokogiri gem/library is required to render nokogiri templates:
|
250
|
+
The <tt>nokogiri</tt> gem/library is required to render nokogiri templates:
|
240
251
|
|
241
|
-
|
252
|
+
# You'll need to require nokogiri in your app
|
242
253
|
require 'nokogiri'
|
243
254
|
|
244
255
|
get '/' do
|
@@ -249,9 +260,9 @@ Renders <tt>./views/index.nokogiri</tt>.
|
|
249
260
|
|
250
261
|
=== Sass Templates
|
251
262
|
|
252
|
-
The haml gem/library is required to render Sass templates:
|
263
|
+
The <tt>haml</tt> or <tt>sass</tt> gem/library is required to render Sass templates:
|
253
264
|
|
254
|
-
|
265
|
+
# You'll need to require haml or sass in your app
|
255
266
|
require 'sass'
|
256
267
|
|
257
268
|
get '/stylesheet.css' do
|
@@ -273,9 +284,9 @@ and overridden on an individual basis.
|
|
273
284
|
|
274
285
|
=== Scss Templates
|
275
286
|
|
276
|
-
The haml gem/library is required to render Scss templates:
|
287
|
+
The <tt>haml</tt> or <tt>sass</tt> gem/library is required to render Scss templates:
|
277
288
|
|
278
|
-
|
289
|
+
# You'll need to require haml or sass in your app
|
279
290
|
require 'sass'
|
280
291
|
|
281
292
|
get '/stylesheet.css' do
|
@@ -297,9 +308,9 @@ and overridden on an individual basis.
|
|
297
308
|
|
298
309
|
=== Less Templates
|
299
310
|
|
300
|
-
The less gem/library is required to render Less templates:
|
311
|
+
The <tt>less</tt> gem/library is required to render Less templates:
|
301
312
|
|
302
|
-
|
313
|
+
# You'll need to require less in your app
|
303
314
|
require 'less'
|
304
315
|
|
305
316
|
get '/stylesheet.css' do
|
@@ -310,9 +321,9 @@ Renders <tt>./views/stylesheet.less</tt>.
|
|
310
321
|
|
311
322
|
=== Liquid Templates
|
312
323
|
|
313
|
-
The liquid gem/library is required to render Liquid templates:
|
324
|
+
The <tt>liquid</tt> gem/library is required to render Liquid templates:
|
314
325
|
|
315
|
-
|
326
|
+
# You'll need to require liquid in your app
|
316
327
|
require 'liquid'
|
317
328
|
|
318
329
|
get '/' do
|
@@ -328,9 +339,9 @@ template, you almost always want to pass locals to it:
|
|
328
339
|
|
329
340
|
=== Markdown Templates
|
330
341
|
|
331
|
-
The rdiscount gem/library is required to render Markdown templates:
|
342
|
+
The <tt>rdiscount</tt> gem/library is required to render Markdown templates:
|
332
343
|
|
333
|
-
|
344
|
+
# You'll need to require rdiscount in your app
|
334
345
|
require "rdiscount"
|
335
346
|
|
336
347
|
get '/' do
|
@@ -340,20 +351,58 @@ The rdiscount gem/library is required to render Markdown templates:
|
|
340
351
|
Renders <tt>./views/index.markdown</tt> (+md+ and +mkd+ are also valid file
|
341
352
|
extensions).
|
342
353
|
|
343
|
-
It is not possible to call methods from markdown, nor to pass locals to it.
|
354
|
+
It is not possible to call methods from markdown, nor to pass locals to it.
|
355
|
+
You therefore will usually use it in combination with another rendering
|
356
|
+
engine:
|
344
357
|
|
345
358
|
erb :overview, :locals => { :text => markdown(:introduction) }
|
346
359
|
|
347
|
-
Note that you may also call the markdown method from within other templates:
|
360
|
+
Note that you may also call the +markdown+ method from within other templates:
|
348
361
|
|
349
362
|
%h1 Hello From Haml!
|
350
363
|
%p= markdown(:greetings)
|
351
364
|
|
365
|
+
Since you cannot call Ruby from Markdown, you cannot use layouts written in
|
366
|
+
Markdown. However, it is possible to use another rendering engine for the
|
367
|
+
template than for the layout by passing the <tt>:layout_engine</tt> option:
|
368
|
+
|
369
|
+
get '/' do
|
370
|
+
markdown :index, :layout_engine => :erb
|
371
|
+
end
|
372
|
+
|
373
|
+
This will render <tt>./views/index.md</tt> with <tt>./views/layout.erb</tt> as
|
374
|
+
layout.
|
375
|
+
|
376
|
+
Remember that you can set such rendering options globally:
|
377
|
+
|
378
|
+
set :markdown, :layout_engine => :haml, :layout => :post
|
379
|
+
|
380
|
+
get '/' do
|
381
|
+
markdown :index
|
382
|
+
end
|
383
|
+
|
384
|
+
This will render <tt>./views/index.md</tt> (and any other Markdown template)
|
385
|
+
with <tt>./views/post.haml</tt> as layout.
|
386
|
+
|
387
|
+
It is also possible to parse Markdown with BlueCloth rather than RDiscount:
|
388
|
+
|
389
|
+
require 'bluecloth'
|
390
|
+
|
391
|
+
Tilt.register 'markdown', BlueClothTemplate
|
392
|
+
Tilt.register 'mkd', BlueClothTemplate
|
393
|
+
Tilt.register 'md', BlueClothTemplate
|
394
|
+
|
395
|
+
get '/' do
|
396
|
+
markdown :index
|
397
|
+
end
|
398
|
+
|
399
|
+
Renders <tt>./views/index.md</tt> with BlueCloth.
|
400
|
+
|
352
401
|
=== Textile Templates
|
353
402
|
|
354
|
-
The RedCloth gem/library is required to render Textile templates:
|
403
|
+
The <tt>RedCloth</tt> gem/library is required to render Textile templates:
|
355
404
|
|
356
|
-
|
405
|
+
# You'll need to require redcloth in your app
|
357
406
|
require "redcloth"
|
358
407
|
|
359
408
|
get '/' do
|
@@ -362,20 +411,43 @@ The RedCloth gem/library is required to render Textile templates:
|
|
362
411
|
|
363
412
|
Renders <tt>./views/index.textile</tt>.
|
364
413
|
|
365
|
-
It is not possible to call methods from textile, nor to pass locals to it. You
|
414
|
+
It is not possible to call methods from textile, nor to pass locals to it. You
|
415
|
+
therefore will usually use it in combination with another rendering engine:
|
366
416
|
|
367
417
|
erb :overview, :locals => { :text => textile(:introduction) }
|
368
418
|
|
369
|
-
Note that you may also call the textile method from within other templates:
|
419
|
+
Note that you may also call the +textile+ method from within other templates:
|
370
420
|
|
371
421
|
%h1 Hello From Haml!
|
372
422
|
%p= textile(:greetings)
|
373
423
|
|
424
|
+
Since you cannot call Ruby from Textile, you cannot use layouts written in
|
425
|
+
Textile. However, it is possible to use another rendering engine for the
|
426
|
+
template than for the layout by passing the <tt>:layout_engine</tt> option:
|
427
|
+
|
428
|
+
get '/' do
|
429
|
+
textile :index, :layout_engine => :erb
|
430
|
+
end
|
431
|
+
|
432
|
+
This will render <tt>./views/index.textile</tt> with
|
433
|
+
<tt>./views/layout.erb</tt> as layout.
|
434
|
+
|
435
|
+
Remember that you can set such rendering options globally:
|
436
|
+
|
437
|
+
set :textile, :layout_engine => :haml, :layout => :post
|
438
|
+
|
439
|
+
get '/' do
|
440
|
+
textile :index
|
441
|
+
end
|
442
|
+
|
443
|
+
This will render <tt>./views/index.textile</tt> (and any other Textile
|
444
|
+
template) with <tt>./views/post.haml</tt> as layout.
|
445
|
+
|
374
446
|
=== RDoc Templates
|
375
447
|
|
376
|
-
The
|
448
|
+
The <tt>rdoc</tt> gem/library is required to render RDoc templates:
|
377
449
|
|
378
|
-
|
450
|
+
# You'll need to require rdoc in your app
|
379
451
|
require "rdoc"
|
380
452
|
|
381
453
|
get '/' do
|
@@ -384,20 +456,43 @@ The RDoc gem/library is required to render RDoc templates:
|
|
384
456
|
|
385
457
|
Renders <tt>./views/index.rdoc</tt>.
|
386
458
|
|
387
|
-
It is not possible to call methods from rdoc, nor to pass locals to it. You
|
459
|
+
It is not possible to call methods from rdoc, nor to pass locals to it. You
|
460
|
+
therefore will usually use it in combination with another rendering engine:
|
388
461
|
|
389
462
|
erb :overview, :locals => { :text => rdoc(:introduction) }
|
390
463
|
|
391
|
-
Note that you may also call the rdoc method from within other templates:
|
464
|
+
Note that you may also call the +rdoc+ method from within other templates:
|
392
465
|
|
393
466
|
%h1 Hello From Haml!
|
394
467
|
%p= rdoc(:greetings)
|
395
468
|
|
469
|
+
Since you cannot call Ruby from RDoc, you cannot use layouts written in
|
470
|
+
RDoc. However, it is possible to use another rendering engine for the
|
471
|
+
template than for the layout by passing the <tt>:layout_engine</tt> option:
|
472
|
+
|
473
|
+
get '/' do
|
474
|
+
rdoc :index, :layout_engine => :erb
|
475
|
+
end
|
476
|
+
|
477
|
+
This will render <tt>./views/index.rdoc</tt> with <tt>./views/layout.erb</tt> as
|
478
|
+
layout.
|
479
|
+
|
480
|
+
Remember that you can set such rendering options globally:
|
481
|
+
|
482
|
+
set :rdoc, :layout_engine => :haml, :layout => :post
|
483
|
+
|
484
|
+
get '/' do
|
485
|
+
rdoc :index
|
486
|
+
end
|
487
|
+
|
488
|
+
This will render <tt>./views/index.rdoc</tt> (and any other RDoc template)
|
489
|
+
with <tt>./views/post.haml</tt> as layout.
|
490
|
+
|
396
491
|
=== Radius Templates
|
397
492
|
|
398
|
-
The radius gem/library is required to render Radius templates:
|
493
|
+
The <tt>radius</tt> gem/library is required to render Radius templates:
|
399
494
|
|
400
|
-
|
495
|
+
# You'll need to require radius in your app
|
401
496
|
require 'radius'
|
402
497
|
|
403
498
|
get '/' do
|
@@ -413,9 +508,9 @@ template, you almost always want to pass locals to it:
|
|
413
508
|
|
414
509
|
=== Markaby Templates
|
415
510
|
|
416
|
-
The markaby gem/library is required to render Markaby templates:
|
511
|
+
The <tt>markaby</tt> gem/library is required to render Markaby templates:
|
417
512
|
|
418
|
-
|
513
|
+
# You'll need to require markaby in your app
|
419
514
|
require 'markaby'
|
420
515
|
|
421
516
|
get '/' do
|
@@ -424,7 +519,7 @@ The markaby gem/library is required to render Markaby templates:
|
|
424
519
|
|
425
520
|
Renders <tt>./views/index.mab</tt>.
|
426
521
|
|
427
|
-
|
522
|
+
You may also use inline Markaby:
|
428
523
|
|
429
524
|
get '/' do
|
430
525
|
markaby { h1 "Welcome!" }
|
@@ -432,9 +527,9 @@ If you have Tilt 1.2 or later, you may also use inline markaby:
|
|
432
527
|
|
433
528
|
=== Slim Templates
|
434
529
|
|
435
|
-
The slim gem/library is required to render Slim templates:
|
530
|
+
The <tt>slim</tt> gem/library is required to render Slim templates:
|
436
531
|
|
437
|
-
|
532
|
+
# You'll need to require slim in your app
|
438
533
|
require 'slim'
|
439
534
|
|
440
535
|
get '/' do
|
@@ -445,10 +540,18 @@ Renders <tt>./views/index.slim</tt>.
|
|
445
540
|
|
446
541
|
=== CoffeeScript Templates
|
447
542
|
|
448
|
-
The coffee-script gem/library and
|
449
|
-
|
543
|
+
The <tt>coffee-script</tt> gem/library and at least <b>one</b> of the
|
544
|
+
following options to execute JavaScript:
|
545
|
+
|
546
|
+
* +node+ (from Node.js) in your path
|
547
|
+
* you must be running on OSX
|
548
|
+
* +therubyracer+ gem/library
|
549
|
+
|
550
|
+
See http://github.com/josh/ruby-coffee-script for an updated list of options.
|
551
|
+
|
552
|
+
Now you can render CoffeeScript templates:
|
450
553
|
|
451
|
-
|
554
|
+
# You'll need to require coffee-script in your app
|
452
555
|
require 'coffee-script'
|
453
556
|
|
454
557
|
get '/application.js' do
|
@@ -457,13 +560,13 @@ CoffeeScript templates:
|
|
457
560
|
|
458
561
|
Renders <tt>./views/application.coffee</tt>.
|
459
562
|
|
460
|
-
===
|
563
|
+
=== Embedded Templates
|
461
564
|
|
462
565
|
get '/' do
|
463
566
|
haml '%div.title Hello World'
|
464
567
|
end
|
465
568
|
|
466
|
-
Renders the
|
569
|
+
Renders the embedded template string.
|
467
570
|
|
468
571
|
=== Accessing Variables in Templates
|
469
572
|
|
@@ -525,27 +628,38 @@ Templates may also be defined using the top-level <tt>template</tt> method:
|
|
525
628
|
end
|
526
629
|
|
527
630
|
If a template named "layout" exists, it will be used each time a template
|
528
|
-
is rendered. You can disable layouts by passing <tt>:layout => false</tt
|
631
|
+
is rendered. You can individually disable layouts by passing <tt>:layout => false</tt>
|
632
|
+
or disable them by default via <tt>set :haml, :layout => false</tt>.
|
529
633
|
|
530
634
|
get '/' do
|
531
635
|
haml :index, :layout => !request.xhr?
|
532
636
|
end
|
533
637
|
|
534
|
-
|
638
|
+
=== Associating File Extensions
|
535
639
|
|
536
|
-
|
537
|
-
|
640
|
+
To associate a file extension with a template engine, use
|
641
|
+
<tt>Tilt.register</tt>. For instance, if you like to use the file extension
|
642
|
+
+tt+ for Textile templates, you can do the following:
|
643
|
+
|
644
|
+
Tilt.register :tt, Tilt[:textile]
|
645
|
+
|
646
|
+
=== Adding You Own Template Engine
|
647
|
+
|
648
|
+
First, register your engine with Tilt, then create a rendering method:
|
649
|
+
|
650
|
+
Tilt.register :myat, MyAwesomeTemplateEngine
|
538
651
|
|
539
652
|
helpers do
|
540
|
-
def
|
541
|
-
"#{name}bar"
|
542
|
-
end
|
653
|
+
def myat(*args) render(:myat, *args) end
|
543
654
|
end
|
544
655
|
|
545
|
-
get '
|
546
|
-
|
656
|
+
get '/' do
|
657
|
+
myat :index
|
547
658
|
end
|
548
659
|
|
660
|
+
Renders <tt>./views/index.myat</tt>. See https://github.com/rtomayko/tilt to
|
661
|
+
learn more about Tilt.
|
662
|
+
|
549
663
|
== Filters
|
550
664
|
|
551
665
|
Before filters are evaluated before each request within the same context as
|
@@ -570,6 +684,10 @@ and routes are accessible by after filters:
|
|
570
684
|
puts response.status
|
571
685
|
end
|
572
686
|
|
687
|
+
Note: Unless you use the +body+ method rather than just returning a String from
|
688
|
+
the routes, the body will not yet be available in the after filter, since it is
|
689
|
+
generated later on.
|
690
|
+
|
573
691
|
Filters optionally taking a pattern, causing them to be evaluated only if the
|
574
692
|
request path matches that pattern:
|
575
693
|
|
@@ -591,7 +709,22 @@ Like routes, filters also take conditions:
|
|
591
709
|
# ...
|
592
710
|
end
|
593
711
|
|
594
|
-
==
|
712
|
+
== Helpers
|
713
|
+
|
714
|
+
Use the top-level <tt>helpers</tt> method to define helper methods for use in
|
715
|
+
route handlers and templates:
|
716
|
+
|
717
|
+
helpers do
|
718
|
+
def bar(name)
|
719
|
+
"#{name}bar"
|
720
|
+
end
|
721
|
+
end
|
722
|
+
|
723
|
+
get '/:name' do
|
724
|
+
bar(params[:name])
|
725
|
+
end
|
726
|
+
|
727
|
+
=== Halting
|
595
728
|
|
596
729
|
To immediately stop a request within a filter or route use:
|
597
730
|
|
@@ -613,7 +746,7 @@ With headers:
|
|
613
746
|
|
614
747
|
halt 402, {'Content-Type' => 'text/plain'}, 'revenge'
|
615
748
|
|
616
|
-
|
749
|
+
=== Passing
|
617
750
|
|
618
751
|
A route can punt processing to the next matching route using <tt>pass</tt>:
|
619
752
|
|
@@ -629,9 +762,148 @@ A route can punt processing to the next matching route using <tt>pass</tt>:
|
|
629
762
|
The route block is immediately exited and control continues with the next
|
630
763
|
matching route. If no matching route is found, a 404 is returned.
|
631
764
|
|
632
|
-
|
765
|
+
=== Triggering Another Route
|
766
|
+
|
767
|
+
Sometimes +pass+ is not what you want, instead you would like to get the result
|
768
|
+
of calling another route. Simply use +call+ to achieve this:
|
769
|
+
|
770
|
+
get '/foo' do
|
771
|
+
status, headers, body = call request.env.merge("PATH_INFO" => '/bar')
|
772
|
+
[status, body.upcase]
|
773
|
+
end
|
774
|
+
|
775
|
+
get '/bar' do
|
776
|
+
"bar"
|
777
|
+
end
|
778
|
+
|
779
|
+
Note that in the example above, you would ease testing and increase performance
|
780
|
+
by simply moving <tt>"bar"</tt> into a helper used by both <tt>/foo</tt> and
|
781
|
+
<tt>/bar</tt>.
|
633
782
|
|
634
|
-
|
783
|
+
If you want the request to be sent to the same application instance rather than
|
784
|
+
a duplicate, use <tt>call!</tt> instead of <tt>call</tt>.
|
785
|
+
|
786
|
+
Check out the Rack specification if you want to learn more about <tt>call</tt>.
|
787
|
+
|
788
|
+
=== Setting Body and Status Code
|
789
|
+
|
790
|
+
It is possible and recommended to set the status code and response body with the
|
791
|
+
return value of the route block. However, in some scenarios you might want to
|
792
|
+
set the body at an arbritary point in the execution flow. You can do so with the
|
793
|
+
+body+ helper method. If you do so, you can use that method from there on to
|
794
|
+
access the body:
|
795
|
+
|
796
|
+
get '/foo' do
|
797
|
+
body "bar"
|
798
|
+
end
|
799
|
+
|
800
|
+
after do
|
801
|
+
puts body
|
802
|
+
end
|
803
|
+
|
804
|
+
It is also possible to pass a block to body, that will be executed by the rack
|
805
|
+
handler (this can be used to implement streaming, see
|
806
|
+
"Return Values").
|
807
|
+
|
808
|
+
Similar to the body, you can also set the status code:
|
809
|
+
|
810
|
+
get '/foo' do
|
811
|
+
status 418
|
812
|
+
halt "I'm a tea pot!"
|
813
|
+
end
|
814
|
+
|
815
|
+
=== Generating URLs
|
816
|
+
|
817
|
+
For generating URLs you should use the +url+ helper method, for instance, in
|
818
|
+
Haml:
|
819
|
+
|
820
|
+
%a{:href => url('/foo')} foo
|
821
|
+
|
822
|
+
It takes reverse proxies and Rack routers into account, if present.
|
823
|
+
|
824
|
+
This method is also aliased to +to+ (see below) for an
|
825
|
+
example).
|
826
|
+
|
827
|
+
=== Browser Redirect
|
828
|
+
|
829
|
+
You can trigger a browser redirect with the +redirect+ helper method:
|
830
|
+
|
831
|
+
get '/foo' do
|
832
|
+
redirect to('/bar')
|
833
|
+
end
|
834
|
+
|
835
|
+
Any additional parameters are handled like arguments passed to +halt+:
|
836
|
+
|
837
|
+
redirect to('/bar'), 303
|
838
|
+
redirect 'http://google.com', 'wrong place, buddy'
|
839
|
+
|
840
|
+
You can also easily redirect back to the page the user came from with
|
841
|
+
<tt>redirect back</tt>:
|
842
|
+
|
843
|
+
get '/foo' do
|
844
|
+
"<a href='/bar'>do something</a>"
|
845
|
+
end
|
846
|
+
|
847
|
+
get '/bar' do
|
848
|
+
do_something
|
849
|
+
redirect back
|
850
|
+
end
|
851
|
+
|
852
|
+
To pass arguments with a redirect, either add them to the query:
|
853
|
+
|
854
|
+
redirect to('/bar?sum=42')
|
855
|
+
|
856
|
+
Or use a session:
|
857
|
+
|
858
|
+
enable :session
|
859
|
+
|
860
|
+
get '/foo' do
|
861
|
+
session[:secret] = 'foo'
|
862
|
+
redirect to('/bar')
|
863
|
+
end
|
864
|
+
|
865
|
+
get '/bar' do
|
866
|
+
session[:secret]
|
867
|
+
end
|
868
|
+
|
869
|
+
=== Sending Files
|
870
|
+
|
871
|
+
For sending files, you can use the <tt>send_file</tt> helper method:
|
872
|
+
|
873
|
+
get '/' do
|
874
|
+
send_file 'foo.png'
|
875
|
+
end
|
876
|
+
|
877
|
+
It also take a couple of options:
|
878
|
+
|
879
|
+
send_file 'foo.png', :type => :jpg
|
880
|
+
|
881
|
+
The options are:
|
882
|
+
|
883
|
+
[filename]
|
884
|
+
file name in response, defaults to the real file name.
|
885
|
+
|
886
|
+
[last_modified]
|
887
|
+
value for Last-Modified header, defaults to the file's mtime.
|
888
|
+
|
889
|
+
[type]
|
890
|
+
content type to use, guessed from the file extension if missing.
|
891
|
+
|
892
|
+
[disposition]
|
893
|
+
used for Content-Disposition, possible values: +nil+ (default),
|
894
|
+
<tt>:attachement</tt> and <tt>:inline</tt>
|
895
|
+
|
896
|
+
[length]
|
897
|
+
Content-Length header, defaults to file size.
|
898
|
+
|
899
|
+
If supported by the Rack handler, other means than streaming from the Ruby
|
900
|
+
process will be used. If you use this helper method, Sinatra will automatically
|
901
|
+
handle range requests.
|
902
|
+
|
903
|
+
=== Accessing the Request Object
|
904
|
+
|
905
|
+
The incoming request object can be accessed from request level (filter, routes,
|
906
|
+
error handlers) through the <tt>request</tt> method:
|
635
907
|
|
636
908
|
# app running on http://example.com/example
|
637
909
|
get '/foo' do
|
@@ -676,12 +948,66 @@ The <tt>request.body</tt> is an IO or StringIO object:
|
|
676
948
|
"Hello #{data['name']}!"
|
677
949
|
end
|
678
950
|
|
951
|
+
=== Looking Up Template Files
|
952
|
+
|
953
|
+
The <tt>find_template</tt> helper is used to find template files for rendering:
|
954
|
+
|
955
|
+
find_template settings.views, 'foo', Tilt[:haml] do |file|
|
956
|
+
puts "could be #{file}"
|
957
|
+
end
|
958
|
+
|
959
|
+
This is not really useful. But it is useful that you can actually override this
|
960
|
+
method to hook in your own lookup mechanism. For instance, if you want to be
|
961
|
+
able to use more than one view directory:
|
962
|
+
|
963
|
+
set :views, ['views', 'templates']
|
964
|
+
|
965
|
+
helpers do
|
966
|
+
def find_template(views, name, engine, &block)
|
967
|
+
Array(views).each { |v| super(v, name, engine, &block) }
|
968
|
+
end
|
969
|
+
end
|
970
|
+
|
971
|
+
Another example would be using different directories for different engines:
|
972
|
+
|
973
|
+
set :views, :sass => 'views/sass', :haml => 'templates', :default => 'views'
|
974
|
+
|
975
|
+
helpers do
|
976
|
+
def find_template(views, name, engine, &block)
|
977
|
+
_, folder = views.detect { |k,v| engine == Tilt[k] }
|
978
|
+
folder ||= views[:default]
|
979
|
+
super(folder, name, engine, &block)
|
980
|
+
end
|
981
|
+
end
|
982
|
+
|
983
|
+
You can also easily wrap this up in an extension and share with others!
|
984
|
+
|
985
|
+
Note that <tt>find_template</tt> does not check if the file really exists but
|
986
|
+
rather calls the given block for all possible paths. This is not a performance
|
987
|
+
issue, since +render+ will use +break+ as soon as a file is found. Also,
|
988
|
+
template locations (and content) will be cached if you are not running in
|
989
|
+
development mode. You should keep that in mind if you write a really crazy
|
990
|
+
method.
|
991
|
+
|
679
992
|
== Configuration
|
680
993
|
|
681
994
|
Run once, at startup, in any environment:
|
682
995
|
|
683
996
|
configure do
|
684
|
-
|
997
|
+
# setting one option
|
998
|
+
set :option, 'value'
|
999
|
+
|
1000
|
+
# setting multiple options
|
1001
|
+
set :a => 1, :b => 2
|
1002
|
+
|
1003
|
+
# same as `set :option, true`
|
1004
|
+
enable :option
|
1005
|
+
|
1006
|
+
# same as `set :option, false`
|
1007
|
+
disable :option
|
1008
|
+
|
1009
|
+
# you can also have dynamic settings with blocks
|
1010
|
+
set(:css_dir) { File.join(views, 'css') }
|
685
1011
|
end
|
686
1012
|
|
687
1013
|
Run only when the environment (RACK_ENV environment variable) is set to
|
@@ -698,7 +1024,102 @@ Run when the environment is set to either <tt>:production</tt> or
|
|
698
1024
|
...
|
699
1025
|
end
|
700
1026
|
|
701
|
-
|
1027
|
+
You can access those options via <tt>settings</tt>:
|
1028
|
+
|
1029
|
+
configure do
|
1030
|
+
set :foo, 'bar'
|
1031
|
+
end
|
1032
|
+
|
1033
|
+
get '/' do
|
1034
|
+
settings.foo? # => true
|
1035
|
+
settings.foo # => 'bar'
|
1036
|
+
...
|
1037
|
+
end
|
1038
|
+
|
1039
|
+
=== Available Settings
|
1040
|
+
|
1041
|
+
[absolute_redirects] If disabled, Sinatra will allow relative redirects,
|
1042
|
+
however, Sinatra will no longer conform with RFC 2616
|
1043
|
+
(HTTP 1.1), which only allows absolute redirects.
|
1044
|
+
|
1045
|
+
Enable if your app is running behind a reverse proxy that
|
1046
|
+
has not been set up properly. Note that the +url+ helper
|
1047
|
+
will still produce absolute URLs, unless you pass in
|
1048
|
+
+false+ as second parameter.
|
1049
|
+
|
1050
|
+
Disabled per default.
|
1051
|
+
|
1052
|
+
[add_charsets] mime types the <tt>content_type</tt> helper will
|
1053
|
+
automatically add the charset info to.
|
1054
|
+
|
1055
|
+
You should add to it rather than overriding this option:
|
1056
|
+
|
1057
|
+
settings.add_charsets << "application/foobar"
|
1058
|
+
|
1059
|
+
[app_file] main application file, used to detect project root,
|
1060
|
+
views and public folder and inline templates
|
1061
|
+
|
1062
|
+
[bind] IP address to bind to (default: 0.0.0.0).
|
1063
|
+
Only used for built-in server.
|
1064
|
+
|
1065
|
+
[default_encoding] encoding to assume if unknown
|
1066
|
+
(defaults to <tt>"utf-8"</tt>)
|
1067
|
+
|
1068
|
+
[dump_errors] display errors in the log
|
1069
|
+
|
1070
|
+
[environment] current environment, defaults to <tt>ENV['RACK_ENV']</tt>,
|
1071
|
+
or <tt>"development"</tt> if not available
|
1072
|
+
|
1073
|
+
[logging] use the logger
|
1074
|
+
|
1075
|
+
[lock] Places a lock around every request, only running
|
1076
|
+
processing on request per Ruby process concurrently.
|
1077
|
+
|
1078
|
+
Enabled if your app is not thread-safe.
|
1079
|
+
Disabled per default.
|
1080
|
+
|
1081
|
+
[method_override] use <tt>_method</tt> magic to allow put/delete forms in
|
1082
|
+
browsers that don't support it
|
1083
|
+
|
1084
|
+
[port] Port to listen on. Only used for built-in server.
|
1085
|
+
|
1086
|
+
[prefixed_redirects] Whether or not to insert <tt>request.script_name</tt> into
|
1087
|
+
redirects if no absolute path is given. That way
|
1088
|
+
<tt>redirect '/foo'</tt> would behave like
|
1089
|
+
<tt>redirect to('/foo')</tt>. Disabled per default.
|
1090
|
+
|
1091
|
+
[public] folder public files are server from
|
1092
|
+
|
1093
|
+
[reload_templates] whether or not to reload templates between requests.
|
1094
|
+
enabled in development mode and on Ruby 1.8.6 (to
|
1095
|
+
compensate a bug in Ruby causing a memory leak)
|
1096
|
+
|
1097
|
+
[root] project root folder
|
1098
|
+
|
1099
|
+
[raise_errors] raise exceptions (will stop application)
|
1100
|
+
|
1101
|
+
[run] if enabled, Sinatra will handle starting the webserver,
|
1102
|
+
do not enable if using rackup or other means.
|
1103
|
+
|
1104
|
+
[running] is the built-in server running now?
|
1105
|
+
do not change this setting!
|
1106
|
+
|
1107
|
+
[server] server or list of servers to use for built-in server.
|
1108
|
+
defaults to ['thin', 'mongrel', 'webrick'], order indicates
|
1109
|
+
priority.
|
1110
|
+
|
1111
|
+
[sessions] enable cookie based sessions
|
1112
|
+
|
1113
|
+
[show_exceptions] show a stacktrace in the browser
|
1114
|
+
|
1115
|
+
[static] Whether Sinatra should handle serving static files.
|
1116
|
+
Disable when using a Server able to do this on its own.
|
1117
|
+
Disabling will boost performance.
|
1118
|
+
Enabled per default.
|
1119
|
+
|
1120
|
+
[views] views folder
|
1121
|
+
|
1122
|
+
== Error Handling
|
702
1123
|
|
703
1124
|
Error handlers run within the same context as routes and before filters, which
|
704
1125
|
means you get all the goodies it has to offer, like <tt>haml</tt>,
|
@@ -758,7 +1179,7 @@ Or a range:
|
|
758
1179
|
Sinatra installs special <tt>not_found</tt> and <tt>error</tt> handlers when
|
759
1180
|
running under the development environment.
|
760
1181
|
|
761
|
-
== Mime
|
1182
|
+
== Mime Types
|
762
1183
|
|
763
1184
|
When using <tt>send_file</tt> or static files you may have mime types Sinatra
|
764
1185
|
doesn't understand. Use +mime_type+ to register them by file extension:
|
@@ -861,13 +1282,6 @@ etc.). That's where Sinatra::Base comes into play:
|
|
861
1282
|
end
|
862
1283
|
end
|
863
1284
|
|
864
|
-
The MyApp class is an independent Rack component that can act as
|
865
|
-
Rack middleware, a Rack application, or Rails metal. You can +use+ or
|
866
|
-
+run+ this class from a rackup +config.ru+ file; or, control a server
|
867
|
-
component shipped as a library:
|
868
|
-
|
869
|
-
MyApp.run! :host => 'localhost', :port => 9090
|
870
|
-
|
871
1285
|
The methods available to Sinatra::Base subclasses are exactly as those
|
872
1286
|
available via the top-level DSL. Most top-level apps can be converted to
|
873
1287
|
Sinatra::Base components with two modifications:
|
@@ -882,6 +1296,91 @@ Sinatra::Base components with two modifications:
|
|
882
1296
|
including the built-in server. See {Options and Configuration}[http://sinatra.github.com/configuration.html]
|
883
1297
|
for details on available options and their behavior.
|
884
1298
|
|
1299
|
+
=== Modular vs. Classic Style
|
1300
|
+
|
1301
|
+
Contrary to common believes, there is nothing wrong with classic style. If it
|
1302
|
+
suits your application, you do not have to switch to a modular application.
|
1303
|
+
|
1304
|
+
There are only two downsides compared to modulare style:
|
1305
|
+
|
1306
|
+
* You may only have one Sinatra application per Ruby process - if you plan to
|
1307
|
+
use more, switch to modular style.
|
1308
|
+
|
1309
|
+
* Classic style pollutes Object with delegator methods - if you plan to ship
|
1310
|
+
your application in a library/gem, switch to modular style.
|
1311
|
+
|
1312
|
+
There is no reason you cannot mix modular and classic style.
|
1313
|
+
|
1314
|
+
If switching from one style to the other, you should be aware of slight
|
1315
|
+
differences in the setting:
|
1316
|
+
|
1317
|
+
Setting Classic Modular
|
1318
|
+
|
1319
|
+
app_file file loading sinatra nil
|
1320
|
+
run $0 == app_file false
|
1321
|
+
logging true false
|
1322
|
+
method_override true false
|
1323
|
+
inline_templates true false
|
1324
|
+
|
1325
|
+
|
1326
|
+
=== Serving a Modular Application
|
1327
|
+
|
1328
|
+
There are two common options for starting a modular app, activly starting with
|
1329
|
+
<tt>run!</tt>:
|
1330
|
+
|
1331
|
+
# my_app.rb
|
1332
|
+
require 'sinatra/base'
|
1333
|
+
|
1334
|
+
class MyApp < Sinatra::Base
|
1335
|
+
# ... app code here ...
|
1336
|
+
|
1337
|
+
# start the server if ruby file executed directly
|
1338
|
+
run! if app_file == $0
|
1339
|
+
end
|
1340
|
+
|
1341
|
+
Start with:
|
1342
|
+
|
1343
|
+
ruby my_app.rb
|
1344
|
+
|
1345
|
+
Or with a <tt>config.ru</tt>, which allows using any Rack handler:
|
1346
|
+
|
1347
|
+
# config.ru
|
1348
|
+
require 'my_app'
|
1349
|
+
run MyApp
|
1350
|
+
|
1351
|
+
Run:
|
1352
|
+
|
1353
|
+
rackup -p 4567
|
1354
|
+
|
1355
|
+
=== Using a Classic Style Application with a config.ru
|
1356
|
+
|
1357
|
+
Write your app file:
|
1358
|
+
|
1359
|
+
# app.rb
|
1360
|
+
require 'sinatra'
|
1361
|
+
|
1362
|
+
get '/' do
|
1363
|
+
'Hello world!'
|
1364
|
+
end
|
1365
|
+
|
1366
|
+
And a corresponding <tt>config.ru</tt>:
|
1367
|
+
|
1368
|
+
require 'app'
|
1369
|
+
run Sinatra::Application
|
1370
|
+
|
1371
|
+
=== When to use a config.ru?
|
1372
|
+
|
1373
|
+
Good signs you probably want to use a <tt>config.ru</tt>:
|
1374
|
+
|
1375
|
+
* You want to deploy with a different Rack handler (Passenger, Unicorn,
|
1376
|
+
Heroku, ...).
|
1377
|
+
* You want to use more than one subclass of <tt>Sinatra::Base</tt>.
|
1378
|
+
* You want to use Sinatra only for middleware, but not as endpoint.
|
1379
|
+
|
1380
|
+
<b>There is no need to switch to a <tt>config.ru</tt> only because you
|
1381
|
+
switched to modular style, and you don't have to use modular style for running
|
1382
|
+
with a <tt>config.ru</tt>.</b>
|
1383
|
+
|
885
1384
|
=== Using Sinatra as Middleware
|
886
1385
|
|
887
1386
|
Not only is Sinatra able to use other Rack middleware, any Sinatra application
|
@@ -928,11 +1427,11 @@ available.
|
|
928
1427
|
Every Sinatra application corresponds to a subclass of Sinatra::Base. If you
|
929
1428
|
are using the top level DSL (<tt>require 'sinatra'</tt>), then this class is
|
930
1429
|
Sinatra::Application, otherwise it is the subclass you created explicitly. At
|
931
|
-
class level you have methods like
|
932
|
-
|
1430
|
+
class level you have methods like +get+ or +before+, but you cannot access the
|
1431
|
+
+request+ object or the +session+, as there only is a single application class
|
933
1432
|
for all requests.
|
934
1433
|
|
935
|
-
Options created via
|
1434
|
+
Options created via +set+ are methods at class level:
|
936
1435
|
|
937
1436
|
class MyApp < Sinatra::Base
|
938
1437
|
# Hey, I'm in the application scope!
|
@@ -948,21 +1447,21 @@ You have the application scope binding inside:
|
|
948
1447
|
|
949
1448
|
* Your application class body
|
950
1449
|
* Methods defined by extensions
|
951
|
-
* The block passed to
|
952
|
-
* Procs/blocks used as value for
|
1450
|
+
* The block passed to +helpers+
|
1451
|
+
* Procs/blocks used as value for +set+
|
953
1452
|
|
954
1453
|
You can reach the scope object (the class) like this:
|
955
1454
|
|
956
1455
|
* Via the object passed to configure blocks (<tt>configure { |c| ... }</tt>)
|
957
|
-
*
|
1456
|
+
* +settings+ from within request scope
|
958
1457
|
|
959
1458
|
=== Request/Instance Scope
|
960
1459
|
|
961
1460
|
For every incoming request, a new instance of your application class is
|
962
1461
|
created and all handler blocks run in that scope. From within this scope you
|
963
|
-
can access the
|
964
|
-
|
965
|
-
scope via the
|
1462
|
+
can access the +request+ and +session+ object or call rendering methods like
|
1463
|
+
+erb+ or +haml+. You can access the application scope from within the request
|
1464
|
+
scope via the +settings+ helper:
|
966
1465
|
|
967
1466
|
class MyApp < Sinatra::Base
|
968
1467
|
# Hey, I'm in the application scope!
|
@@ -992,19 +1491,19 @@ The delegation scope just forwards methods to the class scope. However, it
|
|
992
1491
|
does not behave 100% like the class scope, as you do not have the class'
|
993
1492
|
binding: Only methods explicitly marked for delegation are available and you
|
994
1493
|
do not share variables/state with the class scope (read: you have a different
|
995
|
-
|
1494
|
+
+self+). You can explicitly add method delegations by calling
|
996
1495
|
<tt>Sinatra::Delegator.delegate :method_name</tt>.
|
997
1496
|
|
998
1497
|
You have the delegate scope binding inside:
|
999
1498
|
|
1000
1499
|
* The top level binding, if you did <tt>require "sinatra"</tt>
|
1001
|
-
* An object extended with the
|
1500
|
+
* An object extended with the <tt>Sinatra::Delegator</tt> mixin
|
1002
1501
|
|
1003
1502
|
Have a look at the code for yourself: here's the
|
1004
1503
|
{Sinatra::Delegator mixin}[http://github.com/sinatra/sinatra/blob/ceac46f0bc129a6e994a06100aa854f606fe5992/lib/sinatra/base.rb#L1128]
|
1005
1504
|
being {included into the main namespace}[http://github.com/sinatra/sinatra/blob/ceac46f0bc129a6e994a06100aa854f606fe5992/lib/sinatra/main.rb#L28].
|
1006
1505
|
|
1007
|
-
== Command
|
1506
|
+
== Command Line
|
1008
1507
|
|
1009
1508
|
Sinatra applications can be run directly:
|
1010
1509
|
|
@@ -1020,32 +1519,67 @@ Options are:
|
|
1020
1519
|
-x # turn on the mutex lock (default is off)
|
1021
1520
|
|
1022
1521
|
== The Bleeding Edge
|
1522
|
+
If you would like to use Sinatra's latest bleeding code, feel free to run your
|
1523
|
+
application against the master branch, it should be rather stable.
|
1524
|
+
|
1525
|
+
We also push out prerelease gems from time to time, so you can do a
|
1526
|
+
|
1527
|
+
gem install sinatra --pre
|
1528
|
+
|
1529
|
+
To get some of the latest features.
|
1530
|
+
|
1531
|
+
=== With Bundler
|
1532
|
+
If you want to run your application with the latest Sinatra, using
|
1533
|
+
{Bundler}[http://gembundler.com/] is the recommend way.
|
1534
|
+
|
1535
|
+
First, install bundler, if you haven't:
|
1023
1536
|
|
1024
|
-
|
1025
|
-
|
1026
|
-
|
1537
|
+
gem install bundler
|
1538
|
+
|
1539
|
+
Then, in you project directory, create a +Gemfile+:
|
1540
|
+
|
1541
|
+
source :rubygems
|
1542
|
+
gem 'sinatra', :git => "git://github.com/sinatra/sinatra.git"
|
1543
|
+
|
1544
|
+
# other dependencies
|
1545
|
+
gem 'haml' # for instance, if you use haml
|
1546
|
+
gem 'activerecord', '~> 3.0' # maybe you also need ActiveRecord 3.x
|
1547
|
+
|
1548
|
+
Note that you will have to list all your applications dependencies in there.
|
1549
|
+
Sinatra's direct dependencies (Rack and Tilt) will however be automatically
|
1550
|
+
fetched and added by Bundler.
|
1551
|
+
|
1552
|
+
Now you can run your app like this:
|
1553
|
+
|
1554
|
+
bundle exec ruby myapp.rb
|
1555
|
+
|
1556
|
+
=== Roll Your Own
|
1557
|
+
Create a local clone and run your app with the <tt>sinatra/lib</tt> directory
|
1558
|
+
on the <tt>LOAD_PATH</tt>:
|
1027
1559
|
|
1028
1560
|
cd myapp
|
1029
1561
|
git clone git://github.com/sinatra/sinatra.git
|
1030
1562
|
ruby -Isinatra/lib myapp.rb
|
1031
1563
|
|
1032
|
-
|
1033
|
-
|
1564
|
+
To update the Sinatra sources in the future:
|
1565
|
+
|
1566
|
+
cd myapp/sinatra
|
1567
|
+
git pull
|
1034
1568
|
|
1035
|
-
|
1036
|
-
require 'rubygems'
|
1037
|
-
require 'sinatra'
|
1569
|
+
=== Install Globally
|
1038
1570
|
|
1039
|
-
|
1040
|
-
"I'm running version " + Sinatra::VERSION
|
1041
|
-
end
|
1571
|
+
You can build the gem on your own:
|
1042
1572
|
|
1043
|
-
|
1573
|
+
git clone git://github.com/sinatra/sinatra.git
|
1574
|
+
cd sinatra
|
1575
|
+
rake sinatra.gemspec
|
1576
|
+
rake install
|
1044
1577
|
|
1045
|
-
|
1046
|
-
|
1578
|
+
If you install gems as root, the last step should be
|
1579
|
+
|
1580
|
+
sudo rake install
|
1047
1581
|
|
1048
|
-
==
|
1582
|
+
== Further Reading
|
1049
1583
|
|
1050
1584
|
* {Project Website}[http://www.sinatrarb.com/] - Additional documentation,
|
1051
1585
|
news, and links to other resources.
|