sinatra 1.1.2 → 1.1.3

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.

@@ -119,7 +119,7 @@ You can easily define your own conditions:
119
119
  "Sorry, you lost."
120
120
  end
121
121
 
122
- === Return values
122
+ === Return Values
123
123
 
124
124
  The return value of a route block determines at least the response body passed
125
125
  on to the HTTP client, or at least the next middleware in the Rack stack.
@@ -170,7 +170,7 @@ directly.
170
170
 
171
171
  === Haml Templates
172
172
 
173
- The haml gem/library is required to render HAML templates:
173
+ The <tt>haml</tt> gem/library is required to render HAML templates:
174
174
 
175
175
  ## You'll need to require haml in your app
176
176
  require 'haml'
@@ -202,11 +202,11 @@ and overridden on an individual basis.
202
202
  erb :index
203
203
  end
204
204
 
205
- Renders <tt>./views/index.erb</tt>
205
+ Renders <tt>./views/index.erb</tt>.
206
206
 
207
- === Erubis
207
+ === Erubis Templates
208
208
 
209
- The erubis gem/library is required to render erubis templates:
209
+ The <tt>erubis</tt> gem/library is required to render Erubis templates:
210
210
 
211
211
  ## You'll need to require erubis in your app
212
212
  require 'erubis'
@@ -215,11 +215,22 @@ The erubis gem/library is required to render erubis templates:
215
215
  erubis :index
216
216
  end
217
217
 
218
- Renders <tt>./views/index.erubis</tt>
218
+ Renders <tt>./views/index.erubis</tt>.
219
+
220
+ It is also possible to replace Erb with Erubis:
221
+
222
+ require 'erubis'
223
+ Tilt.register :erb, Tilt[:erubis]
224
+
225
+ get '/' do
226
+ erb :index
227
+ end
228
+
229
+ Renders <tt>./views/index.erb</tt> with Erubis.
219
230
 
220
231
  === Builder Templates
221
232
 
222
- The builder gem/library is required to render builder templates:
233
+ The <tt>builder</tt> gem/library is required to render builder templates:
223
234
 
224
235
  ## You'll need to require builder in your app
225
236
  require 'builder'
@@ -232,7 +243,7 @@ Renders <tt>./views/index.builder</tt>.
232
243
 
233
244
  === Nokogiri Templates
234
245
 
235
- The nokogiri gem/library is required to render nokogiri templates:
246
+ The <tt>nokogiri</tt> gem/library is required to render nokogiri templates:
236
247
 
237
248
  ## You'll need to require nokogiri in your app
238
249
  require 'nokogiri'
@@ -245,7 +256,7 @@ Renders <tt>./views/index.nokogiri</tt>.
245
256
 
246
257
  === Sass Templates
247
258
 
248
- The haml gem/library is required to render Sass templates:
259
+ The <tt>haml</tt> or <tt>sass</tt> gem/library is required to render Sass templates:
249
260
 
250
261
  ## You'll need to require haml or sass in your app
251
262
  require 'sass'
@@ -269,7 +280,7 @@ and overridden on an individual basis.
269
280
 
270
281
  === Scss Templates
271
282
 
272
- The haml gem/library is required to render Scss templates:
283
+ The <tt>haml</tt> or <tt>sass</tt> gem/library is required to render Scss templates:
273
284
 
274
285
  ## You'll need to require haml or sass in your app
275
286
  require 'sass'
@@ -293,7 +304,7 @@ and overridden on an individual basis.
293
304
 
294
305
  === Less Templates
295
306
 
296
- The less gem/library is required to render Less templates:
307
+ The <tt>less</tt> gem/library is required to render Less templates:
297
308
 
298
309
  ## You'll need to require less in your app
299
310
  require 'less'
@@ -306,7 +317,7 @@ Renders <tt>./views/stylesheet.less</tt>.
306
317
 
307
318
  === Liquid Templates
308
319
 
309
- The liquid gem/library is required to render Liquid templates:
320
+ The <tt>liquid</tt> gem/library is required to render Liquid templates:
310
321
 
311
322
  ## You'll need to require liquid in your app
312
323
  require 'liquid'
@@ -324,7 +335,7 @@ template, you almost always want to pass locals to it:
324
335
 
325
336
  === Markdown Templates
326
337
 
327
- The rdiscount gem/library is required to render Markdown templates:
338
+ The <tt>rdiscount</tt> gem/library is required to render Markdown templates:
328
339
 
329
340
  ## You'll need to require rdiscount in your app
330
341
  require "rdiscount"
@@ -336,18 +347,34 @@ The rdiscount gem/library is required to render Markdown templates:
336
347
  Renders <tt>./views/index.markdown</tt> (+md+ and +mkd+ are also valid file
337
348
  extensions).
338
349
 
339
- It is not possible to call methods from markdown, nor to pass locals to it. You therefore will usually use it in combination with another rendering engine:
350
+ It is not possible to call methods from markdown, nor to pass locals to it.
351
+ You therefore will usually use it in combination with another rendering
352
+ engine:
340
353
 
341
354
  erb :overview, :locals => { :text => markdown(:introduction) }
342
355
 
343
- Note that you may also call the markdown method from within other templates:
356
+ Note that you may also call the +markdown+ method from within other templates:
344
357
 
345
358
  %h1 Hello From Haml!
346
359
  %p= markdown(:greetings)
347
360
 
361
+ It is also possible to parse Markdown with BlueCloth rather than RDiscount:
362
+
363
+ require 'bluecloth'
364
+
365
+ Tilt.register 'markdown', BlueClothTemplate
366
+ Tilt.register 'mkd', BlueClothTemplate
367
+ Tilt.register 'md', BlueClothTemplate
368
+
369
+ get '/' do
370
+ markdown :index
371
+ end
372
+
373
+ Renders <tt>./views/index.md</tt> with BlueCloth.
374
+
348
375
  === Textile Templates
349
376
 
350
- The RedCloth gem/library is required to render Textile templates:
377
+ The <tt>RedCloth</tt> gem/library is required to render Textile templates:
351
378
 
352
379
  ## You'll need to require redcloth in your app
353
380
  require "redcloth"
@@ -358,18 +385,19 @@ The RedCloth gem/library is required to render Textile templates:
358
385
 
359
386
  Renders <tt>./views/index.textile</tt>.
360
387
 
361
- It is not possible to call methods from textile, nor to pass locals to it. You therefore will usually use it in combination with another rendering engine:
388
+ It is not possible to call methods from textile, nor to pass locals to it. You
389
+ therefore will usually use it in combination with another rendering engine:
362
390
 
363
391
  erb :overview, :locals => { :text => textile(:introduction) }
364
392
 
365
- Note that you may also call the textile method from within other templates:
393
+ Note that you may also call the +textile+ method from within other templates:
366
394
 
367
395
  %h1 Hello From Haml!
368
396
  %p= textile(:greetings)
369
397
 
370
398
  === RDoc Templates
371
399
 
372
- The RDoc gem/library is required to render RDoc templates:
400
+ The <tt>rdoc</tt> gem/library is required to render RDoc templates:
373
401
 
374
402
  ## You'll need to require rdoc in your app
375
403
  require "rdoc"
@@ -380,18 +408,19 @@ The RDoc gem/library is required to render RDoc templates:
380
408
 
381
409
  Renders <tt>./views/index.rdoc</tt>.
382
410
 
383
- It is not possible to call methods from rdoc, nor to pass locals to it. You therefore will usually use it in combination with another rendering engine:
411
+ It is not possible to call methods from rdoc, nor to pass locals to it. You
412
+ therefore will usually use it in combination with another rendering engine:
384
413
 
385
414
  erb :overview, :locals => { :text => rdoc(:introduction) }
386
415
 
387
- Note that you may also call the rdoc method from within other templates:
416
+ Note that you may also call the +rdoc+ method from within other templates:
388
417
 
389
418
  %h1 Hello From Haml!
390
419
  %p= rdoc(:greetings)
391
420
 
392
421
  === Radius Templates
393
422
 
394
- The radius gem/library is required to render Radius templates:
423
+ The <tt>radius</tt> gem/library is required to render Radius templates:
395
424
 
396
425
  ## You'll need to require radius in your app
397
426
  require 'radius'
@@ -409,7 +438,7 @@ template, you almost always want to pass locals to it:
409
438
 
410
439
  === Markaby Templates
411
440
 
412
- The markaby gem/library is required to render Markaby templates:
441
+ The <tt>markaby</tt> gem/library is required to render Markaby templates:
413
442
 
414
443
  ## You'll need to require markaby in your app
415
444
  require 'markaby'
@@ -422,8 +451,16 @@ Renders <tt>./views/index.mab</tt>.
422
451
 
423
452
  === CoffeeScript Templates
424
453
 
425
- The coffee-script gem/library and the `coffee` binary are required to render
426
- CoffeeScript templates:
454
+ The <tt>coffee-script</tt> gem/library and at least <b>one</b> of the
455
+ following options to execute JavaScript:
456
+
457
+ * +node+ (from Node.js) in your path
458
+ * you must be running on OSX
459
+ * +therubyracer+ gem/library
460
+
461
+ See http://github.com/josh/ruby-coffee-script for an updated list of options.
462
+
463
+ Now you can render CoffeeScript templates:
427
464
 
428
465
  ## You'll need to require coffee-script in your app
429
466
  require 'coffee-script'
@@ -434,13 +471,13 @@ CoffeeScript templates:
434
471
 
435
472
  Renders <tt>./views/application.coffee</tt>.
436
473
 
437
- === Inline Templates
474
+ === Embedded Templates
438
475
 
439
476
  get '/' do
440
477
  haml '%div.title Hello World'
441
478
  end
442
479
 
443
- Renders the inlined template string.
480
+ Renders the embedded template string.
444
481
 
445
482
  === Accessing Variables in Templates
446
483
 
@@ -502,12 +539,38 @@ Templates may also be defined using the top-level <tt>template</tt> method:
502
539
  end
503
540
 
504
541
  If a template named "layout" exists, it will be used each time a template
505
- is rendered. You can disable layouts by passing <tt>:layout => false</tt>.
542
+ is rendered. You can individually disable layouts by passing <tt>:layout => false</tt>
543
+ or disable them by default via <tt>set :haml, :layout => false</tt>.
506
544
 
507
545
  get '/' do
508
546
  haml :index, :layout => !request.xhr?
509
547
  end
510
548
 
549
+ === Associating File Extensions
550
+
551
+ To associate a file extension with a template engine, use
552
+ <tt>Tilt.register</tt>. For instance, if you like to use the file extension
553
+ +tt+ for Textile templates, you can do the following:
554
+
555
+ Tilt.register :tt, Tilt[:textile]
556
+
557
+ === Adding You Own Template Engine
558
+
559
+ First, register your engine with Tilt, then create a rendering method:
560
+
561
+ Tilt.register :myat, MyAwesomeTemplateEngine
562
+
563
+ helpers do
564
+ def myat(*args) render(:myat, *args) end
565
+ end
566
+
567
+ get '/' do
568
+ myat :index
569
+ end
570
+
571
+ Renders <tt>./views/index.myat</tt>. See https://github.com/rtomayko/tilt to
572
+ learn more about Tilt.
573
+
511
574
  == Helpers
512
575
 
513
576
  Use the top-level <tt>helpers</tt> method to define helper methods for use in
@@ -547,6 +610,10 @@ and routes are accessible by after filters:
547
610
  puts response.status
548
611
  end
549
612
 
613
+ Note: Unless you use the `body` method rather than just returning a String from
614
+ the routes, the body will not yet be available in the after filter, since it is
615
+ generated later on.
616
+
550
617
  Filters optionally taking a pattern, causing them to be evaluated only if the
551
618
  request path matches that pattern:
552
619
 
@@ -648,7 +715,20 @@ The <tt>request.body</tt> is an IO or StringIO object:
648
715
  Run once, at startup, in any environment:
649
716
 
650
717
  configure do
651
- ...
718
+ # setting one option
719
+ set :option, 'value'
720
+
721
+ # setting multiple options
722
+ set :a => 1, :b => 2
723
+
724
+ # same as `set :option, true`
725
+ enable :option
726
+
727
+ # same as `set :option, false`
728
+ disable :option
729
+
730
+ # you can also have dynamic settings with blocks
731
+ set(:css_dir) { File.join(views, 'css') }
652
732
  end
653
733
 
654
734
  Run only when the environment (RACK_ENV environment variable) is set to
@@ -665,7 +745,19 @@ Run when the environment is set to either <tt>:production</tt> or
665
745
  ...
666
746
  end
667
747
 
668
- == Error handling
748
+ You can access those options via <tt>settings</tt>:
749
+
750
+ configure do
751
+ set :foo, 'bar'
752
+ end
753
+
754
+ get '/' do
755
+ settings.foo? # => true
756
+ settings.foo # => 'bar'
757
+ ...
758
+ end
759
+
760
+ == Error Handling
669
761
 
670
762
  Error handlers run within the same context as routes and before filters, which
671
763
  means you get all the goodies it has to offer, like <tt>haml</tt>,
@@ -725,7 +817,7 @@ Or a range:
725
817
  Sinatra installs special <tt>not_found</tt> and <tt>error</tt> handlers when
726
818
  running under the development environment.
727
819
 
728
- == Mime types
820
+ == Mime Types
729
821
 
730
822
  When using <tt>send_file</tt> or static files you may have mime types Sinatra
731
823
  doesn't understand. Use +mime_type+ to register them by file extension:
@@ -828,13 +920,6 @@ etc.). That's where Sinatra::Base comes into play:
828
920
  end
829
921
  end
830
922
 
831
- The MyApp class is an independent Rack component that can act as
832
- Rack middleware, a Rack application, or Rails metal. You can +use+ or
833
- +run+ this class from a rackup +config.ru+ file; or, control a server
834
- component shipped as a library:
835
-
836
- MyApp.run! :host => 'localhost', :port => 9090
837
-
838
923
  The methods available to Sinatra::Base subclasses are exactly as those
839
924
  available via the top-level DSL. Most top-level apps can be converted to
840
925
  Sinatra::Base components with two modifications:
@@ -849,6 +934,91 @@ Sinatra::Base components with two modifications:
849
934
  including the built-in server. See {Options and Configuration}[http://sinatra.github.com/configuration.html]
850
935
  for details on available options and their behavior.
851
936
 
937
+ === Modular vs. Classic Style
938
+
939
+ Contrary to common believes, there is nothing wrong with classic style. If it
940
+ suits your application, you do not have to switch to a modular application.
941
+
942
+ There are only two downsides compared to modulare style:
943
+
944
+ * You may only have one Sinatra application per Ruby process - if you plan to
945
+ use more, switch to modular style.
946
+
947
+ * Classic style pollutes Object with delegator methods - if you plan to ship
948
+ your application in a library/gem, switch to modular style.
949
+
950
+ There is no reason you cannot mix modular and classic style.
951
+
952
+ If switching from one style to the other, you should be aware of slight
953
+ differences in the setting:
954
+
955
+ Setting Classic Modular
956
+
957
+ app_file file loading sinatra nil
958
+ run $0 == app_file false
959
+ logging true false
960
+ method_override true false
961
+ inline_templates true false
962
+
963
+
964
+ === Serving a Modular Application
965
+
966
+ There are two common options for starting a modular app, activly starting with
967
+ <tt>run!</tt>:
968
+
969
+ # my_app.rb
970
+ require 'sinatra/base'
971
+
972
+ class MyApp < Sinatra::Base
973
+ # ... app code here ...
974
+
975
+ # start the server if ruby file executed directly
976
+ run! if app_file == $0
977
+ end
978
+
979
+ Start with:
980
+
981
+ ruby my_app.rb
982
+
983
+ Or with a <tt>config.ru</tt>, which allows using any Rack handler:
984
+
985
+ # config.ru
986
+ require 'my_app'
987
+ run MyApp
988
+
989
+ Run:
990
+
991
+ rackup -p 4567
992
+
993
+ === Using a Classic Style Application with a config.ru
994
+
995
+ Write your app file:
996
+
997
+ # app.rb
998
+ require 'sinatra'
999
+
1000
+ get '/' do
1001
+ 'Hello world!'
1002
+ end
1003
+
1004
+ And a corresponding <tt>config.ru</tt>:
1005
+
1006
+ require 'app'
1007
+ run Sinatra::Application
1008
+
1009
+ === When to use a config.ru?
1010
+
1011
+ Good signs you probably want to use a <tt>config.ru</tt>:
1012
+
1013
+ * You want to deploy with a different Rack handler (Passenger, Unicorn,
1014
+ Heroku, ...).
1015
+ * You want to use more than one subclass of <tt>Sinatra::Base</tt>.
1016
+ * You want to use Sinatra only for middleware, but not as endpoint.
1017
+
1018
+ <b>There is no need to switch to a <tt>config.ru</tt> only because you
1019
+ switched to modular style, and you don't have to use modular style for running
1020
+ with a <tt>config.ru</tt>.</b>
1021
+
852
1022
  === Using Sinatra as Middleware
853
1023
 
854
1024
  Not only is Sinatra able to use other Rack middleware, any Sinatra application
@@ -971,7 +1141,7 @@ Have a look at the code for yourself: here's the
971
1141
  {Sinatra::Delegator mixin}[http://github.com/sinatra/sinatra/blob/ceac46f0bc129a6e994a06100aa854f606fe5992/lib/sinatra/base.rb#L1128]
972
1142
  being {included into the main namespace}[http://github.com/sinatra/sinatra/blob/ceac46f0bc129a6e994a06100aa854f606fe5992/lib/sinatra/main.rb#L28].
973
1143
 
974
- == Command line
1144
+ == Command Line
975
1145
 
976
1146
  Sinatra applications can be run directly:
977
1147
 
@@ -987,32 +1157,67 @@ Options are:
987
1157
  -x # turn on the mutex lock (default is off)
988
1158
 
989
1159
  == The Bleeding Edge
1160
+ If you would like to use Sinatra's latest bleeding code, feel free to run your
1161
+ application against the master branch, it should be rather stable.
1162
+
1163
+ We also push out prerelease gems from time to time, so you can do a
990
1164
 
991
- If you would like to use Sinatra's latest bleeding code, create a local
992
- clone and run your app with the <tt>sinatra/lib</tt> directory on the
993
- <tt>LOAD_PATH</tt>:
1165
+ gem install sinatra --pre
1166
+
1167
+ To get some of the latest features.
1168
+
1169
+ === With Bundler
1170
+ If you want to run your application with the latest Sinatra, using
1171
+ {Bundler}[http://gembundler.com/] is the recommend way.
1172
+
1173
+ First, install bundler, if you haven't:
1174
+
1175
+ gem install bundler
1176
+
1177
+ Then, in you project directory, create a +Gemfile+:
1178
+
1179
+ source :rubygems
1180
+ gem 'sinatra', :git => "git://github.com/sinatra/sinatra.git"
1181
+
1182
+ # other dependencies
1183
+ gem 'haml' # for instance, if you use haml
1184
+ gem 'activerecord', '~> 3.0' # maybe you also need ActiveRecord 3.x
1185
+
1186
+ Note that you will have to list all your applications dependencies in there.
1187
+ Sinatra's direct dependencies (Rack and Tilt) will however be automatically
1188
+ fetched and added by Bundler.
1189
+
1190
+ Now you can run your app like this:
1191
+
1192
+ bundle exec ruby myapp.rb
1193
+
1194
+ === Roll Your Own
1195
+ Create a local clone and run your app with the <tt>sinatra/lib</tt> directory
1196
+ on the <tt>LOAD_PATH</tt>:
994
1197
 
995
1198
  cd myapp
996
1199
  git clone git://github.com/sinatra/sinatra.git
997
1200
  ruby -Isinatra/lib myapp.rb
998
1201
 
999
- Alternatively, you can add the <tt>sinatra/lib</tt> directory to the
1000
- <tt>LOAD_PATH</tt> in your application:
1202
+ To update the Sinatra sources in the future:
1001
1203
 
1002
- $LOAD_PATH.unshift File.dirname(__FILE__) + '/sinatra/lib'
1003
- require 'rubygems'
1004
- require 'sinatra'
1204
+ cd myapp/sinatra
1205
+ git pull
1005
1206
 
1006
- get '/about' do
1007
- "I'm running version " + Sinatra::VERSION
1008
- end
1207
+ === Install Globally
1009
1208
 
1010
- To update the Sinatra sources in the future:
1209
+ You can build the gem on your own:
1011
1210
 
1012
- cd myproject/sinatra
1013
- git pull
1211
+ git clone git://github.com/sinatra/sinatra.git
1212
+ cd sinatra
1213
+ rake sinatra.gemspec
1214
+ rake install
1215
+
1216
+ If you install gems as root, the last step should be
1217
+
1218
+ sudo rake install
1014
1219
 
1015
- == More
1220
+ == Further Reading
1016
1221
 
1017
1222
  * {Project Website}[http://www.sinatrarb.com/] - Additional documentation,
1018
1223
  news, and links to other resources.