can call... Each call does some work and returns control to the client.” “A framework embodies some abstract design, with more behavior built in... you need to insert your behavior into various places in the framework.” http://martinfowler.com/bliki/InversionOfControl.html Martin Fowler
:index end __END__ @@index <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Inline Template</title> </head <body> <h1>It Worked!</h1> </body> </html> ERB by default, but any templates supported by Tilt will work Each inline template must have a unique name
then '/about' when :index then '/index' else "/page/#{name}" end end # in your view template <a href="<%= link :about %>">About</a> # -- OR -- # define helpers as methods in a module module MyHelpers # helper methods end helpers MyHelpers
session[:foo] = Time.now "Session time was set." end get '/fetch' do "Session value: #{session[:foo]}" end get '/unset' do session.clear redirect '/fetch' end
class MyAppTest < Test::Unit::TestCase include Rack::Test::Methods def app Sinatra::Application end def test_my_default get '/' assert_equal 'Hello World!', last_response.body end def test_with_params get '/meet', :name => 'Frank' assert_equal 'Hello Frank!', last_response.body end def test_with_rack_env get '/', {}, 'HTTP_USER_AGENT' => 'Songbird' assert_equal "You're using Songbird!", last_response.body end end
a common misconception that modular applications are superior to classic applications, and that really advanced users only use modular style... This is utter nonsense and no one on the Sinatra core team shares this view. Sinatra is all about simplicity and if you can use a classic application, you should.” -- Sinatra Up and Running
Sinatra::Base get '/hi' do "Hello World!" end # Only start the server if the file # has been executed directly # # $0 is the executed file # __FILE__ is the current file run! if __FILE__ == $0 end
file named my_app.rb require 'sinatra/base' class MyApp < Sinatra::Base get '/hi' do "Hello World!" end end # in a separate file named config.ru require './my_app' run MyApp # launch the server using # rackup command or shotgun
file named my_app.rb require 'sinatra/base' class Foo < Sinatra::Base get('/foo') { 'foo' } end class Bar < Sinatra::Base get('/bar') { 'bar' } use Foo end # config.ru require 'my_app' run Bar # Or Foo and Bar can be in separate files # remember... it's just Ruby!
< Sinatra::Base get('/foo') { 'foo' } end class Bar < Sinatra::Base get('/bar') { 'bar' } end # config.ru require 'my_app' run Rack::Cascade, [Foo, Bar] # Differences: # * _slightly_ better performance, but only if you have a lot of endpoints # * you can use any Rack app (like Rails)